资源简介
参照:http://blog.csdn.net/Poison_H/article/details/43735445。点击手机屏幕实现水波效果,如下图:
【核心代码】
public class WaterWaveView extends SurfaceView implements SurfaceHolder.Callback { // 背景图的宽度和高度 private int backWidth; private int backHeight; /** * buf1 和 buf2是波能缓冲区,分别代表了每个点的前一时刻和后一时刻的波幅数据 */ private short[] buf1; private short[] buf2; private int[] bitmap1; private int[] bitmap2; private Bitmap bgImage = null; // 是否第一次加载 private boolean firstLoad = false; WavingThread wavingThread = new WavingThread(); // 显示一个surface的抽象接口,使你可以控制surface的大小和格式, 以及在surface上编辑像素,和监视surace的改变 SurfaceHolder mSurfaceHolder = null; private int doubleWidth; private int fiveWidth; // 持续时间 private int loopTime; private int bitmapLen; public WaterWaveView(Context context) { super(context); mSurfaceHolder = getHolder(); mSurfaceHolder.addCallback(this); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { if (!firstLoad) { // 背景图 bgImage = BitmapFactory.decodeResource(this.getResources(), R.drawable.bg); bgImage = Bitmap.createScaledBitmap(bgImage, w, h, false);// 缩放而已 backWidth = bgImage.getWidth(); backHeight = bgImage.getHeight(); buf2 = new short[backWidth * backHeight]; buf1 = new short[backWidth * backHeight]; bitmap2 = new int[backWidth * backHeight]; bitmap1 = new int[backWidth * backHeight]; // 将bgImage的像素拷贝到bitmap1数组中,用于渲染。。。 bgImage.getPixels(bitmap1, 0, backWidth, 0, 0, backWidth, backHeight); bgImage.getPixels(bitmap2, 0, backWidth, 0, 0, backWidth, backHeight); for (int i = 0; i < backWidth * backHeight; i) { buf2[i] = 0; buf1[i] = 0; } doubleWidth = backWidth << 1; fiveWidth = 5 * backWidth; loopTime = ((backHeight - 4) * backWidth) >> 1; bitmapLen = backWidth * backHeight - 1; firstLoad = true; } } class WavingThread extends Thread { boolean running = true; public void setRunning(boolean running) { this.running = running; } @Override public void run() { Canvas c = null; while (running) { c = mSurfaceHolder.lockCanvas(); makeRipple(); doDraw(c); mSurfaceHolder.unlockCanvasAndPost(c); } } } /******************************************************* * 计算波能数据缓冲区 *******************************************************/ private void makeRipple() { int k = fiveWidth; int xoff = 0, yoff = 0; int cp = 0; int tarClr = 0; int i = fiveWidth; while (i < loopTime) { // 波能扩散 buf2[k] = (short) (((buf1[k - 2] buf1[k 2] buf1[k - doubleWidth] buf1[k doubleWidth]) >> 1) - buf2[k]); // 波能衰减 buf2[k] = (short) (buf2[k] - (buf2[k] >> 5)); // 求出该点的左上的那个点xoff,yoff cp = k - doubleWidth - 2; xoff = buf2[cp - 2] - buf2[cp 2]; yoff = buf2[cp - doubleWidth] - buf2[k - 2]; tarClr = k yoff * doubleWidth xoff; if (tarClr > bitmapLen || tarClr < 0) { k = 2; continue; } // 复制象素 bitmap2[k] = bitmap1[tarClr]; k = 2; i; } short[] tmpBuf = buf2; buf2 = buf1; buf1 = tmpBuf; } /***************************************************** * 增加波源 x坐标 y坐标 波源半径 波源能量 *****************************************************/ private void touchWater(int x, int y, int stonesize, int stoneweight) { // 判断坐标是否在屏幕范围内 if (x stonesize > backWidth) { return; } if (y stonesize > backHeight) { return; } if (x - stonesize < 0) { return; } if (y - stonesize < 0) { return; } // 产生波源,填充前导波能缓冲池 int endStoneX = x stonesize; int endStoneY = y stonesize; int squaSize = stonesize * stonesize; int posy = y - stonesize; int posx = x - stonesize; for (posy = y - stonesize; posy < endStoneY; posy) { for (posx = x - stonesize; posx < endStoneX; posx) { if ((posx - x) * (posx - x) (posy - y) * (posy - y) < squaSize) { buf1[backWidth * posy posx] = (short) -stoneweight; } } } } /***************************************************** * 增加波源 x坐标 y坐标 波源半径 波源能量 *****************************************************/ private void trickWater(int x, int y, int stonesize, int stoneweight) { // 判断坐标是否在屏幕范围内 if (x stonesize > backWidth) { return; } if (y stonesize > backHeight) { return; } if (x - stonesize < 0) { return; } if (y - stonesize < 0) { return; } // 产生波源,填充波能缓冲池 int endStoneX = x stonesize; int endStoneY = y stonesize; int posy = y - stonesize; int posx = x - stonesize; for (posy = y - stonesize; posy < endStoneY; posy) { for (posx = x - stonesize; posx < endStoneX; posx) { if (posy >= 0 && posy < backHeight && posx >= 0 && posx < backWidth) { buf1[backWidth * posy posx] = (short) -stoneweight; } } } } @Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { // 设置波源半径和波源能量 touchWater((int) event.getX(), (int) event.getY(), 200, 300); } else if (event.getAction() == MotionEvent.ACTION_MOVE) { trickWater((int) event.getX(), (int) event.getY(), 200, 300); } return true; } protected void doDraw(Canvas canvas) { /** * Parameters: 1.colors 2.offset 3.stride 4.x 5. y 6.width 7.height * 8.hasAlpha 9. paint */ //绘制 canvas.drawBitmap(bitmap2, 0, backWidth, 0, 0, backWidth, backHeight, false, null); } @Override public void surfaceCreated(SurfaceHolder holder) { wavingThread.setRunning(true); wavingThread.start(); } @Override public void surfaceDestroyed(SurfaceHolder holder) { boolean retry = true; wavingThread.setRunning(false); // 非暴力关闭线程,直到此次该线程运行结束之前,主线程停止运行,以防止Surface被重新激活 while (retry) { try { wavingThread.join(); // 阻塞current // Thread(当前执行线程)直到被调用线程(thread)完成。 retry = false; } catch (InterruptedException e) { } } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } }
waterWaveView = new WaterWaveView(this); setContentView(waterWaveView);
代码片段和文件信息
/**
* Automatically generated file. DO NOT MODIFY
*/
package com.casjs.waterwave.test;
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean(“true“);
public static final String APPLICATION_ID = “com.casjs.waterwave.test“;
public static final String BUILD_TYPE = “debug“;
public static final String FLAVOR = ““;
public static final int VERSION_CODE = 1;
public static final String VERSION_NAME = “1.0“;
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-03-05 13:16 WaterWave\
文件 127 2018-03-05 13:16 WaterWave\.gitignore
目录 0 2018-03-05 13:16 WaterWave\.gradle\
目录 0 2018-03-05 13:35 WaterWave\.gradle\4.1\
目录 0 2018-03-05 13:16 WaterWave\.gradle\4.1\fileChanges\
文件 1 2018-03-05 14:54 WaterWave\.gradle\4.1\fileChanges\last-build.bin
目录 0 2018-03-05 13:35 WaterWave\.gradle\4.1\fileContent\
文件 17 2018-03-05 14:54 WaterWave\.gradle\4.1\fileContent\fileContent.lock
目录 0 2018-03-05 13:16 WaterWave\.gradle\4.1\fileHashes\
文件 266969 2018-03-05 14:54 WaterWave\.gradle\4.1\fileHashes\fileHashes.bin
文件 17 2018-03-05 14:54 WaterWave\.gradle\4.1\fileHashes\fileHashes.lock
文件 19925 2018-03-05 13:36 WaterWave\.gradle\4.1\fileHashes\resourceHashesCache.bin
目录 0 2018-03-05 13:35 WaterWave\.gradle\4.1\javaCompile\
文件 775418 2018-03-05 13:56 WaterWave\.gradle\4.1\javaCompile\classAnalysis.bin
文件 321993 2018-03-05 13:36 WaterWave\.gradle\4.1\javaCompile\jarAnalysis.bin
文件 17 2018-03-05 13:56 WaterWave\.gradle\4.1\javaCompile\javaCompile.lock
文件 142201 2018-03-05 13:56 WaterWave\.gradle\4.1\javaCompile\taskHistory.bin
文件 20786 2018-03-05 13:56 WaterWave\.gradle\4.1\javaCompile\taskJars.bin
目录 0 2018-03-05 13:16 WaterWave\.gradle\4.1\taskHistory\
文件 2343365 2018-03-05 14:53 WaterWave\.gradle\4.1\taskHistory\fileSnapshots.bin
文件 78864 2018-03-05 14:53 WaterWave\.gradle\4.1\taskHistory\taskHistory.bin
文件 17 2018-03-05 14:54 WaterWave\.gradle\4.1\taskHistory\taskHistory.lock
目录 0 2018-03-05 13:16 WaterWave\.gradle\buildOutputCleanup\
文件 0 2018-03-05 13:16 WaterWave\.gradle\buildOutputCleanup\built.bin
文件 51 2018-03-05 13:16 WaterWave\.gradle\buildOutputCleanup\cache.properties
文件 2 2018-03-05 14:54 WaterWave\.gradle\buildOutputCleanup\cache.properties.lock
目录 0 2018-03-05 15:08 WaterWave\.idea\
文件 626 2018-03-05 13:16 WaterWave\.idea\gradle.xm
目录 0 2018-03-05 13:22 WaterWave\.idea\libraries\
文件 533 2018-03-05 13:16 WaterWave\.idea\libraries\android_arch_core_common_1_0_0_jar.xm
文件 548 2018-03-05 13:16 WaterWave\.idea\libraries\android_arch_lifecycle_common_1_0_0_jar.xm
............此处省略2976个文件信息
- 上一篇:android 版本更新
- 下一篇:android+netty 入门级
相关资源
- android bluetooth 搜尋距離
- Android应用源码追加的6中图片特效处理
- 完美解决android_gif_drawable_demo
- android 调整屏幕亮度 (无需root)
- android通过ajax上传文件到php服务器
- android 联动滚轮(选择控件)
- cad快速看图 破解版 永久免费
- WordPress大前端主题DUX5.3破解优化版
- PDF数字签名(ITEXT+keytool)
- darkcomet原版(从作者那要来的)
-
android_自定义ti
tleBar - 《Core.Java.Volume.I.Fundamentals.10th.Editio
- 《IBM存储串口命令大全》pdf
- android 异常崩溃后 重启app(进程守护
- QQSlidingMenu(QQ5.0的侧滑菜单特性)
- SwipeRefreshLayoutDemo(下拉刷新)
- TurntableDemo(圆形菜单)
- ViewPagerAnimator(ViewPager动画切换)
- DialogPlusDemo(对话框组件,可做上下弹
- 最完整的伺服培训教程(图文并茂)
- 2017年数模国赛优秀论文
- android 字母导航
- android 日期选择控件(可选择小时和分
- 《CONCEPTS OF PROGRAMMING LANGUAGES》pdf 高清
- 电力电子技术册_第五版_王兆安_课后
- Android滑动解锁
- 安卓JNA例程
- Java How to Program10th Edition
- Android 分贝测试仪(多媒体-10_091)
- Android 弹出底部弹窗(5.0 +效果(Mat
评论
共有 条评论