我也是个懒人党,做到跑步结束按钮时需要一个长按事件,网上一搜东西一大堆但是没有我想要的效果,没办法只好自己写一个,希望给大家带来方便
/**自定义的一个view*/
public class TasksCompletedView extends View { // 画实心圆的画笔
private Paint mCirclePaint;
// 画圆环的画笔
private Paint mRingPaint;
private Paint nRingPaint;
// 画字体的画笔
private Paint mTextPaint;
// 圆形颜色
private int mCircleColor;
// 圆环颜色
private int mRingColor;
private int nRingColor;
// 半径
private float mRadius;
// 圆环半径
private float mRingRadius;
// 圆环宽度
private float mStrokeWidth;
// 圆心x坐标
private int mXCenter;
// 圆心y坐标
private int mYCenter;
// 字的长度
private float mTxtWidth;
// 字的高度
private float mTxtHeight;
// 总进度
private int mTotalProgress = 100;
// 当前进度
private int mProgress;
Bitmap bitmap = null;
int imgWidth = 0;
int ingHight = 0;
public TasksCompletedView(Context context, AttributeSet attrs) {
super(context, attrs);
// 获取自定义的属性
bitmap = BitmapFactory.decodeResource(context.getResources(),
R.drawable.but_end_sports);
imgWidth = bitmap.getWidth();
ingHight = bitmap.getHeight();
initAttrs(context, attrs);
initVariable();
}
private void initAttrs(Context context, AttributeSet attrs) {
TypedArray typeArray = context.getTheme().obtainStyledAttributes(attrs,
R.styleable.TasksCompletedView, 0, 0);
// mRadius =
// typeArray.getDimension(R.styleable.TasksCompletedView_radius,
// 80);
mRadius = imgWidth / 2;
mStrokeWidth = typeArray.getDimension(
R.styleable.TasksCompletedView_strokeWidth, 10);
mCircleColor = 0xffffffff;
// typeArray.getColor(
// R.styleable.TasksCompletedView_circleColor, 0xFFFFFFFF);
mRingColor = typeArray.getColor(
R.styleable.TasksCompletedView_ringColor, 0xFFFFFFFF);
nRingColor = 0xffff4100;
mRingRadius = mRadius + mStrokeWidth / 2;
}
private void initVariable() {
mCirclePaint = new Paint();
mCirclePaint.setAntiAlias(true);
mCirclePaint.setColor(mCircleColor);
mCirclePaint.setStyle(Paint.Style.FILL);
mRingPaint = new Paint();
mRingPaint.setAntiAlias(true);
mRingPaint.setColor(mRingColor);
mRingPaint.setStyle(Paint.Style.STROKE);
mRingPaint.setStrokeWidth(mStrokeWidth);
nRingPaint = new Paint();
nRingPaint.setAntiAlias(true);
nRingPaint.setColor(mRingColor);
nRingPaint.setStyle(Paint.Style.STROKE);
nRingPaint.setStrokeWidth(mStrokeWidth);
mTextPaint = new Paint();
mTextPaint.setAntiAlias(true);
mTextPaint.setStyle(Paint.Style.FILL);
mTextPaint.setARGB(255, 255, 255, 255);
mTextPaint.setTextSize(mRadius / 2);
FontMetrics fm = mTextPaint.getFontMetrics();
mTxtHeight = (int) Math.ceil(fm.descent - fm.ascent);
}
@Override
protected void onDraw(Canvas canvas) {
mXCenter = getWidth() / 2;
mYCenter = getHeight() / 2;
canvas.drawCircle(mXCenter, mYCenter, mRadius, mCirclePaint);
if (mProgress > 0) {
RectF oval = new RectF();
oval.left = (mXCenter - mRingRadius);
oval.top = (mYCenter - mRingRadius);
oval.right = mRingRadius * 2 + (mXCenter - mRingRadius);
oval.bottom = mRingRadius * 2 + (mYCenter - mRingRadius);
canvas.drawArc(oval, -90,
((float) mProgress / mTotalProgress) * 360, false,
mRingPaint); //
// canvas.drawCircle(mXCenter, mYCenter, mRadius + mStrokeWidth / 2,
// mRingPaint);
// String txt = mProgress + "%";
// mTxtWidth = mTextPaint.measureText(txt, 0, txt.length());
// canvas.drawText(txt, mXCenter - mTxtWidth / 2, mYCenter
// + mTxtHeight / 4, mTextPaint);
canvas.drawBitmap(bitmap, mXCenter - imgWidth / 2, mYCenter
- ingHight / 2, mTextPaint);
} else {
RectF oval = new RectF();
oval.left = (mXCenter - mRingRadius);
oval.top = (mYCenter - mRingRadius);
oval.right = mRingRadius * 2 + (mXCenter - mRingRadius);
oval.bottom = mRingRadius * 2 + (mYCenter - mRingRadius);
canvas.drawArc(oval, 0, 0, false, nRingPaint); //
canvas.drawBitmap(bitmap, mXCenter - imgWidth / 2, mYCenter
- ingHight / 2, mTextPaint);
}
}
public void setProgress(int progress) {
mProgress = progress;
// invalidate();
postInvalidate();
}
}
在attrs 中定义view中需要的属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TasksCompletedView">
<attr name="radius" format="dimension"/>
<attr name="strokeWidth" format="dimension"/>
<attr name="circleColor" format="color"/>
<attr name="ringColor" format="color"/>
</declare-styleable>
</resources>
在activity的man.xml中
<com.test.demo.TasksCompletedView
android:id="@+id/tasks_view"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_gravity="center"
tc:circleColor="@color/circle_color"
tc:radius="100dp"
tc:ringColor="@color/ring_color"
tc:strokeWidth="6dp" />
最后activity中
public class MainActivity extends Activity {
private TasksCompletedView mTasksView;
private int mTotalProgress;
private int mCurrentProgress;
private Handler mHandler = new Handler();
private boolean isClick;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initVariable();
initView();
// new Thread(new ProgressRunable()).start();
}
private void initVariable() {
mTotalProgress = 100;
mCurrentProgress = 0;
}
private void initView() {
mTasksView = (TasksCompletedView) findViewById(R.id.tasks_view);
mTasksView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()) {// 返回true的话,单击事件、长按事件不可以被触发
case MotionEvent.ACTION_DOWN:
isClick = true;
mCurrentProgress = 0;
startplay();
return true;
case MotionEvent.ACTION_UP:
isClick = false;
break;
default:
break;
}
return false;
}
});
}
private void startplay() {
mHandler.post(new Runnable() {
@Override
public void run() {
if (mCurrentProgress < mTotalProgress) {
if (isClick) {// 一直长按
mCurrentProgress += 1;
mHandler.postDelayed(this, 50);
mTasksView.setProgress(mCurrentProgress);
} else {// 中途放弃长按
if (mCurrentProgress >= 50) {// 进度超过50%直接走到100%,
mCurrentProgress += 1;
mHandler.postDelayed(this, 10);
mTasksView.setProgress(mCurrentProgress);
} else {// 进度没到50%重置为0
mCurrentProgress = 0;
mTasksView.setProgress(mCurrentProgress);
}
}
}
}
});
}
}
自己使用了下还算不错分享给大家