Android菜鸟练习第九课 自定义横向进度条

这篇博客介绍了如何自定义Android的横向进度条,包括在drawable中定义样式、布局文件中的应用以及在activity中更新进度条的方法。通过XML定义渐变色背景、主次进度条,并在代码中实现下载进度的显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一部分 在drawable中自定义progressBar的样式

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- 背景  gradient是渐变,corners定义的是圆角 -->
    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="10dp" />
            <solid android:color="#f3f3f3" />
        </shape>
    </item>

    <!-- 第二条进度条颜色 -->
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="10dip" />

                <gradient
                    android:angle="90.0"
                    android:centerColor="#ac6079"
                    android:centerY="0.45"
                    android:endColor="#6c213a"
                    android:startColor="#e71a5e" />
            </shape>
        </clip>
    </item>

    <!-- 进度条 -->
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="10dip" />

                <solid android:color="#FF8080" />
            </shape>
        </clip>
    </item>

</layer-list>

第二部分 布局部分
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn_update"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="update APK!" />

    <ProgressBar
        android:layout_margin="25dp"
        android:background="@drawable/progress_bar"
        android:id="@+id/progressbar"
        style="?android:attr/progressBarStyleHorizontal"
        android:progressDrawable="@drawable/progress_bar"
        android:layout_width="match_parent"
        android:layout_height="15dp" />
</LinearLayout>

第三部分 activity部分,PS:Activity部分复用的Android菜鸟练习第八课内容具体请查看第八课详情
public class MainActivity extends Activity {
    FinalHttp fh;
    ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn_update).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                update();
            }
        });
        //实例化进度条
        progressBar = (ProgressBar) findViewById(R.id.progressbar);
    }

    public void update() {
        //apk安装路径
        String apkPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/qq.apk";
        //判断安装路径是否存在同名APK,若存在则删除该APK
        File f = new File(apkPath);
        if (f.exists()) {
            f.delete();
        }
        fh = new FinalHttp();
        //第一个参数为文件下载地址 第二个参数为APK安装路径
        fh.download("http://gdown.baidu.com/data/wisegame/4ae6d2d7378e6cdf/QQ_122.apk", apkPath,
                new AjaxCallBack<File>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        Toast.makeText(getApplicationContext(), "开始下载", Toast.LENGTH_SHORT).show();
                    }

                    //count为下载文件的总长度,current为当
                    @Override
                    public void onLoading(long count, long current) {
                        super.onLoading(count, current);
                        //下载进度的百分比
                        int progress = 0;
                        if (current != count && current != 0) {
                            progress = (int) (current / (float) count * 100);
                        } else {
                            progress = 100;
                        }
                        //设置进度条最大值和当前值
                        progressBar.setMax((int) count);
                        progressBar.setProgress((int) current);
                    }

                    @Override
                    public void onSuccess(File t) {
                        super.onSuccess(t);
                        Toast.makeText(getApplicationContext(), "下载完成", Toast.LENGTH_SHORT).show();
                        installApk(t);
                    }

                    @Override
                    public void onFailure(Throwable t, int errorNo, String strMsg) {
                        super.onFailure(t, errorNo, strMsg);
                        Toast.makeText(getApplicationContext(), "下载失败", Toast.LENGTH_SHORT).show();
                    }
                });
    }

    //安装APK的方法
    private void installApk(File t) {
        Toast.makeText(MainActivity.this, t.exists() + "", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.setDataAndType(Uri.fromFile(t),
                "application/vnd.android.package-archive");
        startActivity(intent);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值