安卓系统内存检测--StatFsProgressBar:自定义view

先上图:

--

项目要求在很多界面中展示最底部的内存状态:占用空间,可用空间大小;考虑多界面复用,我自定义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);
最终实现上图效果
实现上图效果

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值