Android菜鳥練習第九課 自定義橫向進度條

第一部分 在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);
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章