今天實(shí)現(xiàn)一個(gè)很多app中使用到的加載進(jìn)度條的效果,可能我們平時(shí)數(shù)據(jù)加載都使用到的是系統(tǒng)自帶的,但是也有很多app加載進(jìn)度條的效果實(shí)現(xiàn)挺好看,就是三個(gè)點(diǎn)不停的水平跑而且是變換顏色的,其實(shí)這個(gè)效果很簡單,
創(chuàng)新互聯(lián)建站專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、宣恩網(wǎng)絡(luò)推廣、小程序定制開發(fā)、宣恩網(wǎng)絡(luò)營銷、宣恩企業(yè)策劃、宣恩品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)建站為所有大學(xué)生創(chuàng)業(yè)者提供宣恩建站搭建服務(wù),24小時(shí)服務(wù)熱線:13518219792,官方網(wǎng)址:www.cdcxhl.com
分析:
第一步:
第二步:
為了圓的顏色 大小,以及移動(dòng)的距離都對(duì)外開放,采用了自定義屬性的方式,當(dāng)然也可以進(jìn)行設(shè)置,
分析了后 代碼就直接上了,
<?xml version="1.0" encoding="utf-8"?>
這是自定義屬性,左,中,右顏色,圓的半徑,以及圓x軸移動(dòng)的距離
CircleItemPointView.java
是每個(gè)子view的代碼,很簡單就是畫了圓,然后設(shè)置了下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(); } /** * 獲取當(dāng)前的顏色 */ 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); } /** * 展開動(dòng)畫 */ 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(); } /** * 開啟動(dòng)畫 */ public void startLoad(){ if(spreadAnimation==null){ spreadAnimation(); } } /** * 停止動(dòng)畫 */ public void stopLoad(){ clearAnimation(); leftView.setVisibility(View.GONE); middleView.setVisibility(View.GONE); rightView.setVisibility(View.GONE); } /** * 設(shè)置左側(cè)顏色值 * @param leftColor */ public void setLeftColor(int leftColor) { this.leftColor = leftColor; } /** * 設(shè)置中間顏色值 * @param middleColor */ public void setMiddleColor(int middleColor) { this.middleColor = middleColor; } /** * 設(shè)置右側(cè)顏色值 * @param rightColor */ public void setRightColor(int rightColor) { this.rightColor = rightColor; } /** * 設(shè)置圓的半徑 * @param radius */ public void setRadius(int radius) { this.radius = radius; } /** * 設(shè)置動(dòng)畫x軸方向移動(dòng)的距離 * @param translationDistance */ public void setTranslationDistance(int translationDistance) { translationDistance = translationDistance; } }
效果圖:
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。