在尝试了所有我认为我会尝试看看这里是否有人知道我的问题是什么之后,在我完全放弃之前。所以我想做的是让我的图像在这个“旋转立方体”后面滑动。例如,如果我将手机向右倾斜,它就会开始向右滑动,反之亦然。
这是代码。对于这个问题,它很长并且有很多毫无意义的代码,但我想给它完整的,忽略不重要的部分。它是 sdk 中提供的 CubeWallpaper1 示例。我刚刚对其进行了修改并尝试为其添加加速度计并使其工作。
因此,如果您可以看一下并告诉我它有什么问题。
public class LiveWall extends WallpaperService implements SensorEventListener {
float xAxis,yAxis,zAxis;
private SensorManager mSensorManager;
private Sensor mAccelerometer;
Bitmap drawable;
private final Handler mHandler = new Handler();
@Override
public void onCreate() {
super.onCreate();
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
drawable = BitmapFactory.decodeResource(getResources(),R.drawable.bg);
mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
}
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
public void onSensorChanged(SensorEvent event) {
xAxis = event.values[0];
yAxis = event.values[1];
zAxis = event.values[2];
}
@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(Color.GREEN);
paint.setAntiAlias(true);
paint.setStrokeWidth(15);
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,
float xStep, float yStep, 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);
c.drawBitmap(drawable, 0+xAxis,0+yAxis, mPaint);
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);
}
}
}
}