您好,登錄后才能下訂單哦!
今天實現一個很多app中使用到的加載進度條的效果,可能我們平時數據加載都使用到的是系統自帶的,但是也有很多app加載進度條的效果實現挺好看,就是三個點不停的水平跑而且是變換顏色的,其實這個效果很簡單,
分析:
第一步:
第二步:
為了圓的顏色 大小,以及移動的距離都對外開放,采用了自定義屬性的方式,當然也可以進行設置,
分析了后 代碼就直接上了,
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="CirclePointLoadView"> <attr name="leftPointColor" format="color|reference"></attr> <attr name="middlePointColor" format="color|reference"></attr> <attr name="rightPointColor" format="color|reference"></attr> <attr name="radius" format="integer|reference"></attr> <attr name="translationDistance" format="dimension|reference"></attr> </declare-styleable> </resources>
這是自定義屬性,左,中,右顏色,圓的半徑,以及圓x軸移動的距離
CircleItemPointView.java
是每個子view的代碼,很簡單就是畫了圓,然后設置了下paint的顏色而已
package com.circle.load; import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.View; /** * Created by zhouguizhijxhz on 2018/5/29. */ public class CircleItemPointView extends View { private Paint mPaint; private int mColor; public CircleItemPointView(Context context) { this(context,null); } public CircleItemPointView(Context context, @Nullable AttributeSet attrs) { this(context, attrs,0); } public CircleItemPointView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setDither(true); } @Override protected void onDraw(Canvas canvas) { int cx = getWidth()/2; int cy = getHeight()/2; canvas.drawCircle(cx,cy,cx,mPaint); } /** * 切換顏色 */ public void changeColor(int color){ mColor = color; mPaint.setColor(color); invalidate(); } public void setColor(int color){ mColor = color; mPaint.setColor(color); invalidate(); } /** * 獲取當前的顏色 */ public int getColor() { return mColor; } }
CirclePointLoadView.java
package com.circle.load; import android.animation.Animator; import android.animation.AnimatorListenerAdapter; import android.animation.AnimatorSet; import android.animation.ObjectAnimator; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Color; import android.util.AttributeSet; import android.view.View; import android.view.animation.AccelerateInterpolator; import android.widget.RelativeLayout; /** * Created by zhouguizhijxhz on 2018/5/29. */ public class CirclePointLoadView extends RelativeLayout{ private int defaultLeftColor = Color.parseColor("#FF8247"); private int defaultMiddleColor = Color.parseColor("#EE4000"); private int defaultRightColor = Color.parseColor("#CD2626"); private int leftColor; private int middleColor; private int rightColor; private int defaultCircleRadius = 10; private int defaultTranslationDistance = 36; private int translationDistance; private static final long ANIMATION_DURATION = 400; private int radius; private CircleItemPointView leftView; private CircleItemPointView middleView; private CircleItemPointView rightView; private AnimatorSet spreadAnimation; private AnimatorSet closedAnimation; public CirclePointLoadView(Context context) { this(context,null); } public CirclePointLoadView(Context context, AttributeSet attrs) { this(context, attrs,0); } public CirclePointLoadView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); parseAttrs(context,attrs); addViewToLayout(context); } private void addViewToLayout(Context context) { if(null==context){ return; } leftView = createView(context); leftView.setColor(leftColor); middleView = createView(context); middleView.setColor(middleColor); rightView = createView(context); rightView.setColor(rightColor); addView(leftView); addView(rightView); addView(middleView); } /** * 展開動畫 */ private void spreadAnimation() { ObjectAnimator leftTranslationAnimator = ObjectAnimator.ofFloat(leftView,"translationX",0,-translationDistance); ObjectAnimator rightTranslationAnimator = ObjectAnimator.ofFloat(rightView,"translationX",0,translationDistance); spreadAnimation = new AnimatorSet(); spreadAnimation.setDuration(ANIMATION_DURATION); spreadAnimation.playTogether(leftTranslationAnimator,rightTranslationAnimator); spreadAnimation.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { closedAnimation(); } }); spreadAnimation.start(); } private void closedAnimation() { ObjectAnimator leftTranslationAnimator = ObjectAnimator.ofFloat(leftView,"translationX",-translationDistance,0); ObjectAnimator rightTranslationAnimator = ObjectAnimator.ofFloat(rightView,"translationX",translationDistance,0); closedAnimation = new AnimatorSet(); closedAnimation.setInterpolator(new AccelerateInterpolator()); closedAnimation.setDuration(ANIMATION_DURATION); closedAnimation.playTogether(leftTranslationAnimator,rightTranslationAnimator); closedAnimation.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { int leftColor = leftView.getColor(); int rightColor = rightView.getColor(); int middleColor = middleView.getColor(); middleView.changeColor(leftColor); rightView.changeColor(middleColor); leftView.changeColor(rightColor); spreadAnimation(); } }); closedAnimation.start(); } public CircleItemPointView createView(Context context) { CircleItemPointView itemPointView = new CircleItemPointView(context); LayoutParams params = new LayoutParams(DensityUtils.dp2px(context,radius),DensityUtils.dp2px(context,radius)); params.addRule(CENTER_IN_PARENT); itemPointView.setLayoutParams(params); return itemPointView; } private void parseAttrs(Context context, AttributeSet attrs) { if(null==context||null==attrs){ return; } TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.CirclePointLoadView); leftColor = typedArray.getColor(R.styleable.CirclePointLoadView_leftPointColor,defaultLeftColor); middleColor = typedArray.getColor(R.styleable.CirclePointLoadView_middlePointColor,defaultMiddleColor); rightColor = typedArray.getColor(R.styleable.CirclePointLoadView_rightPointColor,defaultRightColor); radius = typedArray.getInteger(R.styleable.CirclePointLoadView_radius,defaultCircleRadius); translationDistance = (int) typedArray.getDimension(R.styleable.CirclePointLoadView_translationDistance,defaultTranslationDistance); translationDistance = DensityUtils.dp2px(context,translationDistance); typedArray.recycle(); } /** * 開啟動畫 */ public void startLoad(){ if(spreadAnimation==null){ spreadAnimation(); } } /** * 停止動畫 */ public void stopLoad(){ clearAnimation(); leftView.setVisibility(View.GONE); middleView.setVisibility(View.GONE); rightView.setVisibility(View.GONE); } /** * 設置左側顏色值 * @param leftColor */ public void setLeftColor(int leftColor) { this.leftColor = leftColor; } /** * 設置中間顏色值 * @param middleColor */ public void setMiddleColor(int middleColor) { this.middleColor = middleColor; } /** * 設置右側顏色值 * @param rightColor */ public void setRightColor(int rightColor) { this.rightColor = rightColor; } /** * 設置圓的半徑 * @param radius */ public void setRadius(int radius) { this.radius = radius; } /** * 設置動畫x軸方向移動的距離 * @param translationDistance */ public void setTranslationDistance(int translationDistance) { translationDistance = translationDistance; } }
效果圖:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。