国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

安卓自定義控件--垂直進(jìn)度條

2019-11-08 18:50:35
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

安卓只給我們提供了水平的進(jìn)度條和環(huán)形進(jìn)度條,沒有垂直的進(jìn)度條,下面我們來(lái)開發(fā)個(gè)垂直的進(jìn)度條。

效果圖如下: 這里寫圖片描述

一、工作原理

其實(shí)就是畫一個(gè)矩形,改變矩形的高度就能模擬進(jìn)度的變化。當(dāng)進(jìn)度變化時(shí),改變矩形的高度,然后重繪即可。

二、代碼如下

1.VerticalPRogressBar.java( 繼承View):

private Paint paint;// 畫筆 private int progress;// 進(jìn)度值 private int width;// 寬度值 private int height;// 高度值 public VerticalProgressBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } public VerticalProgressBar(Context context, AttributeSet attrs) { super(context, attrs); init(); } public VerticalProgressBar(Context context) { super(context); init(); } private void init() { paint = new Paint(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); width = getMeasuredWidth() - 1;// 寬度值 height = getMeasuredHeight() - 1;// 高度值 } @Override protected void onDraw(Canvas canvas) { paint.setColor(Color.rgb(55, 200, 255));// 設(shè)置畫筆顏色 canvas.drawRect(0, height - progress / 100f * height, width, height, paint);// 畫矩形 canvas.drawLine(0, 0, width, 0, paint);// 畫頂邊 canvas.drawLine(0, 0, 0, height, paint);// 畫左邊 canvas.drawLine(width, 0, width, height, paint);// 畫右邊 canvas.drawLine(0, height, width, height, paint);// 畫底邊 paint.setColor(Color.RED);// 設(shè)置畫筆顏色為紅色 paint.setTextSize(width / 3);// 設(shè)置文字大小 canvas.drawText(progress + "%", (width - getTextWidth(progress + "%")) / 2, height / 2, paint);// 畫文字 super.onDraw(canvas); } /** * 拿到文字寬度 * @param str 傳進(jìn)來(lái)的字符串 * return 寬度 */ private int getTextWidth(String str) { // 計(jì)算文字所在矩形,可以得到寬高 Rect rect = new Rect(); paint.getTextBounds(str, 0, str.length(), rect); return rect.width(); } /** 設(shè)置progressbar進(jìn)度 */ public void setProgress(int progress) { this.progress = progress; postInvalidate(); }

2.MainActivity.java(繼承ActionBarActivity ):

private VerticalProgressBar vpProgressBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } /**初始化控件 */ private void initView() { vpProgressBar = (VerticalProgressBar) findViewById(R.id.vp_progress); run(); } /**測(cè)試progressbar*/ private void run() { new Thread(){ public void run() { try { for (int i= 0;i<=100;i++) { Thread.sleep(50);//休息50毫秒 vpProgressBar.setProgress(i);//更新進(jìn)度條進(jìn)度 } } catch (InterruptedException e) { e.printStackTrace(); } }; }.start(); }

3.布局文件activity_main.xml:

<com.example.verticalprogressbar.VerticalProgressBar android:id="@+id/vp_progress" android:layout_width="50dp" android:layout_height="300dp" android:layout_centerInParent="true" />

源碼地址:https://github.com/Jszgw/VerticalProgressBar


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 天等县| 万宁市| 建昌县| 金堂县| 彩票| 石城县| 文化| 益阳市| 浪卡子县| 泊头市| 江城| 民勤县| 长泰县| 黎川县| 新平| 襄城县| 安化县| 五河县| 平凉市| 河间市| 南安市| 固镇县| 新津县| 贡嘎县| 苗栗市| 洛宁县| 江陵县| 顺平县| 霍邱县| 嫩江县| 伊金霍洛旗| 南郑县| 宁明县| 临邑县| 政和县| 钦州市| 祁阳县| 祁阳县| 鹤壁市| 厦门市| 巴林左旗|