Android 水波纹扩散效果

项目地址下载:https://github.com/LiuJunb/RippleImageView

1.效果图:

2.使用方法:

  1. 在xml里使用RippleImageView自定义控件:

    xmlns:app="http://schemas.android.com/apk/res-auto"
    
    
    
    
  2. 在Activity中的使用:

    rippleImageView=(RippleImageView)findViewById(R.id.rippleImageView);
    //开始动画
    rippleImageView.startWaveAnimation();
    
    //停止动画
    rippleImageView.stopWaveAnimation();
    

3.如何将自定义控件引入到项目:

  1. 拷贝自定义控件RippleImageView

    /**
     * Description :
     * Author : liujun
     * Email  : [email protected]
     */
    public class RippleImageView extends RelativeLayout {
        private static final int SHOW_SPACING_TIME=700;
        private static final int MSG_WAVE2_ANIMATION = 1;
        private static final int MSG_WAVE3_ANIMATION = 2;
        private static final int IMAMGEVIEW_SIZE = 80;
        /**三张波纹图片*/
        private static final int SIZE =3 ;
    
        /**动画默认循环播放时间*/
        private  int show_spacing_time=SHOW_SPACING_TIME;
        /**初始化动画集*/
        private AnimationSet [] mAnimationSet=new AnimationSet[SIZE];
        /**水波纹图片*/
        private ImageView [] imgs=new ImageView[SIZE];
        /**背景图片*/
        private ImageView img_bg;
        /**水波纹和背景图片的大小*/
        private float imageViewWidth=IMAMGEVIEW_SIZE;
        private float imageViewHeigth=IMAMGEVIEW_SIZE;
    
        private Handler mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case MSG_WAVE2_ANIMATION:
                        imgs[MSG_WAVE2_ANIMATION].startAnimation(mAnimationSet[MSG_WAVE2_ANIMATION]);
                        break;
                    case MSG_WAVE3_ANIMATION:
                        imgs[MSG_WAVE2_ANIMATION].startAnimation(mAnimationSet[MSG_WAVE3_ANIMATION]);
                        break;
                }
    
            }
        };
    
        public RippleImageView(Context context) {
            super(context);
            initView(context);
        }
    
        public RippleImageView(Context context, AttributeSet attrs) {
            super(context, attrs);
            getAttributeSet(context,attrs);
            initView(context);
        }
    
        /**
         * 获取xml属性
         * @param context
         * @param attrs
         */
        private void getAttributeSet(Context context, AttributeSet attrs) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.custume_ripple_imageview);
    
            show_spacing_time = typedArray.getInt(R.styleable.custume_ripple_imageview_show_spacing_time, SHOW_SPACING_TIME);
            imageViewWidth = typedArray.getDimension(R.styleable.custume_ripple_imageview_imageViewWidth, IMAMGEVIEW_SIZE);
            imageViewHeigth = typedArray.getDimension(R.styleable.custume_ripple_imageview_imageViewHeigth, IMAMGEVIEW_SIZE);
            Log.d("TAG","show_spacing_time="+show_spacing_time+"mm imageViewWidth="+imageViewWidth+"px  imageViewHeigth="+imageViewHeigth+"px");
            typedArray.recycle();
        }
        private void initView(Context context) {
            setLayout(context);
            for (int i = 0; i 

    }

  2. 拷贝自定义属性到arrts下

    
        
        
        
    
    
  3. 拷贝默认图片

你可能感兴趣的:(Android)