android:滑动挂断自定义View的简单实现

要点:

随着手指的滑动更新位置

drawText的时候,,如何计算开始的位置,使str居中

1.CallSliderEndView.java

package net.mobctrl.callendview;import android.annotation.SuppressLint;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Paint.Style;import android.graphics.RectF;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;/** * @date 2015年2月2日 下午11:02:00 * @author Zheng Haibo * @Description: 滑动挂断的自定义控件 * @Web */@SuppressLint({ “DrawAllocation”, “ClickableViewAccessibility” }){{();}private SliderEndListener sliderEndListener;(SliderEndListener sliderEndListener) {this.sliderEndListener = sliderEndListener;}private int height;private int width;private int circleOffset = 0;private int prevX = 0;private int maxOffset;private String sliderText;private float textSize;private int progressBackgroundColor;private int backgroundColor;private int redReginWidth;public CallSliderEndView(Context context) {super(context);init(context, null);}public CallSliderEndView(Context context, AttributeSet attrs) {super(context, attrs);init(context, attrs);}public CallSliderEndView(Context context, AttributeSet attrs,int defStyleAttr) {super(context, attrs, defStyleAttr);init(context, attrs);}(Context context, AttributeSet attrs) {if (null == attrs) {return;}TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.CallSliderEndView);textSize = typedArray.getDimensionPixelSize(R.styleable.CallSliderEndView_textSize, 40);sliderText = typedArray.getString(R.styleable.CallSliderEndView_text);progressBackgroundColor = typedArray.getColor(R.styleable.CallSliderEndView_progressBackgroundColor,Color.GREEN);backgroundColor = typedArray.getColor(R.styleable.CallSliderEndView_backgroundColor, 0x0fffffff);typedArray.recycle();}(Canvas canvas) {super.onDraw(canvas);// TODOheight = getHeight();width = getWidth();// 绘制背景Paint paint = new Paint();paint.setStyle(Style.FILL);paint.setAntiAlias(true);paint.setColor(backgroundColor);drawBackground(canvas, paint);// drawCircleButton(canvas, paint);drawRoundButton(canvas, paint);}(Canvas canvas, Paint paint) {canvas.drawRoundRect(new RectF(0, 0, width, height), height / 2,height / 2, paint);}(Canvas canvas, Paint paint) {int circleMargin = height / 10;paint.setColor(Color.RED);canvas.drawCircle(height / 2 + circleOffset, height / 2, height / 2- circleMargin, paint);}(Canvas canvas, Paint paint) {redReginWidth = width / 2;// 绘制进度背景paint.setColor(progressBackgroundColor);canvas.drawRoundRect(new RectF(circleOffset, 0, circleOffset+ redReginWidth, height), height / 2, height / 2, paint);// 将文本sliderText显示在中间paint.setTextSize(textSize);paint.setColor(Color.WHITE);int yCenterPos = (int) ((canvas.getHeight() / 2) – ((paint.descent() + paint.ascent()) / 2));// 计算在Yint startX = circleOffset+ (redReginWidth – (int) paint.measureText(sliderText, 0,sliderText.length())) / 2;canvas.drawText(sliderText, startX, yCenterPos, paint);}(MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:actionDown(event);break;case MotionEvent.ACTION_MOVE:actionMove(event);break;case MotionEvent.ACTION_UP:actionUp(event);break;}return true;}(MotionEvent event) {if (this.circleOffset != maxOffset) {this.circleOffset = 0;}postInvalidate();}(MotionEvent event) {int tempOffset = (int) (event.getX() – this.prevX);this.maxOffset = width – redReginWidth;if (tempOffset >= maxOffset && this.circleOffset == maxOffset) {return;}this.circleOffset = tempOffset;if (this.circleOffset > maxOffset) {// 是否已经滑动到边缘this.circleOffset = maxOffset;if (sliderEndListener != null) {sliderEndListener.onSliderEnd();}}if (this.circleOffset <= 0) {this.circleOffset = 0;}postInvalidate();}(MotionEvent e) {this.prevX = (int) e.getX();}}比谁都感激这份“不能说出的爱”。

android:滑动挂断自定义View的简单实现

相关文章:

你感兴趣的文章:

标签云: