1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > android Launcher 自定义View 高仿hola一键清理效果

android Launcher 自定义View 高仿hola一键清理效果

时间:2023-10-07 19:37:41

相关推荐

android Launcher 自定义View 高仿hola一键清理效果

android Launcher 自定义View 高仿hola一键清理效果

package com.example.administrator.cleanviewactivity;import android.animation.Animator;import android.animation.ValueAnimator;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.RectF;import android.util.AttributeSet;import android.view.View;import android.view.animation.DecelerateInterpolator;/*** ferris* 10月17日 14:37:02* TODO: 一键清理View*/public class CleanProgressView extends View {private final float mStartAngle = -90;private float mSweepAngle = 0;private float mMaxSweepAngle = 360;private int mAnimationDuration = 400;private int mMaxProgress = 100;private Paint mPaint;private RectF outerRect;private int outerWidth,middleWidth,progressWidth;public CleanProgressView(Context context) {super(context);init(null, 0);}public CleanProgressView(Context context, AttributeSet attrs) {super(context, attrs);init(attrs, 0);}public CleanProgressView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);init(attrs, defStyle);}private void init(AttributeSet attrs, int defStyle) {// Load attributes// final TypedArray a = getContext().obtainStyledAttributes(//attrs, R.styleable.CleanProgressView, defStyle, 0);//// mExampleString = a.getString(//R.styleable.CleanProgressView_exampleString);// mExampleColor = a.getColor(//R.styleable.CleanProgressView_exampleColor,//mExampleColor);// // Use getDimensionPixelSize or getDimensionPixelOffset when dealing with// // values that should fall on pixel boundaries.// mExampleDimension = a.getDimension(//R.styleable.CleanProgressView_exampleDimension,//mExampleDimension);//// if (a.hasValue(R.styleable.CleanProgressView_exampleDrawable)) {// mExampleDrawable = a.getDrawable(//R.styleable.CleanProgressView_exampleDrawable);// mExampleDrawable.setCallback(this);// }//// a.recycle();}private void initClean(int width,int hight){int cleanSize = Math.min(width, hight);outerWidth=(cleanSize-getPaddingLeft()-getPaddingRight())/3;middleWidth=outerWidth/3*2;progressWidth=middleWidth/2;if(outerRect==null){outerRect=new RectF();}outerRect.set(getPaddingLeft(), getPaddingTop(), cleanSize - getPaddingRight(), cleanSize - getPaddingBottom());}private Paint getBasePaint(){mPaint=new Paint();mPaint.setStyle(Paint.Style.STROKE);mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);mPaint.setAntiAlias(true);mPaint.setStyle(Paint.Style.STROKE);mPaint.setStrokeCap(Paint.Cap.ROUND);return mPaint;}private Paint getOuterPaint(){if(mPaint==null){mPaint=getBasePaint();}mPaint.setColor(Color.BLACK);mPaint. setStrokeWidth(outerWidth);mPaint.setAlpha(30);return mPaint;}private Paint getMiddlePaint(){if(mPaint==null){mPaint=getBasePaint();}mPaint.setColor(Color.BLACK);mPaint. setStrokeWidth(middleWidth);mPaint.setAlpha(100);return mPaint;}private Paint getProgressPaint(){if(mPaint==null){mPaint=getBasePaint();}mPaint.setColor(Color.GREEN);mPaint. setStrokeWidth(progressWidth);mPaint.setAlpha(200);return mPaint;}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);canvas.drawArc(outerRect, 0, 360, false, getOuterPaint());canvas.drawArc(outerRect, 0, 360, false, getMiddlePaint());canvas.drawArc(outerRect, mStartAngle, mSweepAngle, false, getProgressPaint());}private float calcSweepAngleFromProgress(int progress) {return (mMaxSweepAngle / mMaxProgress) * progress;}private int calcProgressFromSweepAngle(float sweepAngle) {return (int) ((sweepAngle * mMaxProgress) / mMaxSweepAngle);}//直接设置进度信息public void setFastProgress(int progress) {mSweepAngle = calcSweepAngleFromProgress(progress);invalidate();}public void setProgress(int progress) {ValueAnimator animator = ValueAnimator.ofFloat(mSweepAngle,calcSweepAngleFromProgress(progress));animator.setInterpolator(new DecelerateInterpolator());animator.setDuration(mAnimationDuration);animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator valueAnimator) {mSweepAngle = (Float) valueAnimator.getAnimatedValue();invalidate();}});animator.addListener(new Animator.AnimatorListener() {@Overridepublic void onAnimationStart(Animator animation) {// TODO Auto-generated method stub}@Overridepublic void onAnimationRepeat(Animator animation) {// TODO Auto-generated method stub}@Overridepublic void onAnimationEnd(Animator animation) {// TODO Auto-generated method stub}@Overridepublic void onAnimationCancel(Animator animation) {// TODO Auto-generated method stub}});animator.start();}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {super.onSizeChanged(w, h, oldw, oldh);initClean(w,h);}}

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