本文讲述了Android实现水波纹扩散效果的实例代码。分享给大家供大家参考,具体如下:
项目地址下载
1.效果图:
2.使用方法:
在xml里使用RippleImageView自定义控件:
xmlns:app="http://schemas.android.com/apk/res-auto"
在Activity中的使用:
rippleImageView=(RippleImageView)findViewById(R.id.rippleImageView); //开始动画 rippleImageView.startWaveAnimation(); //停止动画 rippleImageView.stopWaveAnimation();
3.如何将自定义控件引入到项目:
拷贝自定义控件RippleImageView
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);
}
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
拷贝自定义属性到arrts下
拷贝默认图片



