点击图片产生水波的动画效果代码展示

效果展示先:
这里写图片描述

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();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值