Android 水滴下落效果View

ASingleAndroid發表於2015-06-01

    第一次寫部落格,寫的不好還希望大家見諒!

    很多時候我們都需要自己來寫View,今天給大家個自己寫的水滴下落的效果,圖片換成水滴即可。

   WaterWaveView.java


package com.berfy.dbcache.view;


import com.berfy.dbcache.R;
import com.berfy.dbcache.util.MyLog;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;


public class WaterWaveView extends View {


private Context mContext;


private boolean mStarted = false;
private final Paint mPaint = new Paint();
private boolean mIsRun;
private long mRawTime;
private float mAngle;
private float mAngle_water;
private float mWaterY;
private float mWaterSin = 15;// 睡眠波浪振幅 數值越大波浪越大
private float mWaterSize = 0.1f;// 水滴尺寸 0-1
private float mLevel = 0.5f;// 水面高度
private float mSpeed = 0.2f;// 水浪速度
private Matrix mMatrix;
private Bitmap mWater;
private PaintFlagsDrawFilter mDrawFilter;


private int mWidth, mHeight;


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


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


private void init() {
mDrawFilter = new PaintFlagsDrawFilter(Paint.ANTI_ALIAS_FLAG,
Paint.DITHER_FLAG);
mPaint.setAlpha(150);
mPaint.setColor(Color.rgb(89, 186, 231));
}


public void startWave() {
mWater = BitmapFactory.decodeResource(mContext.getResources(),
R.drawable.ic_launcher);
setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
if (!mStarted) {
mIsRun = true;
mStarted = true;
mRawTime = System.currentTimeMillis();
new Thread(new Runnable() {


@Override
public void run() {
// TODO Auto-generated method stub
while (mIsRun) {
mAngle += mSpeed;
if (mAngle == 360) {
mAngle = 0;
}
mAngle_water += mSpeed;
if (mAngle_water == 360) {
mAngle_water = 0;
}


float trueHeight = mHeight * (1 - mLevel);
mWaterSize += 0.0001;


if (mWaterY > trueHeight * 2) {
mWaterY = 0;
mWaterSize = 0.1f;
mRawTime = System.currentTimeMillis();
}


float time = (float) (((System.currentTimeMillis() * 0.1 - mRawTime * 0.1)) / 500);


mWaterY += time;
MyLog.e("下落", mWaterY + "");


mHandler.sendEmptyMessage(0);
try {
Thread.sleep(1L);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();
}
}


private Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
invalidate();
};
};


@Override
public void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.setDrawFilter(mDrawFilter);
mWidth = getWidth();
mHeight = getHeight();
double lineX = 0;
double lineY = 0;
mMatrix = new Matrix();
mMatrix.postScale(mWaterSize, mWaterSize);
mMatrix.postTranslate(getWidth() / 2
- (int) (mWater.getWidth() * mWaterSize) / 2, mWaterY);
canvas.drawBitmap(mWater, mMatrix, mPaint);
// canvas.drawBitmap(mWater, width / 2, angle, mPaint1);


for (int i = 0; i < getWidth(); i++) {
lineX = i;
if (mIsRun) {
lineY = mWaterSin * Math.sin((i + mAngle) * Math.PI / 180) + 50;
// MyLog.e("高度", lineY3 + "");
} else {
lineY = 50;
}
canvas.drawLine((int) lineX,
(int) (lineY + mHeight * (1 - mLevel)), (int) lineX + 1,
getHeight(), mPaint);
}
}


public void stop() {
mWater.recycle();
mWater = null;
mIsRun = false;
mAngle = 0;


}


}

相關文章