Android 水滴下落效果View
第一次寫部落格,寫的不好還希望大家見諒!
很多時候我們都需要自己來寫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;
}
}
相關文章
- jQuery水滴彈性摘取效果jQuery
- CSS製作水滴波浪效果案例CSS
- Android 自定義View 點贊效果AndroidView
- android仿微信表情雨下落!Android
- css3水滴效果程式碼例項CSSS3
- CSS3水滴向下滴落動畫效果CSSS3動畫
- Android自定義View 雷達掃描效果AndroidView
- 使用 HTML5 Canvas 繪製的水滴效果HTMLCanvas
- Android自定義View實現文字輪播效果AndroidView
- Android 自定義View UC下拉重新整理效果(二)AndroidView
- Android 自定義View UC下拉重新整理效果(三)AndroidView
- Android 自定義View UC下拉重新整理效果(一)AndroidView
- Android ViewAndroidView
- Android自定義View:View(二)AndroidView
- Android自定義View實現流式佈局(熱門標籤效果)AndroidView
- 直播系統程式碼,Android自定義View實現呼吸燈效果AndroidView
- 直播平臺原始碼,Android自定義View實現呼吸燈效果原始碼AndroidView
- Android自定義View——從零開始實現雪花飄落效果AndroidView
- Android劉海屏、水滴屏全面屏適配方案Android
- Android View 系統 1 - View樹AndroidView
- Android自定義View——從零開始實現覆蓋翻頁效果AndroidView
- android view 分析AndroidView
- 手教手你使用ps繪製葉片逼真的水滴效果(分享)
- Android XML佈局報錯:android/view/View$OnUnhandledKeyEventListenerAndroidXMLView
- Android自定義view-自繪ViewAndroidView
- Android自定義View——從零開始實現書籍翻頁效果(二)AndroidView
- Android自定義View——從零開始實現書籍翻頁效果(四)AndroidView
- Android自定義View——從零開始實現書籍翻頁效果(三)AndroidView
- Android自定義View——從零開始實現書籍翻頁效果(一)AndroidView
- view的陰影效果shadowColorView
- 小球下落-二叉樹二叉樹
- Android View post 方法AndroidView
- Android 自定義viewAndroidView
- Android: 自定義ViewAndroidView
- Android view架構AndroidView架構
- Android View總結AndroidView
- Android自定義View教你一步一步實現即刻點贊效果AndroidView
- Android技術分享|【自定義View】實現Material Design的Loading效果AndroidViewMaterial Design