zoukankan      html  css  js  c++  java
  • 弹跳加载中自定义简单控件实现

    package com.loaderman.customviewdemo;
    
    import android.animation.Animator;
    import android.animation.ValueAnimator;
    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.animation.AccelerateDecelerateInterpolator;
    
    
    public class LoadingImageView extends android.support.v7.widget.AppCompatImageView {
        private int mTop;
        //当前动画图片索引
        private int mCurImgIndex = 0;
        //动画图片总张数
        private static int mImgCount = 3;
    
        public LoadingImageView(Context context) {
            super(context);
            init();
        }
    
        public LoadingImageView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        public LoadingImageView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init();
        }
    
        @Override
        protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
            super.onLayout(changed, left, top, right, bottom);
    
            mTop = top;
        }
    
        private void init() {
            ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 100, 0);
            valueAnimator.setRepeatMode(ValueAnimator.RESTART);
            valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
            valueAnimator.setDuration(2000);//动画时长
            valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
    
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {//监听动画值得实时变化
                public void onAnimationUpdate(ValueAnimator animation) {
                    Integer dx = (Integer) animation.getAnimatedValue();
                    setTop(mTop - dx);//将控件移动到当前位置,注意都是相对父控件坐标位置
                }
            });
    
            valueAnimator.addListener(new Animator.AnimatorListener() {
                public void onAnimationStart(Animator animation) {
                    setImageDrawable(getResources().getDrawable(R.drawable.pic_1));
                }
    
                public void onAnimationRepeat(Animator animation) {
                    mCurImgIndex++;
                    switch (mCurImgIndex % mImgCount) {
                        case 0:
                            setImageDrawable(getResources().getDrawable(R.drawable.pic_1));
                            break;
                        case 1:
                            setImageDrawable(getResources().getDrawable(R.drawable.pic_2));
                            break;
                        case 2:
                            setImageDrawable(getResources().getDrawable(R.drawable.pic_3));
                            break;
                    }
                }
    
                public void onAnimationEnd(Animator animation) {
    
                }
    
                public void onAnimationCancel(Animator animation) {
    
                }
            });
    
    
            valueAnimator.start();
        }
    }
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center"
        tools:context="com.loaderman.customviewdemo.MainActivity">
    
        <com.loaderman.customviewdemo.LoadingImageView
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:src="@drawable/pic_1"/>
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="加载中……"/>
    
    
    </LinearLayout>

    效果:

  • 相关阅读:
    Flink 电商实时数仓(二十三):ClickHouse基础(二)使用基础(2)ClickHouse 的安装(centos)
    Flink 电商实时数仓(二十二):ClickHouse基础(一)使用基础(1)ClickHouse 入门
    Flink 源码(二十六):Flink 内存管理(二)内存数据结构 、管理器
    Flink 源码(二十五):Flink 内存管理(一)内存模型与内存数据结构
    Flink 源码(二十四):Flink 任务调度机制(五)调度
    460. LFU Cache (solution 1)
    785. Is Graph Bipartite? (是否二分图)
    1318. Minimum Flips to Make a OR b Equal to c
    211. Add and Search Word
    188. Best Time to Buy and Sell Stock IV
  • 原文地址:https://www.cnblogs.com/loaderman/p/10195514.html
Copyright © 2011-2022 走看看