1. 程式人生 > >android ClipDrawable實現進度條

android ClipDrawable實現進度條

1、

在drawable 下建立一個<clip>標籤的佈局 progress.xml 如下

<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/pro"
    android:clipOrientation="horizontal"
    android:gravity="left" >
  
</clip>

android:drawable 引用的圖片資源


android:clipOrientation 控制裁剪的方向可以是horizontal 或者vertical 

android:gravity控制裁剪從哪裡開始 這裡表示從左開始

2、在佈局檔案中應用 eg:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

 <ImageView
        android:id="@+id/iv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         android:src="@drawable/progress"
        />


</RelativeLayout>

3.關鍵程式碼

private ImageView imageView;
  
private ClipDrawable  clipDrawable;

private   int  progress= 0

 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
imageView =(ImageView) findViewById(R.id.iv);
clipDrawable= (ClipDrawable) imageView.getDrawable();

clipDrawable.setLevel(2000);

}

      imageLevel 最大10000,預設是從0開始  在此可控制這個來實現進度條的變化