自定義 橫向的進度條

效果圖是這樣的:android

接下來爲你們粘貼代碼:canvas

首先是自定義的ProgressBar :微信

public class MyProgress extends ProgressBar {ide

String text;ui

Paint mPaint;this


public MyProgress(Context context) {spa

super(context);3d

// TODO Auto-generated constructor stubxml

System.out.println("1");ip

initText();

}


public MyProgress(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

// TODO Auto-generated constructor stub

System.out.println("2");

initText();


}


public MyProgress(Context context, AttributeSet attrs) {

super(context, attrs);

// TODO Auto-generated constructor stub

System.out.println("3");

initText();

}


@Override

public synchronized void setProgress(int progress) {

// TODO Auto-generated method stub

setText(progress);


super.setProgress(progress);


}


@Override

protected synchronized void onDraw(Canvas canvas) {

// TODO Auto-generated method stub

super.onDraw(canvas);

// this.setText();

Rect rect = new Rect();

this.mPaint.getTextBounds(this.text, 0, this.text.length(), rect);


int x = (getWidth() / 1) - rect.centerX() - 18;

int y = (getHeight() / 2) - rect.centerY();

canvas.drawText(this.text, x, y, this.mPaint);

}


// 初始化,畫筆

private void initText() {

this.mPaint = new Paint();

this.mPaint.setColor(Color.WHITE);


}


private void setText() {

setText(this.getProgress());


}


// 設置文字內容

private void setText(int progress) {

int i = (progress * 100) / this.getMax();

this.text = String.valueOf(i) + "%";


}


}


再者就是  actiivty裏面的代碼:

public class ProgressTest extends Activity {


private Button btn_go = null;

private MyProgress myProgress = null;

private Handler mHandler;

public TextView baifen;


/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

findView();

setParam();

addListener();


mHandler = new Handler(new Callback() {


@Override

public boolean handleMessage(Message msg) {

// TODO Auto-generated method stub

myProgress.setProgress(msg.what);

return false;

}

});


}


private void findView() {

btn_go = (Button) findViewById(R.id.btn_go);

myProgress = (MyProgress) findViewById(R.id.pgsBar);

// baifen = (TextView) findViewById(R.id.baifen);

}


private void setParam() {

btn_go.setText("開始");

}


private void addListener() {

btn_go.setOnClickListener(new OnClickListener() {


@Override

public void onClick(View v) {

// TODO Auto-generated method stub

new Thread(new Runnable() {

@Override

public void run() {

// TODO Auto-generated method stub

for (int i = 0; i <= 50; i++) {

mHandler.sendEmptyMessage(i * 2);

try {

Thread.sleep(100);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}).start();

}

});

}

}

還有就是須要在res文件夾下建立一個drawable文件夾;*****.xml,代碼以下:

<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >


    <item android:id="@android:id/background">

        <shape>

            <corners android:radius="3dip" />


            <gradient

                android:angle="270"

                android:centerY="0.75"

                android:endColor="#999999"

                android:startColor="#999999" />

        </shape>

    </item>

    <item android:id="@android:id/secondaryProgress">

        <clip>

            <shape>

                <corners android:radius="0dip" />


                <gradient

                    android:angle="270"

                    android:centerY="0.75"

                    android:endColor="#23b7a1"

                    android:startColor="#23b7a1" />

            </shape>

        </clip>

    </item>

    <item android:id="@android:id/progress">

        <clip>

            <shape>

                <corners android:radius="5dip" />


                <gradient

                    android:angle="270"

                    android:centerY="0.75"

                    android:endColor="#23b7a1"

                    android:startColor="#23b7a1" />

            </shape>

        </clip>

    </item>


</layer-list>


values文件夾下 stytles.xml  裏面添加代碼:

<!-- 自定義進度條 -->  

   <style name="ProgressBar_Mini" parent="@android:style/Widget.ProgressBar.Horizontal">  

       <item name="android:maxHeight">50dip</item>  

       <item name="android:minHeight">12dip</item>  

       <item name="android:indeterminateOnly">false</item>  

       <item name="android:indeterminateDrawable">@android:drawable/progress_indeterminate_horizontal</item>  

       <item name="android:progressDrawable">@drawable/progressbar_mini</item>  

   </style>



基本全部代碼我都粘貼到 這裏了,裏面會用到一些顏色值,這個大家能夠本身定義。

若是有用到的,不清楚怎麼寫的,能夠加我QQ:2268214831  或者微信:hanxinghui0817 私聊我哦

本人經驗不足,若有什麼錯誤還請大神多多指點,在這裏謝謝啦!但願本博文哭幫助到一些人!

相關文章
相關標籤/搜索