先上圖:
--
項目要求在很多界面中展示最底部的內存狀態:佔用空間,可用空間大小;考慮多界面複用,我自定義view
首先繪製佈局、
[java] view plain copy
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/base22dp"
android:orientation="vertical">
<ProgressBar
android:id="@+id/download_progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:progress="30"
android:progressDrawable="@drawable/progressbar"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_already"
android:layout_width="wrap_content"
android:layout_marginLeft="@dimen/base15dp"
android:textColor="@color/white"
android:textSize="@dimen/base13dp"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_unused"
android:layout_width="wrap_content"
android:layout_marginLeft="@dimen/base15dp"
android:textColor="@color/white"
android:textSize="@dimen/base13dp"
android:layout_height="wrap_content"/>
</LinearLayout>
</RelativeLayout>
自定義view方法:主要通過statFs 方法獲取總扇區大小以及剩餘扇區大小然後求比,把值賦給progressbar
/**
* 類名稱:
* 作者: aj
* 時間: 2017/11/8 下午6:32
* 功能:
*/
public class MemoryProgressBarView extends RelativeLayout {
private TextView tvAlready, tvUnused;
private final ProgressBar progressBar;
public MemoryProgressBarView(Context context, AttributeSet attrs) {
super(context, attrs);
// 加載佈局
View inflate = LayoutInflater.from(context).inflate(R.layout.view_memory, this);
// 獲取控件
tvAlready = (TextView) findViewById(R.id.tv_already);
tvUnused = (TextView) findViewById(R.id.tv_unused);
progressBar = (ProgressBar) findViewById(R.id.download_progressBar);
progressBar.setMax(100);
}
public void initProgressBar(Context context) {
//獲得sd卡的內存狀態
File sdcardFileDir = Environment.getExternalStorageDirectory();
String sdcardMemory = getMemoryInfo(sdcardFileDir, context);
//獲得手機內部存儲控件的狀態
File dataFileDir = Environment.getDataDirectory();
String dataMemory = getMemoryInfo(dataFileDir, context);
// tvAlready.setText("SD卡: " + sdcardMemory + "\n手機內部: " + dataMemory);
tvAlready.setText("手機內部: " + dataMemory);
}
/**
* 根據路徑獲取內存狀態
*
* @param path
* @return
*/
@SuppressWarnings("deprecation")
private String getMemoryInfo(File path, Context context) {
//獲得一個磁盤狀態對象
StatFs stat = new StatFs(path.getPath());
//獲得一個扇區的大小
long blockSize = stat.getBlockSize();
//獲得扇區的總數
long totalBlocks = stat.getBlockCount();
//獲得可用的扇區數量
long availableBlocks = stat.getAvailableBlocks();
//獲得已用的扇區數量
long alreadyBlocks = totalBlocks - availableBlocks;
//設置進度條
long l = (((alreadyBlocks * 100) / totalBlocks));
progressBar.setProgress((int) (l));
//總空間
// String totalMemory = Formatter.formatFileSize(context, totalBlocks * blockSize);
//佔用空間
String alreadyMemory = Formatter.formatFileSize(context, alreadyBlocks * blockSize);
//可用空間
String availableMemory = Formatter.formatFileSize(context, availableBlocks * blockSize);
return "佔用空間:" + alreadyMemory + " 可用空間:" + availableMemory;
}
}
運用:在頁面佈局引入自定義的view
<com.CustomView.MemoryProgressBarView
android:id="@+id/memory_progress"
android:layout_width="match_parent"
android:layout_height="@dimen/base22dp"/>
調用方法:
memoryView = (MemoryProgressBarView) findViewById(R.id.memory_progress);
memoryView.initProgressBar(context);
最終實現上圖效果
實現上圖效果