Three implementations of a live wallpaper showing a spinning cube.
One very basic, one slightly more elaborate (selectable shape, settings panel), and one that uses renderscript.
This commit is contained in:
@@ -0,0 +1,240 @@
|
||||
/*
|
||||
* 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.android.livecubes.cube1;
|
||||
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Rect;
|
||||
import android.os.Handler;
|
||||
import android.os.SystemClock;
|
||||
import android.service.wallpaper.WallpaperService;
|
||||
import android.util.Log;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.SurfaceHolder;
|
||||
|
||||
/*
|
||||
* This animated wallpaper draws a rotating wireframe cube.
|
||||
*/
|
||||
public class CubeWallpaper1 extends WallpaperService {
|
||||
|
||||
private final Handler mHandler = new Handler();
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Engine onCreateEngine() {
|
||||
return new CubeEngine();
|
||||
}
|
||||
|
||||
class CubeEngine extends Engine {
|
||||
|
||||
private final Paint mPaint = new Paint();
|
||||
private float mOffset;
|
||||
private float mTouchX = -1;
|
||||
private float mTouchY = -1;
|
||||
private long mStartTime;
|
||||
private float mCenterX;
|
||||
private float mCenterY;
|
||||
|
||||
private final Runnable mDrawCube = new Runnable() {
|
||||
public void run() {
|
||||
drawFrame();
|
||||
}
|
||||
};
|
||||
private boolean mVisible;
|
||||
|
||||
CubeEngine() {
|
||||
// Create a Paint to draw the lines for our cube
|
||||
final Paint paint = mPaint;
|
||||
paint.setColor(0xffffffff);
|
||||
paint.setAntiAlias(true);
|
||||
paint.setStrokeWidth(2);
|
||||
paint.setStrokeCap(Paint.Cap.ROUND);
|
||||
paint.setStyle(Paint.Style.STROKE);
|
||||
|
||||
mStartTime = SystemClock.elapsedRealtime();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(SurfaceHolder surfaceHolder) {
|
||||
super.onCreate(surfaceHolder);
|
||||
|
||||
// By default we don't get touch events, so enable them.
|
||||
setTouchEventsEnabled(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
mHandler.removeCallbacks(mDrawCube);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onVisibilityChanged(boolean visible) {
|
||||
mVisible = visible;
|
||||
if (visible) {
|
||||
drawFrame();
|
||||
} else {
|
||||
mHandler.removeCallbacks(mDrawCube);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
|
||||
super.onSurfaceChanged(holder, format, width, height);
|
||||
// store the center of the surface, so we can draw the cube in the right spot
|
||||
mCenterX = width/2.0f;
|
||||
mCenterY = height/2.0f;
|
||||
drawFrame();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSurfaceCreated(SurfaceHolder holder) {
|
||||
super.onSurfaceCreated(holder);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSurfaceDestroyed(SurfaceHolder holder) {
|
||||
super.onSurfaceDestroyed(holder);
|
||||
mVisible = false;
|
||||
mHandler.removeCallbacks(mDrawCube);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onOffsetsChanged(float xOffset, float yOffset, int xPixels, int yPixels) {
|
||||
mOffset = xOffset;
|
||||
drawFrame();
|
||||
}
|
||||
|
||||
/*
|
||||
* Store the position of the touch event so we can use it for drawing later
|
||||
*/
|
||||
@Override
|
||||
public void onTouchEvent(MotionEvent event) {
|
||||
if (event.getAction() == MotionEvent.ACTION_MOVE) {
|
||||
mTouchX = event.getX();
|
||||
mTouchY = event.getY();
|
||||
} else {
|
||||
mTouchX = -1;
|
||||
mTouchY = -1;
|
||||
}
|
||||
super.onTouchEvent(event);
|
||||
}
|
||||
|
||||
/*
|
||||
* Draw one frame of the animation. This method gets called repeatedly
|
||||
* by posting a delayed Runnable. You can do any drawing you want in
|
||||
* here. This example draws a wireframe cube.
|
||||
*/
|
||||
void drawFrame() {
|
||||
final SurfaceHolder holder = getSurfaceHolder();
|
||||
|
||||
Canvas c = null;
|
||||
try {
|
||||
c = holder.lockCanvas();
|
||||
if (c != null) {
|
||||
// draw something
|
||||
drawCube(c);
|
||||
drawTouchPoint(c);
|
||||
}
|
||||
} finally {
|
||||
if (c != null) holder.unlockCanvasAndPost(c);
|
||||
}
|
||||
|
||||
// Reschedule the next redraw
|
||||
mHandler.removeCallbacks(mDrawCube);
|
||||
if (mVisible) {
|
||||
mHandler.postDelayed(mDrawCube, 1000 / 25);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Draw a wireframe cube by drawing 12 3 dimensional lines between
|
||||
* adjacent corners of the cube
|
||||
*/
|
||||
void drawCube(Canvas c) {
|
||||
c.save();
|
||||
c.translate(mCenterX, mCenterY);
|
||||
c.drawColor(0xff000000);
|
||||
drawLine(c, -400, -400, -400, 400, -400, -400);
|
||||
drawLine(c, 400, -400, -400, 400, 400, -400);
|
||||
drawLine(c, 400, 400, -400, -400, 400, -400);
|
||||
drawLine(c, -400, 400, -400, -400, -400, -400);
|
||||
|
||||
drawLine(c, -400, -400, 400, 400, -400, 400);
|
||||
drawLine(c, 400, -400, 400, 400, 400, 400);
|
||||
drawLine(c, 400, 400, 400, -400, 400, 400);
|
||||
drawLine(c, -400, 400, 400, -400, -400, 400);
|
||||
|
||||
drawLine(c, -400, -400, 400, -400, -400, -400);
|
||||
drawLine(c, 400, -400, 400, 400, -400, -400);
|
||||
drawLine(c, 400, 400, 400, 400, 400, -400);
|
||||
drawLine(c, -400, 400, 400, -400, 400, -400);
|
||||
c.restore();
|
||||
}
|
||||
|
||||
/*
|
||||
* Draw a 3 dimensional line on to the screen
|
||||
*/
|
||||
void drawLine(Canvas c, int x1, int y1, int z1, int x2, int y2, int z2) {
|
||||
long now = SystemClock.elapsedRealtime();
|
||||
float xrot = ((float)(now - mStartTime)) / 1000;
|
||||
float yrot = (0.5f - mOffset) * 2.0f;
|
||||
float zrot = 0;
|
||||
|
||||
// 3D transformations
|
||||
|
||||
// rotation around X-axis
|
||||
float newy1 = (float)(Math.sin(xrot) * z1 + Math.cos(xrot) * y1);
|
||||
float newy2 = (float)(Math.sin(xrot) * z2 + Math.cos(xrot) * y2);
|
||||
float newz1 = (float)(Math.cos(xrot) * z1 - Math.sin(xrot) * y1);
|
||||
float newz2 = (float)(Math.cos(xrot) * z2 - Math.sin(xrot) * y2);
|
||||
|
||||
// rotation around Y-axis
|
||||
float newx1 = (float)(Math.sin(yrot) * newz1 + Math.cos(yrot) * x1);
|
||||
float newx2 = (float)(Math.sin(yrot) * newz2 + Math.cos(yrot) * x2);
|
||||
newz1 = (float)(Math.cos(yrot) * newz1 - Math.sin(yrot) * x1);
|
||||
newz2 = (float)(Math.cos(yrot) * newz2 - Math.sin(yrot) * x2);
|
||||
|
||||
// 3D-to-2D projection
|
||||
float startX = newx1 / (4 - newz1 / 400);
|
||||
float startY = newy1 / (4 - newz1 / 400);
|
||||
float stopX = newx2 / (4 - newz2 / 400);
|
||||
float stopY = newy2 / (4 - newz2 / 400);
|
||||
|
||||
c.drawLine(startX, startY, stopX, stopY, mPaint);
|
||||
}
|
||||
|
||||
/*
|
||||
* Draw a circle around the current touch point, if any.
|
||||
*/
|
||||
void drawTouchPoint(Canvas c) {
|
||||
if (mTouchX >=0 && mTouchY >= 0) {
|
||||
c.drawCircle(mTouchX, mTouchY, 80, mPaint);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,296 @@
|
||||
/*
|
||||
* 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.android.livecubes.cube2;
|
||||
|
||||
import com.android.livecubes.R;
|
||||
|
||||
import android.content.SharedPreferences;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Rect;
|
||||
import android.os.Handler;
|
||||
import android.os.SystemClock;
|
||||
import android.service.wallpaper.WallpaperService;
|
||||
import android.util.Log;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.SurfaceHolder;
|
||||
|
||||
/*
|
||||
* This animated wallpaper draws a rotating wireframe shape. It is similar to
|
||||
* example #1, but has a choice of 2 shapes, which are user selectable and
|
||||
* defined in resources instead of in code.
|
||||
*/
|
||||
|
||||
public class CubeWallpaper2 extends WallpaperService {
|
||||
|
||||
public static final String SHARED_PREFS_NAME="cube2settings";
|
||||
|
||||
static class ThreeDPoint {
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
}
|
||||
|
||||
static class ThreeDLine {
|
||||
int startPoint;
|
||||
int endPoint;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Engine onCreateEngine() {
|
||||
return new CubeEngine();
|
||||
}
|
||||
|
||||
class CubeEngine extends Engine
|
||||
implements SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
|
||||
private final Handler mHandler = new Handler();
|
||||
|
||||
ThreeDPoint [] mOriginalPoints;
|
||||
ThreeDPoint [] mRotatedPoints;
|
||||
ThreeDLine [] mLines;
|
||||
private final Paint mPaint = new Paint();
|
||||
private float mOffset;
|
||||
private float mTouchX = -1;
|
||||
private float mTouchY = -1;
|
||||
private long mStartTime;
|
||||
private float mCenterX;
|
||||
private float mCenterY;
|
||||
|
||||
private final Runnable mDrawCube = new Runnable() {
|
||||
public void run() {
|
||||
drawFrame();
|
||||
}
|
||||
};
|
||||
private boolean mVisible;
|
||||
private SharedPreferences mPrefs;
|
||||
|
||||
CubeEngine() {
|
||||
// Create a Paint to draw the lines for our cube
|
||||
final Paint paint = mPaint;
|
||||
paint.setColor(0xffffffff);
|
||||
paint.setAntiAlias(true);
|
||||
paint.setStrokeWidth(2);
|
||||
paint.setStrokeCap(Paint.Cap.ROUND);
|
||||
paint.setStyle(Paint.Style.STROKE);
|
||||
|
||||
mStartTime = SystemClock.elapsedRealtime();
|
||||
|
||||
mPrefs = CubeWallpaper2.this.getSharedPreferences(SHARED_PREFS_NAME, 0);
|
||||
mPrefs.registerOnSharedPreferenceChangeListener(this);
|
||||
onSharedPreferenceChanged(mPrefs, null);
|
||||
}
|
||||
|
||||
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
|
||||
|
||||
String shape = prefs.getString("cube2_shape", "cube");
|
||||
|
||||
// read the 3D model from the resource
|
||||
readModel(shape);
|
||||
}
|
||||
|
||||
private void readModel(String prefix) {
|
||||
// Read the model definition in from a resource.
|
||||
|
||||
// get the resource identifiers for the arrays for the selected shape
|
||||
int pid = getResources().getIdentifier(prefix + "points", "array", getPackageName());
|
||||
int lid = getResources().getIdentifier(prefix + "lines", "array", getPackageName());
|
||||
|
||||
String [] p = getResources().getStringArray(pid);
|
||||
int numpoints = p.length;
|
||||
mOriginalPoints = new ThreeDPoint[numpoints];
|
||||
mRotatedPoints = new ThreeDPoint[numpoints];
|
||||
|
||||
for (int i = 0; i < numpoints; i++) {
|
||||
mOriginalPoints[i] = new ThreeDPoint();
|
||||
mRotatedPoints[i] = new ThreeDPoint();
|
||||
String [] coord = p[i].split(" ");
|
||||
mOriginalPoints[i].x = Float.valueOf(coord[0]);
|
||||
mOriginalPoints[i].y = Float.valueOf(coord[1]);
|
||||
mOriginalPoints[i].z = Float.valueOf(coord[2]);
|
||||
}
|
||||
|
||||
String [] l = getResources().getStringArray(lid);
|
||||
int numlines = l.length;
|
||||
mLines = new ThreeDLine[numlines];
|
||||
|
||||
for (int i = 0; i < numlines; i++) {
|
||||
mLines[i] = new ThreeDLine();
|
||||
String [] idx = l[i].split(" ");
|
||||
mLines[i].startPoint = Integer.valueOf(idx[0]);
|
||||
mLines[i].endPoint = Integer.valueOf(idx[1]);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(SurfaceHolder surfaceHolder) {
|
||||
super.onCreate(surfaceHolder);
|
||||
setTouchEventsEnabled(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
mHandler.removeCallbacks(mDrawCube);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onVisibilityChanged(boolean visible) {
|
||||
mVisible = visible;
|
||||
if (visible) {
|
||||
drawFrame();
|
||||
} else {
|
||||
mHandler.removeCallbacks(mDrawCube);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
|
||||
super.onSurfaceChanged(holder, format, width, height);
|
||||
// store the center of the surface, so we can draw the cube in the right spot
|
||||
mCenterX = width/2.0f;
|
||||
mCenterY = height/2.0f;
|
||||
drawFrame();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSurfaceCreated(SurfaceHolder holder) {
|
||||
super.onSurfaceCreated(holder);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSurfaceDestroyed(SurfaceHolder holder) {
|
||||
super.onSurfaceDestroyed(holder);
|
||||
mVisible = false;
|
||||
mHandler.removeCallbacks(mDrawCube);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onOffsetsChanged(float xOffset, float yOffset, int xPixels, int yPixels) {
|
||||
mOffset = xOffset;
|
||||
drawFrame();
|
||||
}
|
||||
|
||||
/*
|
||||
* Store the position of the touch event so we can use it for drawing later
|
||||
*/
|
||||
@Override
|
||||
public void onTouchEvent(MotionEvent event) {
|
||||
if (event.getAction() == MotionEvent.ACTION_MOVE) {
|
||||
mTouchX = event.getX();
|
||||
mTouchY = event.getY();
|
||||
} else {
|
||||
mTouchX = -1;
|
||||
mTouchY = -1;
|
||||
}
|
||||
super.onTouchEvent(event);
|
||||
}
|
||||
|
||||
/*
|
||||
* Draw one frame of the animation. This method gets called repeatedly
|
||||
* by posting a delayed Runnable. You can do any drawing you want in
|
||||
* here. This example draws a wireframe cube.
|
||||
*/
|
||||
void drawFrame() {
|
||||
final SurfaceHolder holder = getSurfaceHolder();
|
||||
final Rect frame = holder.getSurfaceFrame();
|
||||
final int width = frame.width();
|
||||
final int height = frame.height();
|
||||
|
||||
Canvas c = null;
|
||||
try {
|
||||
c = holder.lockCanvas();
|
||||
if (c != null) {
|
||||
// draw something
|
||||
drawCube(c);
|
||||
drawTouchPoint(c);
|
||||
}
|
||||
} finally {
|
||||
if (c != null) holder.unlockCanvasAndPost(c);
|
||||
}
|
||||
|
||||
mHandler.removeCallbacks(mDrawCube);
|
||||
if (mVisible) {
|
||||
mHandler.postDelayed(mDrawCube, 1000 / 25);
|
||||
}
|
||||
}
|
||||
|
||||
void drawCube(Canvas c) {
|
||||
c.save();
|
||||
c.translate(mCenterX, mCenterY);
|
||||
c.drawColor(0xff000000);
|
||||
|
||||
long now = SystemClock.elapsedRealtime();
|
||||
float xrot = ((float)(now - mStartTime)) / 1000;
|
||||
float yrot = (0.5f - mOffset) * 2.0f;
|
||||
rotateAndProjectPoints(xrot, yrot);
|
||||
drawLines(c);
|
||||
c.restore();
|
||||
}
|
||||
|
||||
void rotateAndProjectPoints(float xrot, float yrot) {
|
||||
int n = mOriginalPoints.length;
|
||||
for (int i = 0; i < n; i++) {
|
||||
// rotation around X-axis
|
||||
ThreeDPoint p = mOriginalPoints[i];
|
||||
float x = p.x;
|
||||
float y = p.y;
|
||||
float z = p.z;
|
||||
float newy = (float)(Math.sin(xrot) * z + Math.cos(xrot) * y);
|
||||
float newz = (float)(Math.cos(xrot) * z - Math.sin(xrot) * y);
|
||||
|
||||
// rotation around Y-axis
|
||||
float newx = (float)(Math.sin(yrot) * newz + Math.cos(yrot) * x);
|
||||
newz = (float)(Math.cos(yrot) * newz - Math.sin(yrot) * x);
|
||||
|
||||
// 3D-to-2D projection
|
||||
float screenX = newx / (4 - newz / 400);
|
||||
float screenY = newy / (4 - newz / 400);
|
||||
|
||||
mRotatedPoints[i].x = screenX;
|
||||
mRotatedPoints[i].y = screenY;
|
||||
mRotatedPoints[i].z = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void drawLines(Canvas c) {
|
||||
int n = mLines.length;
|
||||
for (int i = 0; i < n; i++) {
|
||||
ThreeDLine l = mLines[i];
|
||||
ThreeDPoint start = mRotatedPoints[l.startPoint];
|
||||
ThreeDPoint end = mRotatedPoints[l.endPoint];
|
||||
c.drawLine(start.x, start.y, end.x, end.y, mPaint);
|
||||
}
|
||||
}
|
||||
|
||||
void drawTouchPoint(Canvas c) {
|
||||
if (mTouchX >=0 && mTouchY >= 0) {
|
||||
c.drawCircle(mTouchX, mTouchY, 80, mPaint);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright (C) 2009 Google Inc.
|
||||
*
|
||||
* 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.android.livecubes.cube2;
|
||||
|
||||
import com.android.livecubes.R;
|
||||
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Bundle;
|
||||
import android.service.wallpaper.WallpaperSettingsActivity;
|
||||
|
||||
public class CubeWallpaper2Settings extends WallpaperSettingsActivity
|
||||
implements SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle icicle) {
|
||||
super.onCreate(icicle);
|
||||
getPreferenceManager().setSharedPreferencesName(
|
||||
CubeWallpaper2.SHARED_PREFS_NAME);
|
||||
addPreferencesFromResource(R.xml.cube2_settings);
|
||||
getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(
|
||||
this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(
|
||||
this);
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
|
||||
String key) {
|
||||
//(new BackupManager(this)).dataChanged();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,242 @@
|
||||
/*
|
||||
* 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.android.livecubes.cube3;
|
||||
|
||||
import com.android.livecubes.R;
|
||||
import com.android.livecubes.RenderScriptScene;
|
||||
|
||||
import android.renderscript.Allocation;
|
||||
import android.renderscript.Element;
|
||||
import android.renderscript.Primitive;
|
||||
import android.renderscript.ProgramRaster;
|
||||
import android.renderscript.ProgramVertex;
|
||||
import android.renderscript.ScriptC;
|
||||
import android.renderscript.SimpleMesh;
|
||||
import android.renderscript.Type;
|
||||
import android.renderscript.Element.Builder;
|
||||
|
||||
import java.util.TimeZone;
|
||||
|
||||
/*
|
||||
* This example draws a shape whose definition is read from resources (though
|
||||
* it's not user selectable like in example #2), but does the drawing using
|
||||
* RenderScript.
|
||||
*/
|
||||
class Cube3RS extends RenderScriptScene {
|
||||
|
||||
static class ThreeDPoint {
|
||||
public float x;
|
||||
public float y;
|
||||
public float z;
|
||||
}
|
||||
|
||||
static class ThreeDLine {
|
||||
int startPoint;
|
||||
int endPoint;
|
||||
}
|
||||
|
||||
static class WorldState {
|
||||
public float yRotation;
|
||||
public float mCenterX;
|
||||
public float mCenterY;
|
||||
}
|
||||
ThreeDPoint [] mOriginalPoints;
|
||||
ThreeDLine [] mLines;
|
||||
|
||||
WorldState mWorldState = new WorldState();
|
||||
private Type mStateType;
|
||||
private Allocation mState;
|
||||
|
||||
private SimpleMesh mCubeMesh;
|
||||
|
||||
private Allocation mPointAlloc;
|
||||
private float [] mPointData;
|
||||
|
||||
private Allocation mLineIdxAlloc;
|
||||
private short [] mIndexData;
|
||||
|
||||
private ProgramVertex mPVBackground;
|
||||
private ProgramVertex.MatrixAllocation mPVAlloc;
|
||||
|
||||
private int mWidth;
|
||||
private int mHeight;
|
||||
|
||||
private static final int RSID_STATE = 0;
|
||||
private static final int RSID_POINTS = 1;
|
||||
private static final int RSID_LINES = 2;
|
||||
private static final int RSID_PROGRAMVERTEX = 3;
|
||||
|
||||
|
||||
Cube3RS(int width, int height) {
|
||||
super(width, height);
|
||||
mWidth = width;
|
||||
mHeight = height;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resize(int width, int height) {
|
||||
super.resize(width, height);
|
||||
mWidth = width;
|
||||
mHeight = height;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ScriptC createScript() {
|
||||
|
||||
// Read the model in to our point/line objects
|
||||
readModel();
|
||||
|
||||
// Create a renderscript type from a java class. The specified name doesn't
|
||||
// really matter; the name by which we refer to the object in RenderScript
|
||||
// will be specified later.
|
||||
mStateType = Type.createFromClass(mRS, WorldState.class, 1, "WorldState");
|
||||
// Create an allocation from the type we just created.
|
||||
mState = Allocation.createTyped(mRS, mStateType);
|
||||
// set our java object as the data for the renderscript allocation
|
||||
mWorldState.yRotation = (-0.5f) * 2 * 180 / (float) Math.PI;
|
||||
mState.data(mWorldState);
|
||||
|
||||
/*
|
||||
* Now put our model in to a form that renderscript can work with:
|
||||
* - create a buffer of floats that are the coordinates for the points that define the cube
|
||||
* - create a buffer of integers that are the indices of the points that form lines
|
||||
* - combine the two in to a mesh
|
||||
*/
|
||||
|
||||
// First set up the coordinate system and such
|
||||
ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
|
||||
mPVBackground = pvb.create();
|
||||
mPVBackground.setName("PVBackground");
|
||||
mPVAlloc = new ProgramVertex.MatrixAllocation(mRS);
|
||||
mPVBackground.bindAllocation(mPVAlloc);
|
||||
mPVAlloc.setupProjectionNormalized(mWidth, mHeight);
|
||||
|
||||
// Start creating the mesh
|
||||
final SimpleMesh.Builder meshBuilder = new SimpleMesh.Builder(mRS);
|
||||
|
||||
// Create the Element for the points
|
||||
Builder elementBuilder = new Builder(mRS);
|
||||
// By specifying a prefix, even an empty one, the members will be accessible
|
||||
// in the renderscript. If we just called addFloatXYZ(), the members would be
|
||||
// unnamed in the renderscript struct definition.
|
||||
elementBuilder.addFloatXYZ("");
|
||||
final Element vertexElement = elementBuilder.create();
|
||||
final int vertexSlot = meshBuilder.addVertexType(vertexElement, mOriginalPoints.length);
|
||||
// Specify the type and number of indices we need. We'll allocate them later.
|
||||
meshBuilder.setIndexType(Element.INDEX_16(mRS), mLines.length * 2);
|
||||
// This will be a line mesh
|
||||
meshBuilder.setPrimitive(Primitive.LINE);
|
||||
|
||||
// Create the Allocation for the vertices
|
||||
mCubeMesh = meshBuilder.create();
|
||||
mCubeMesh.setName("CubeMesh");
|
||||
mPointAlloc = mCubeMesh.createVertexAllocation(vertexSlot);
|
||||
mPointAlloc.setName("PointBuffer");
|
||||
|
||||
// Create the Allocation for the indices
|
||||
mLineIdxAlloc = mCubeMesh.createIndexAllocation();
|
||||
|
||||
// Bind the allocations to the mesh
|
||||
mCubeMesh.bindVertexAllocation(mPointAlloc, 0);
|
||||
mCubeMesh.bindIndexAllocation(mLineIdxAlloc);
|
||||
|
||||
/*
|
||||
* put the vertex and index data in their respective buffers
|
||||
*/
|
||||
// one float each for x, y and z, and the 4th float will hold rgba
|
||||
mPointData = new float[mOriginalPoints.length * 3];
|
||||
for(int i = 0; i < mOriginalPoints.length; i ++) {
|
||||
mPointData[i*3] = mOriginalPoints[i].x;
|
||||
mPointData[i*3+1] = mOriginalPoints[i].y;
|
||||
mPointData[i*3+2] = mOriginalPoints[i].z;
|
||||
}
|
||||
mIndexData = new short[mLines.length * 2];
|
||||
for(int i = 0; i < mLines.length; i++) {
|
||||
mIndexData[i * 2] = (short)(mLines[i].startPoint);
|
||||
mIndexData[i * 2 + 1] = (short)(mLines[i].endPoint);
|
||||
}
|
||||
|
||||
/*
|
||||
* upload the vertex and index data
|
||||
*/
|
||||
mPointAlloc.data(mPointData);
|
||||
mPointAlloc.uploadToBufferObject();
|
||||
mLineIdxAlloc.data(mIndexData);
|
||||
mLineIdxAlloc.uploadToBufferObject();
|
||||
|
||||
// Time to create the script
|
||||
ScriptC.Builder sb = new ScriptC.Builder(mRS);
|
||||
// Specify the name by which to refer to the WorldState object in the
|
||||
// renderscript.
|
||||
sb.setType(mStateType, "State", RSID_STATE);
|
||||
sb.setType(mCubeMesh.getVertexType(0), "Points", RSID_POINTS);
|
||||
// this crashes when uncommented
|
||||
//sb.setType(mCubeMesh.getIndexType(), "Lines", RSID_LINES);
|
||||
|
||||
// Set the render script that will make use of the objects we defined above
|
||||
sb.setScript(mResources, R.raw.cube);
|
||||
sb.setRoot(true);
|
||||
|
||||
ScriptC script = sb.create();
|
||||
script.setClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
script.setTimeZone(TimeZone.getDefault().getID());
|
||||
|
||||
script.bindAllocation(mState, RSID_STATE);
|
||||
script.bindAllocation(mPointAlloc, RSID_POINTS);
|
||||
script.bindAllocation(mLineIdxAlloc, RSID_LINES);
|
||||
script.bindAllocation(mPVAlloc.mAlloc, RSID_PROGRAMVERTEX);
|
||||
|
||||
return script;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setOffset(float xOffset, float yOffset, int xPixels, int yPixels) {
|
||||
// update our state, then push it to the renderscript
|
||||
mWorldState.yRotation = (xOffset - 0.5f) * 2 * 180 / (float) Math.PI;
|
||||
mState.data(mWorldState);
|
||||
}
|
||||
|
||||
/*
|
||||
* Read the model definition from the resource.
|
||||
*/
|
||||
private void readModel() {
|
||||
|
||||
String [] p = mResources.getStringArray(R.array.cubepoints);
|
||||
int numpoints = p.length;
|
||||
mOriginalPoints = new ThreeDPoint[numpoints];
|
||||
|
||||
for (int i = 0; i < numpoints; i++) {
|
||||
mOriginalPoints[i] = new ThreeDPoint();
|
||||
String [] coord = p[i].split(" ");
|
||||
mOriginalPoints[i].x = Float.valueOf(coord[0]);
|
||||
mOriginalPoints[i].y = Float.valueOf(coord[1]);
|
||||
mOriginalPoints[i].z = Float.valueOf(coord[2]);
|
||||
}
|
||||
|
||||
String [] l = mResources.getStringArray(R.array.cubelines);
|
||||
int numlines = l.length;
|
||||
mLines = new ThreeDLine[numlines];
|
||||
|
||||
for (int i = 0; i < numlines; i++) {
|
||||
mLines[i] = new ThreeDLine();
|
||||
String [] idx = l[i].split(" ");
|
||||
mLines[i].startPoint = Integer.valueOf(idx[0]);
|
||||
mLines[i].endPoint = Integer.valueOf(idx[1]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* 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.android.livecubes.cube3;
|
||||
|
||||
import com.android.livecubes.RenderScriptWallpaper;
|
||||
|
||||
public class CubeWallpaper3 extends RenderScriptWallpaper<Cube3RS> {
|
||||
|
||||
@Override
|
||||
protected Cube3RS createScene(int width, int height) {
|
||||
return new Cube3RS(width, height);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* 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.android.livecubes;
|
||||
|
||||
import android.content.res.Resources;
|
||||
import android.renderscript.RenderScript;
|
||||
import android.renderscript.ScriptC;
|
||||
|
||||
public abstract class RenderScriptScene {
|
||||
protected int mWidth;
|
||||
protected int mHeight;
|
||||
protected boolean mPreview;
|
||||
protected Resources mResources;
|
||||
protected RenderScript mRS;
|
||||
protected ScriptC mScript;
|
||||
|
||||
public RenderScriptScene(int width, int height) {
|
||||
mWidth = width;
|
||||
mHeight = height;
|
||||
}
|
||||
|
||||
public void init(RenderScript rs, Resources res, boolean isPreview) {
|
||||
mRS = rs;
|
||||
mResources = res;
|
||||
mPreview = isPreview;
|
||||
mScript = createScript();
|
||||
}
|
||||
|
||||
public boolean isPreview() {
|
||||
return mPreview;
|
||||
}
|
||||
|
||||
public int getWidth() {
|
||||
return mWidth;
|
||||
}
|
||||
|
||||
public int getHeight() {
|
||||
return mHeight;
|
||||
}
|
||||
|
||||
public Resources getResources() {
|
||||
return mResources;
|
||||
}
|
||||
|
||||
public RenderScript getRS() {
|
||||
return mRS;
|
||||
}
|
||||
|
||||
public ScriptC getScript() {
|
||||
return mScript;
|
||||
}
|
||||
|
||||
protected abstract ScriptC createScript();
|
||||
|
||||
public void stop() {
|
||||
mRS.contextBindRootScript(null);
|
||||
}
|
||||
|
||||
public void start() {
|
||||
mRS.contextBindRootScript(mScript);
|
||||
}
|
||||
|
||||
public void resize(int width, int height) {
|
||||
mWidth = width;
|
||||
mHeight = height;
|
||||
}
|
||||
|
||||
@SuppressWarnings({"UnusedDeclaration"})
|
||||
public void setOffset(float xOffset, float yOffset, int xPixels, int yPixels) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* 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.android.livecubes;
|
||||
|
||||
import android.service.wallpaper.WallpaperService;
|
||||
import android.renderscript.RenderScript;
|
||||
import android.view.SurfaceHolder;
|
||||
import android.view.Surface;
|
||||
|
||||
public abstract class RenderScriptWallpaper<T extends RenderScriptScene> extends WallpaperService {
|
||||
public Engine onCreateEngine() {
|
||||
return new RenderScriptEngine();
|
||||
}
|
||||
|
||||
protected abstract T createScene(int width, int height);
|
||||
|
||||
private class RenderScriptEngine extends Engine {
|
||||
private RenderScript mRs;
|
||||
private T mRenderer;
|
||||
|
||||
@Override
|
||||
public void onCreate(SurfaceHolder surfaceHolder) {
|
||||
super.onCreate(surfaceHolder);
|
||||
setTouchEventsEnabled(false);
|
||||
surfaceHolder.setSizeFromLayout();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
destroyRenderer();
|
||||
}
|
||||
|
||||
private void destroyRenderer() {
|
||||
if (mRenderer != null) {
|
||||
mRenderer.stop();
|
||||
mRenderer = null;
|
||||
}
|
||||
if (mRs != null) {
|
||||
mRs.destroy();
|
||||
mRs = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onVisibilityChanged(boolean visible) {
|
||||
super.onVisibilityChanged(visible);
|
||||
if (mRenderer != null) {
|
||||
if (visible) {
|
||||
mRenderer.start();
|
||||
} else {
|
||||
mRenderer.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
|
||||
super.onSurfaceChanged(holder, format, width, height);
|
||||
if (mRenderer == null) {
|
||||
mRenderer = createScene(width, height);
|
||||
mRenderer.init(mRs, getResources(), isPreview());
|
||||
mRenderer.start();
|
||||
} else {
|
||||
mRenderer.resize(width, height);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onOffsetsChanged(float xOffset, float yOffset, int xPixels, int yPixels) {
|
||||
mRenderer.setOffset(xOffset, yOffset, xPixels, yPixels);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSurfaceCreated(SurfaceHolder holder) {
|
||||
super.onSurfaceCreated(holder);
|
||||
|
||||
Surface surface = null;
|
||||
while (surface == null) {
|
||||
surface = holder.getSurface();
|
||||
}
|
||||
mRs = new RenderScript(surface, false, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSurfaceDestroyed(SurfaceHolder holder) {
|
||||
super.onSurfaceDestroyed(holder);
|
||||
destroyRenderer();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user