修改FallView文件,主要参考RenderScriptWallpaper和RenderScriptWallpaper中对RenderscriptGL初始化和调用的时机,在覆写的RSSurfaceView指定方法中初始化和调用RenderScriptGL。
FallView.java文件如下:
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.harlan.waterscreen;
import android.content.Context;
import android.renderscript.RSSurfaceView;
import android.renderscript.RenderScript;
import android.renderscript.RenderScriptGL;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
class FallView extends RSSurfaceView {
private FallRS mRender;
/*******add by Harlan***********************/
private RenderScriptGL mRs;
/*******************************************/
public FallView(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
super.surfaceChanged(holder, format, w, h);
// RenderScriptGL.SurfaceConfig sc = new RenderScriptGL.SurfaceConfig();
// RenderScriptGL RS = createRenderScriptGL(sc);
// mRender = new FallRS(w, h);
// mRender.init(RS, getResources(), false);
// mRender.start();
/*******add by Harlan***********************/
if (mRs != null) {
mRs.setSurface(holder, w, h);
}
if (mRender == null) {
mRender = new FallRS(w, h);
mRender.init(mRs, getResources(), false);
mRender.start();
} else {
mRender.resize(w, h);
}
/*****************************************/
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
mRender.addDrop(event.getX(), event.getY());
try {
Thread.sleep(16);
} catch (InterruptedException e) {
// Ignore
}
break;
}
return true;
}
/*******add by Harlan***********************/
@Override
public void surfaceCreated(SurfaceHolder holder)
{
super.surfaceCreated(holder);
//初始化RenderScriptRS
RenderScriptGL.SurfaceConfig sc = new RenderScriptGL.SurfaceConfig();
if (mRs == null) {
mRs = createRenderScriptGL(sc);
}
mRs.setPriority(RenderScript.Priority.LOW);
}
@Override
protected void onDetachedFromWindow()
{
super.onDetachedFromWindow();
destroyRenderer();
}
private void destroyRenderer() {
if (mRender != null) {
mRender.stop();
mRender = null;
}
if (mRs != null) {
mRs.setSurface(null, 0, 0);
mRs.destroy();
mRs = null;
}
}
}
在Fall activity中将FallView设为内容背景。放在我的手机上运行下,发现已经可以实现大致功能,但连续触屏时,水滴团簇在一块儿,效果不是很好,而且没有水滴音效,不是很自然。
如图:
处理下,先加上音效.这里我使用SoundPool播放音效。SoundPool使用音效池的概念来管理多个短促的音效,在程序中按照音效的ID进行播放。
为了方便管理SoundPool所加载的每个声音的ID,我使用了一个HashMap对象来管理声音。(音频文件提取自三星9300)
定义需要用到的常量变量:
public static final int PLAY_DROP_MUSIC = 0x1;
public static final int PLAY_WATER_MUSIC = 0x2;
private boolean isPlay;
private static int sConut;
private SoundPool mSound;
private HashMap soundMap;
在surfaceCreated(SurfaceHolder holder)方法中添加:
mSound = new SoundPool(2, AudioManager.STREAM_RING, 5);
soundMap = new HashMap();
soundMap.put(1, mSound.load(getContext(), R.raw.drop, 5));
soundMap.put(2, mSound.load(getContext(), R.raw.water, 5));
在TouchEvent事件中加上音效的播放,并对水波纹addDrop方法调用的频率改进一下,使其自然些。
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mSound.play(soundMap.get(1), 5, 5, 0, 0, 1);
isPlay = true;
sConut = 0;
mRender.addDrop(event.getX(), event.getY());
break;
case MotionEvent.ACTION_MOVE:
sConut ++;
if (isPlay){
mSound.play(soundMap.get(2), 5, 5, 0, 0, 1);
isPlay = false;
}
if (sConut <10){
mRender.addDrop(event.getX(), event.getY());
}
MotionEvent.obtain(event).setAction(MotionEvent.ACTION_UP);
try {
Thread.sleep(150);
} catch (InterruptedException e) {
// Ignore
}
break;
}
return true;
}
好了,这时候水波纹的效果就自然很多了。
好了,大致完工了,最后将pond.png图片置换为三星蒲公英图片。另外,在Manifest文件里面将应用主题设置为全屏显示。
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
值得注意的是,源码枫叶壁纸中,河床是图片的倒影,因此蒲公英也是倒着的,修改下参数,将FallRS.java中String t中的
" varTex0 = vec2((pos.x + 1.0), (pos.y + 1.6666));\n" +
以及
" varTex0.xy *= vec2(0.25, 0.33);\n" +
修改为
" varTex0 = vec2((pos.x +1.2), (-pos.y+1.15));\n" +
" varTex0.xy *= vec2(0.4, 0.45);\n" +
OK,大功告成,最终效果图:
最后,贴上附件(三星S3水波纹触屏效果源码):