效果展示先:
XMl布局中的代码
<com.example.administrator.gamehleper.widget.ScaleView
<ImageView
android:id="@+id/iv_download"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="每日礼包"
android:textSize="20dp"
android:textColor="#FFFFFF"
android:gravity="center"/>
</com.example.administrator.gamehleper.widget.ScaleView>
产生都动画效果ScaleView类中的代码:
public class ScaleView extends RelativeLayout {
private int maxLength;
private float radiu;
private float centerX, centerY;
private Paint paint;
private float FACTOR=0.1F;
public ScaleView(Context context, AttributeSet attrs) {
super(context, attrs);
paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.parseColor("#55ffffff"));
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//当手指抬起表示确认才执行操作
if (event.getAction() == MotionEvent.ACTION_UP) {
radiu = 0;
maxLength = (int) Math.sqrt(Math.pow(getWidth(), 2) + Math.pow(getHeight(), 2));
centerX = event.getX();
centerY = event.getY();
startScaleAnimation();
invalidate();
}
return true;
}
private void startScaleAnimation() {
ObjectAnimator animator=ObjectAnimator.ofFloat(this,"xxx",0,1).setDuration(300);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float progress= (float) animation.getAnimatedValue();
updateView(progress);
}
});
animator.start();
}
private void updateView(float progress) {
setPivotX(getWidth()/2);
setPivotY(getHeight()/2);
if(progress>=0&&progress<=0.5f){
setScaleX(1+FACTOR*progress);
setScaleY(1+FACTOR*progress);
}else{
setScaleX(1+FACTOR*(1-progress));
setScaleY(1+FACTOR*(1-progress));
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (radiu > maxLength) {
return;
}
canvas.drawCircle(centerX, centerY, radiu, paint);
radiu = radiu + 30;
//防止第一次进入onDraw
invalidate();
}
}