中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

android仿直播圓點加載效果

發布時間:2020-10-22 15:21:38 來源:腳本之家 閱讀:147 作者:zhoushenxian 欄目:移動開發

今天實現一個很多app中使用到的加載進度條的效果,可能我們平時數據加載都使用到的是系統自帶的,但是也有很多app加載進度條的效果實現挺好看,就是三個點不停的水平跑而且是變換顏色的,其實這個效果很簡單,

分析:

第一步:

android仿直播圓點加載效果

第二步:

android仿直播圓點加載效果

為了圓的顏色 大小,以及移動的距離都對外開放,采用了自定義屬性的方式,當然也可以進行設置,

分析了后 代碼就直接上了,

<?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;
  }
}

效果圖:

android仿直播圓點加載效果

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

镇雄县| 湘潭市| 阿城市| 谢通门县| 淮北市| 民权县| 镇赉县| 上饶市| 广东省| 兰西县| 秦安县| 翁源县| 海淀区| 平度市| 江油市| 微博| 随州市| 桃园市| 高清| 耒阳市| 会理县| 榆中县| 桑植县| 思南县| 金秀| 扶余县| 枝江市| 六盘水市| 葫芦岛市| 怀仁县| 广灵县| 三都| 大同市| 闸北区| 丰宁| 顺义区| 永仁县| 大邑县| 兴城市| 犍为县| 徐州市|