自定義加載View

效果圖:


效果圖

核心代碼如下:

public class LoadCircleView extends LinearLayout {

    private static final String TAG = "LoadCircleView";

    private Context mContext;


    public LoadCircleView(Context context) {
        this(context, null);
    }

    public LoadCircleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadCircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public LoadCircleView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        this.mContext = context;
        setOrientation(HORIZONTAL);//設置水平布局
        setGravity(Gravity.CENTER);//設置子View居中顯示
        CircleView circleView;
        LinearLayout.LayoutParams lp;
        for (int i = 0; i < 3; i++) {
            circleView = new CircleView(mContext);
            lp = new LayoutParams(40, 40);
            if (i != 0) {
                lp.leftMargin = 30;
            }
            addView(circleView, lp);
        }
        postDelayed(new Runnable() {
            @Override
            public void run() {
                startAnim(getChildAt(0));
            }
        },1000);
        postDelayed(new Runnable() {
            @Override
            public void run() {
                startAnim(getChildAt(1));
            }
        }, 1250);
        postDelayed(new Runnable() {
            @Override
            public void run() {
                startAnim(getChildAt(2));
            }
        }, 1500);
    }

//
//    private void startAnim(View view) {
//        ScaleAnimation scaleAnimation = new ScaleAnimation(1f, 0.6f, 1f, 0.6f,
//                ScaleAnimation.RELATIVE_TO_SELF, 0.5f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
//        int duration = 350;
//        if (childViewIndex % 2 == 0) {
//            duration = 300;
//        }
//        scaleAnimation.setDuration(duration);
//        childViewIndex++;
//        scaleAnimation.setInterpolator(new LinearInterpolator());
//        scaleAnimation.setRepeatCount(Integer.MAX_VALUE);
//        scaleAnimation.setRepeatMode(Animation.REVERSE);
//        view.startAnimation(scaleAnimation);
//    }

    private void startAnim(View view) {
        AnimatorSet animatorSet = new AnimatorSet();//組合動畫
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 1, 0.5f);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 1, 0.5f);
        scaleX.setRepeatCount(ValueAnimator.INFINITE);
        scaleX.setRepeatMode(ValueAnimator.REVERSE);
        scaleY.setRepeatCount(ValueAnimator.INFINITE);
        scaleY.setRepeatMode(ValueAnimator.REVERSE);

        animatorSet.setDuration(500);
        animatorSet.setInterpolator(new DecelerateInterpolator());
        animatorSet.play(scaleX).with(scaleY);//兩個動畫同時開始
        animatorSet.start();
    }

    /**
     * 靜態(tài)內部類,圓形View
     */
    private static class CircleView extends View {

        public CircleView(Context context) {
            super(context);
        }

        public CircleView(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
        }

        public CircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }

        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        public CircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            Paint paint = new Paint();
            Random random = new Random();
            int i = random.nextInt(3);
            paint.setColor(getContext().getResources().getColor(R.color.colorAccent));
            paint.setAntiAlias(true);//抗鋸齒
            paint.setStyle(Paint.Style.FILL);
            int height = getHeight();
            int width = getWidth();
            int radius = 0;
            if (height > width) {
                radius = width / 2;
            } else {
                radius = height / 2;
            }
            canvas.drawCircle(width / 2, height / 2, width / 2, paint);
        }
    }
}

這里默認添加了3個小圓圈往踢,有興趣的可以參考上述代碼修改

  1. 繼承線性布局,設置水平布局,居中排列
  2. 添加CircleView气堕,設置左邊距
  3. 以一定間隔時間依次開始動畫
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市畔咧,隨后出現(xiàn)的幾起案子茎芭,更是在濱河造成了極大的恐慌,老刑警劉巖誓沸,帶你破解...
    沈念sama閱讀 217,734評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件梅桩,死亡現(xiàn)場離奇詭異,居然都是意外死亡拜隧,警方通過查閱死者的電腦和手機宿百,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來洪添,“玉大人垦页,你說我怎么就攤上這事「缮荩” “怎么了痊焊?”我有些...
    開封第一講書人閱讀 164,133評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我薄啥,道長辕羽,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,532評論 1 293
  • 正文 為了忘掉前任罪佳,我火速辦了婚禮逛漫,結果婚禮上,老公的妹妹穿的比我還像新娘赘艳。我一直安慰自己酌毡,他們只是感情好,可當我...
    茶點故事閱讀 67,585評論 6 392
  • 文/花漫 我一把揭開白布蕾管。 她就那樣靜靜地躺著枷踏,像睡著了一般。 火紅的嫁衣襯著肌膚如雪掰曾。 梳的紋絲不亂的頭發(fā)上旭蠕,一...
    開封第一講書人閱讀 51,462評論 1 302
  • 那天,我揣著相機與錄音旷坦,去河邊找鬼掏熬。 笑死,一個胖子當著我的面吹牛秒梅,可吹牛的內容都是我干的旗芬。 我是一名探鬼主播,決...
    沈念sama閱讀 40,262評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼捆蜀,長吁一口氣:“原來是場噩夢啊……” “哼疮丛!你這毒婦竟也來了?” 一聲冷哼從身側響起辆它,我...
    開封第一講書人閱讀 39,153評論 0 276
  • 序言:老撾萬榮一對情侶失蹤誊薄,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后锰茉,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體呢蔫,經...
    沈念sama閱讀 45,587評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,792評論 3 336
  • 正文 我和宋清朗相戀三年洞辣,在試婚紗的時候發(fā)現(xiàn)自己被綠了咐刨。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,919評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡扬霜,死狀恐怖定鸟,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情著瓶,我是刑警寧澤联予,帶...
    沈念sama閱讀 35,635評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響沸久,放射性物質發(fā)生泄漏季眷。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,237評論 3 329
  • 文/蒙蒙 一卷胯、第九天 我趴在偏房一處隱蔽的房頂上張望子刮。 院中可真熱鬧,春花似錦窑睁、人聲如沸挺峡。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽橱赠。三九已至,卻和暖如春箫津,著一層夾襖步出監(jiān)牢的瞬間狭姨,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評論 1 269
  • 我被黑心中介騙來泰國打工苏遥, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留饼拍,地道東北人。 一個月前我還...
    沈念sama閱讀 48,048評論 3 370
  • 正文 我出身青樓田炭,卻偏偏與公主長得像惕耕,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子诫肠,可洞房花燭夜當晚...
    茶點故事閱讀 44,864評論 2 354