猎豹清理大师波浪动画实现

感谢段大师的指导!




波浪效果源码:

自定义控件:实现



package com.apidemos2.animation.bezierview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Region;
import android.graphics.Path.Direction;
import android.graphics.Region.Op;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;

import com.apidemos2.utils.HardwareAccUtils;
import com.apidemos2.utils.Utils;

public class WaterWaveView  extends View{

    private Handler mHandler;
    private long c = 0L;
    private boolean mStarted = false;
    private final float f = 0.033F;
    private int mAlpha = 31;
    private final int mColor = Color.GREEN;
    private float mAmplitude = 10.0F; // 振幅
    private final Paint mPaint = new Paint();
    private float mWateLevel = 0.5F;
    private Path mPath;
    
    private int mScreenWidth;
    private int mScreenHeight;

    public WaterWaveView(Context paramContext) {
        super(paramContext);
        init(paramContext);
    }

    public WaterWaveView(Context paramContext, AttributeSet paramAttributeSet) {
        super(paramContext, paramAttributeSet);
        init(paramContext);
    }

    public void startWave() {
        if (!mStarted) {
            this.c = 0L;
            mStarted= true;
            this.mHandler.sendEmptyMessage(0);
        }
    }

    private void init(Context context) {
        mPaint.setStrokeWidth(1.0F);
        mPaint.setColor(mColor);
        mPaint.setAlpha(mAlpha);
        mPath = new Path();
        mScreenWidth = Utils.getScreenWidth(context);
        mScreenHeight = Utils.getScreenHeight(context);
        mHandler = new Handler() {
            @Override
            public void handleMessage(android.os.Message msg) {
                if (msg.what == 0) {
                    invalidate();
                    if (mStarted) {
                        mHandler.sendEmptyMessageDelayed(0, 60L);
                    }
                }
            }
        };
    }
  
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();
        int width = getWidth();
        int height = getHeight();
        if ((!mStarted) || (width == 0) || (height == 0)) {
            canvas.drawRect(0.0F, height / 2, width, height, mPaint);
            return;
        }
        if (this.c >= 8388607L) {
            this.c = 0L;
        }
        this.c = (1L + this.c);
        float f1 = height * (1.0F - mWateLevel);
        int top = (int) (f1 + mAmplitude);
        mPath.reset();
        mPath.addCircle(mScreenWidth/2,mScreenWidth/2, mScreenWidth/2, Path.Direction.CCW);
       canvas.clipPath(mPath, Region.Op.REPLACE);
        canvas.drawRect(0.0F, top, width, height, mPaint);
     //   canvas.drawPath(mPath, mPaint);
        
     /*   RectF rectF = new RectF(0.0F, top + 100, width, height);
        canvas.drawArc(rectF, 0, 180, true, mPaint);*/
        int n = (int) (f1 - this.mAmplitude
                * Math.sin(Math.PI * (2.0F * (0.0F + this.c * width * this.f)) / width));
        int startX = 0;
        int stopX = 0;
        while (stopX < width) {
            int startY = (int) (f1 - mAmplitude
                    * Math.sin(Math.PI * (2.0F * (stopX + this.c * width * this.f))
                            / width));
            canvas.drawLine(startX, n, stopX, startY, mPaint);
            canvas.drawLine(stopX, startY, stopX, top, mPaint);
            int i4 = stopX + 1;
            startX = stopX;
            stopX = i4;
            n = startY;
        }
        canvas.restore();
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        HardwareAccUtils.setLayerTypeAsSoftware(this);
    }

    public void setAmplitude(float amplitued) {
        mAmplitude = amplitued;
    }

    public void setWaterAlpha(float alpha) {
        this.mAlpha = ((int) (255.0F * alpha));
        mPaint.setAlpha(this.mAlpha);
    }

    public void setWaterLevel(float paramFloat) {
        mWateLevel = paramFloat;
    }
}



评论 45
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值