自訂進度條(圓形、橫向進度條)

來源:互聯網
上載者:User

標籤:

自訂進度條實現大體流程
  1、自訂屬性聲明(attrs檔案)
  2、自訂屬性擷取
  3、測量(onMeasure)
  4、繪製(onDraw)

代碼:

 attrs檔案:

  

<!-- 自訂聲明 -->
<attr name="progress_unreach_color" format="color"></attr>
<attr name="progress_unreach_height" format="dimension"></attr>
<attr name="progress_reach_color" format="color"></attr>
<attr name="progress_reach_height" format="dimension"></attr>
<attr name="progress_text_color" format="color"></attr>
<attr name="progress_text_size" format="dimension"></attr>
<attr name="progress_text_offset" format="dimension"></attr>
<!-- 自訂使用 -->
<declare-styleable name="ProgressbarView">
<attr name="progress_unreach_color"></attr>
<attr name="progress_unreach_height"></attr>
<attr name="progress_reach_color"></attr>
<attr name="progress_reach_height"></attr>
<attr name="progress_text_color"></attr>
<attr name="progress_text_size"></attr>
<attr name="progress_text_offset"></attr>
</declare-styleable>

  布局檔案:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:hyman="http://schemas.android.com/apk/res/com.Horizontal.horizontalprogressbar"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<com.Horizontal.view.ProgressbarView
android:id="@+id/progress01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dip"
hyman:progress_reach_color="#f00"
hyman:progress_text_color="#f00"
hyman:progress_unreach_color="#000"
android:background="#d1d3d7"
android:padding="10dip"
android:progress="0" />

<com.Horizontal.view.RoundProgressbar
android:id="@+id/progress02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
hyman:progress_reach_color="#f3d4e3"
hyman:progress_text_color="#f00"
hyman:progress_unreach_color="#000"
android:layout_marginTop="30dp"
hyman:rudio="80dp"
android:padding="10dip"
android:progress="0"
/>
</LinearLayout>

</ScrollView>

ProgressbarView檔案:

package com.Horizontal.view;

import com.Horizontal.horizontalprogressbar.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.ProgressBar;

public class ProgressbarView extends ProgressBar {
private static final int DEFAULT_TEXT_SIZE = 10;// 字型大小 sp
private static final int DEFAULT_TEXT_COLOR = 0xFC00D1;
private static final int DEFAULT_REACH_HEIGHT = 3;// 左進度條高度 dp
private static final int DEFAULT_REACH_COLOR = DEFAULT_TEXT_COLOR;
private static final int DEFAULT_UNREACH_HEIGHT = 2;// 右進度條高度 dp
private static final int DEFAULT_UNREACH_COLOR = 0xD3D6D1;
private static final int DEFAULT_TEXT_OFFSET = 10;// 做進度條與文字的距離 dp

protected int mTextSize = sp2px(DEFAULT_TEXT_SIZE);
protected int mTextColor = DEFAULT_TEXT_COLOR;
protected int mReachHeight = dp2px(DEFAULT_REACH_HEIGHT);
protected int mReachColor = DEFAULT_REACH_COLOR;
protected int mUnReachHeight = dp2px(DEFAULT_UNREACH_HEIGHT);
protected int mUnReachColor = DEFAULT_UNREACH_COLOR;
protected int mTextOffset = dp2px(DEFAULT_TEXT_OFFSET);

protected Paint mPaint = new Paint();
// 控制項的寬度
private int mRealWidth;

public ProgressbarView(Context context) {
this(context, null);
}

public ProgressbarView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public ProgressbarView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
obtainStyledAttrs(attrs);
}

/** 擷取自訂屬性 **/
private void obtainStyledAttrs(AttributeSet attrs) {
// 擷取attr檔案中的styleable(ProgressbarView)
TypedArray ta = getContext().obtainStyledAttributes(attrs,
R.styleable.ProgressbarView);
// 擷取自訂屬性和預設值
mTextSize = (int) ta.getDimension(
R.styleable.ProgressbarView_progress_text_size, mTextSize);
mTextColor = ta.getColor(
R.styleable.ProgressbarView_progress_text_color, mTextColor);
mReachHeight = (int) ta
.getDimension(
R.styleable.ProgressbarView_progress_reach_height,
mReachHeight);
mReachColor = ta.getColor(
R.styleable.ProgressbarView_progress_reach_color, mReachColor);
mUnReachHeight = (int) ta.getDimension(
R.styleable.ProgressbarView_progress_unreach_height,
mUnReachHeight);
mUnReachColor = ta.getColor(
R.styleable.ProgressbarView_progress_unreach_color,
mUnReachColor);
mTextOffset = (int) ta.getDimension(
R.styleable.ProgressbarView_progress_text_offset, mTextOffset);

ta.recycle();
mPaint.setTextSize(mTextSize);
}

/** 寬度和高度的測量 **/
@Override
protected synchronized void onMeasure(int widthMeasureSpec,
int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// 拿寬度的模式
// int widthMode = MeasureSpec.getMode(widthMeasureSpec);
// 寬度的值(橫向精度條要給明確值,所以不需要判斷)
int widthVal = MeasureSpec.getSize(widthMeasureSpec);
int heigth = MeasureSpecHeigth(heightMeasureSpec);
setMeasuredDimension(widthVal, heigth);
// 實際繪畫的寬度
mRealWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
}

/** 高度測量 **/
private int MeasureSpecHeigth(int heightMeasureSpec) {
int result = 0;
int mode = MeasureSpec.getMode(heightMeasureSpec);
int size = MeasureSpec.getSize(heightMeasureSpec);
// EXACTLY 父結點要求其子節點的大小指定為某個確切的值。其子節點以及其他子孫結點都需要適應該大小。
if (mode == MeasureSpec.EXACTLY) {
result = size;
} else {
int textHeigth = (int) (mPaint.descent() - mPaint.ascent());
result = getPaddingTop()
+ getPaddingBottom()
+ Math.max(Math.max(mReachHeight, mUnReachHeight),
Math.abs(textHeigth));
// AT_MOST 父結點要求其子節點的大小不能超過某個最大值,其子節點以及其他子孫結點的大小都需要小於這個值
if (mode == MeasureSpec.AT_MOST) {
result = Math.min(result, size);
}
}

return result;
}

/** 畫控制項 **/
@Override
protected synchronized void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
// 消除鋸齒
canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG
| Paint.FILTER_BITMAP_FLAG));
// 設定畫布的原點
canvas.translate(getPaddingLeft(), getHeight() / 2);
// /draw reach
boolean noNeedUnreach = false;
String text = getProgress() + "%";
int textWidth = (int) mPaint.measureText(text);
float radio = getProgress() * 1.0f / getMax();
float progressX = radio * mRealWidth;
if (textWidth + progressX > mRealWidth) {
progressX = mRealWidth - textWidth;
noNeedUnreach = true;
}

float endX = progressX - mTextOffset / 2;
if (endX > 0) {
mPaint.setColor(mReachColor);
mPaint.setStrokeWidth(mReachHeight);
canvas.drawLine(0, 0, endX, 0, mPaint);
}

// draw text
mPaint.setColor(mTextColor);
int y = (int) (-(mPaint.descent() + mPaint.ascent()) / 2);
canvas.drawText(text, progressX, y, mPaint);
// draw unreach bar
if (!noNeedUnreach) {
float start = progressX + mTextOffset / 2 + textWidth;
mPaint.setColor(mUnReachColor);
mPaint.setStrokeWidth(mUnReachHeight);
canvas.drawLine(start, 0, mRealWidth, 0, mPaint);
}

canvas.restore();
}

/** dp轉px **/
protected int dp2px(int dpVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dpVal, getResources().getDisplayMetrics());
}

/** sp轉px **/
protected int sp2px(int spVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
spVal, getResources().getDisplayMetrics());
}
}

  MainActivity:

package com.Horizontal.horizontalprogressbar;

import com.Horizontal.view.ProgressbarView;
import com.Horizontal.view.RoundProgressbar;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {
private ProgressbarView progressbarView;
//private RoundProgressbar roundProgressbar;
private static final int MSG_UPDATE = 0X110;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressbarView = (ProgressbarView) findViewById(R.id.progress01);
//roundProgressbar=(RoundProgressbar) findViewById(R.id.progress02);
handler.sendEmptyMessage(MSG_UPDATE);
}

private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
int progress = progressbarView.getProgress();
//roundProgressbar.setProgress(++progress);
progressbarView.setProgress(++progress);
if (progress >= 100) {
handler.removeMessages(MSG_UPDATE);
}
handler.sendEmptyMessageDelayed(MSG_UPDATE, 100);
};
};

}

 源碼下載:連結: http://pan.baidu.com/s/1hrByCV6 密碼: whs7

自訂進度條(圓形、橫向進度條)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.