600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > 精通Android自定义View(十八)自定义圆形菊花加载转圈效果

精通Android自定义View(十八)自定义圆形菊花加载转圈效果

时间:2019-06-17 21:02:08

相关推荐

精通Android自定义View(十八)自定义圆形菊花加载转圈效果

1 效果:

2 源码:

public class LoadingView extends View {public LoadingView(Context context) {this(context, null);}public LoadingView(Context context, AttributeSet attrs) {this(context, attrs, 0);}public LoadingView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);//对画笔的设置mPaint = new Paint();mPaint.setAntiAlias(true);mPaint.setStrokeJoin(Paint.Join.ROUND);mPaint.setStrokeCap(Paint.Cap.ROUND);mPaint.setStyle(Paint.Style.FILL);mPaint.setStrokeWidth(TypedValue.applyDimension(PLEX_UNIT_DIP, 3, getResources().getDisplayMetrics()));mPaint.setColor(Color.WHITE);}private Paint mPaint;//默认view的宽度private int mDefaultWidth = dp2px(100);private int mDefaultHeight =mDefaultWidth;private int mDefaultPadding = dp2px(10);//绘制进度条的实际宽度private int mMeasureHeight;private int mMeasureWidth;//绘制直线的索引private int mCurrentIndex = 0;//圆圈组成线段的个数private int count = 12;private boolean mIsSart=false;Timer mTimer;TimerTask mTimerTask;@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);//测量计算宽度int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);if (widthSpecMode == MeasureSpec.EXACTLY) {//当specMode = EXACTLY时,精确值模式,即当我们在布局文件中为View指定了具体的大小mMeasureWidth = widthSpecSize;} else {//指定默认大小mMeasureWidth = mDefaultWidth;if (widthSpecMode == MeasureSpec.AT_MOST) {mMeasureWidth = Math.min(mMeasureWidth, widthSpecSize);}}//测量计算View的高int heightSpecMode = MeasureSpec.getMode(widthMeasureSpec);int heightSpecSize = MeasureSpec.getSize(widthMeasureSpec);if (heightSpecMode == MeasureSpec.EXACTLY) {//当specMode = EXACTLY时,精确值模式,即当我们在布局文件中为View指定了具体的大小mMeasureHeight = heightSpecSize;} else {//指定默认大小mMeasureHeight = mDefaultHeight;if (heightSpecMode == MeasureSpec.AT_MOST) {mMeasureHeight = Math.min(mMeasureHeight, heightSpecSize);}}mMeasureHeight=mMeasureHeight-getPaddingBottom()-getPaddingTop();mMeasureWidth=mMeasureWidth-getPaddingLeft()-getPaddingBottom();//重新测量setMeasuredDimension(mMeasureWidth, mMeasureHeight);}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);canvas.save();//将画面平移到View的中心canvas.translate(mMeasureWidth/2,mMeasureHeight/2);if (mCurrentIndex >= count) {mCurrentIndex = 0;}int endAlpha = 255 / count;for (int i = 0; i < count; i++) {int alpha;//计算将要绘制直线的颜色透明度if (mCurrentIndex - i > 0) {alpha = endAlpha * (mCurrentIndex - i);} else {alpha = 255 - 255 / count * (i - mCurrentIndex);}mPaint.setColor(Color.argb(alpha, 255, 255, 255));//绘制直线canvas.drawLine(-mMeasureWidth / 5, 0, -mMeasureWidth / 11, 0, mPaint);//旋转画布canvas.rotate(360 / count, 0, 0);}mCurrentIndex++;canvas.restore();}//将设置的db转为屏幕像素protected int dp2px(int dpVal) {return (int) TypedValue.applyDimension(PLEX_UNIT_DIP,dpVal, getResources().getDisplayMetrics());}//开始旋转public void start(){mTimer = new Timer();mIsSart=true;mTimerTask = new TimerTask() {@Overridepublic void run() {try {postInvalidate();} catch (Exception e) {e.printStackTrace();}}};mTimer.schedule(mTimerTask,0,100);}//停止旋转public void stop(){mIsSart=false;mTimerTask.cancel();mTimer.cancel();}//是否开始旋转public boolean isStart() {return mIsSart;}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。