我正在製作一個使用android手機加速計的應用程序,我現在將x值設置爲變量mX(即從0-10限制)現在我有一個ImageView窗口小部件如果mX小於5則向左旋轉,如果mX大於5則向右旋轉並在mX爲5時重置爲水平。我的問題是我嘗試過使用動畫,矩陣但它們不起作用。請提前幫助和感謝。ImageView基於關閉加速度計的旋轉
0
A
回答
0
享受此代碼。如果有幫助,讓我知道:
public int getDeviceDefaultOrientation() {
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
Configuration config = getResources().getConfiguration();
int rotation = windowManager.getDefaultDisplay().getRotation();
if (((rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) &&
config.orientation == Configuration.ORIENTATION_LANDSCAPE)
|| ((rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) &&
config.orientation == Configuration.ORIENTATION_PORTRAIT)) {
return Configuration.ORIENTATION_LANDSCAPE;
} else {
return Configuration.ORIENTATION_PORTRAIT;
}
}
+0
對不起,我應該讓自己更清楚,當mX超過5時,我有一個圖像需要旋轉到30度。 – user4330907 2014-12-06 03:18:26
+0
更好地在您的問題中添加代碼以獲得更清晰。 – VicJordan 2014-12-06 03:21:23
0
我有一個類似的例子
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
public class SkySurfaceView extends SurfaceView implements OnTouchListener {
private static final String TAG = SkySurfaceView.class.getSimpleName();
private double viewPortX, viewPortY;
private int starX, starY;
private Bitmap target;
private int targetWidth, targetHeight;
private int vpWidth, vpHeight;
private Matrix skyMatrix, skyMatrix2;
private Matrix skyMatrix3;
private Bitmap star;
private Paint paint;
private Rect touchRect, touchRect2, touchRect3;
public SkySurfaceView(Context context, AttributeSet attr) {
super(context, attr);
viewPortX = viewPortY = 0;
skyMatrix = new Matrix();
skyMatrix2 = new Matrix();
skyMatrix3 = new Matrix();
paint = new Paint();
paint.setStrokeWidth(1);
paint.setDither(true);
paint.setColor(Color.RED);
Options opt = new Options();
opt.inSampleSize = 2;
opt.inScaled = false;
opt.inPreferredConfig = Config.RGB_565;
star = BitmapFactory.decodeResource(getResources(), R.drawable.star,
opt);
touchRect = new Rect(0, 0, star.getWidth(), star.getHeight());
touchRect2 = new Rect(0, 0, star.getWidth(), star.getHeight());
touchRect3 = new Rect(0, 0, star.getWidth(), star.getHeight());
this.setWillNotDraw(true);
setLayerType(View.LAYER_TYPE_HARDWARE, null);
this.setOnTouchListener(this);
getHolder().setFormat(PixelFormat.RGB_565);
}
public void setTarget(Bitmap b) {
target = b;
targetHeight = target.getHeight();
targetWidth = target.getWidth();
}
public void init() {
this.starX = (int) (vpWidth * Math.random()) + vpWidth;
this.starY = (int) ((vpHeight - star.getHeight()) * Math.random());
Log.i(TAG, "drawn star on " + starX + "," + starY);
Canvas c = new Canvas();
Log.i(TAG,
"target dimension is " + target.getWidth() + "x"
+ target.getHeight());
Bitmap bitmap = Bitmap.createBitmap(target.getWidth(),
target.getHeight(), Config.RGB_565);
c.setBitmap(bitmap);
c.drawBitmap(target, 0, 0, paint);
c.drawBitmap(star, starX, starY, paint);
c.drawBitmap(star, starX - targetWidth, starY, paint);
target.recycle();
setTarget(bitmap);
setWillNotDraw(false);
}
@Override
public boolean performClick() {
super.performClick();
return false;
}
/**
*
* @param x
* - [-1:1]
* @param y
* - [-1:1]
*/
public void setViewPort(double x, double y) {
viewPortX = x;
viewPortY = y;
int tempX = (int) (targetWidth * (viewPortX));
int tempY = (int) (targetHeight * (viewPortY - 1));
tempY = Math.max(tempY, -(targetHeight - vpHeight)/2);
tempY = Math.min(tempY, +(targetHeight - vpHeight/2));
Log.i(TAG,
String.format("%d %d , %d %d, %d %d", tempX, tempY, tempX
- targetWidth, tempY, tempX + targetWidth, tempY));
skyMatrix.reset();
skyMatrix.postTranslate(tempX, tempY);
skyMatrix2.reset();
skyMatrix2.postTranslate(tempX - targetWidth, tempY);
skyMatrix3.reset();
skyMatrix3.postTranslate(tempX + targetWidth, tempY);
int xx = (tempX + starX);
while (xx < targetWidth) {
xx += targetWidth;
}
touchRect.offsetTo(xx % targetWidth, (tempY + starY) % targetHeight);
touchRect2.offsetTo(xx % targetWidth - targetWidth, (tempY + starY)
% targetHeight);
touchRect3.offsetTo(xx % targetWidth + targetWidth, (tempY + starY)
% targetHeight);
postInvalidate();
}
public void setViewportSize(int x, int y) {
vpWidth = x;
vpHeight = y;
}
@Override
protected void onDraw(Canvas c) {
super.onDraw(c);
c.drawBitmap(target, skyMatrix, paint);
c.drawBitmap(target, skyMatrix2, paint);
c.drawBitmap(target, skyMatrix3, paint);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
float ex = event.getX();
float ey = event.getY();
Log.i(TAG, "touched " + ex + " " + ey);
if (touchRect.contains((int) ex, (int) ey)) {
if (listener != null) {
listener.onCaptured();
}
} else if (touchRect2.contains((int) ex, (int) ey)) {
if (listener != null) {
listener.onCaptured();
}
} else if (touchRect3.contains((int) ex, (int) ey)) {
if (listener != null) {
listener.onCaptured();
}
}
return false;
}
public void setListener(OnStarCaptureListener listener) {
this.listener = listener;
}
}
在這個類中,「目標」是根據視移動的圖像。當調用「setViewPort」(x和y值從-1到1)時,視圖無效,onDraw()方法將繪製帶偏移量的圖像。
相關問題
- 1. 用於加速度計轉換的Android旋轉矩陣
- 2. Cocos2d:基於加速度計的動畫
- 3. 基於加速度計的移動
- 4. 基於加速度計值的Android速度
- 5. 速度計中的箭頭旋轉
- 6. 使用加速度計計算旋轉矩陣
- 7. 以3D旋轉方式轉換加速度計
- 8. 基於他的速度通知用戶一個關閉區域
- 9. 計算旋轉速度和自然旋轉視圖iOS
- 10. 旋轉ImageView的
- 11. Android:如何使用加速度計旋轉我的位圖?
- 12. 四旋翼加速度計不穩定
- 13. 迷宮遊戲中的相機旋轉取決於加速度計
- 14. 用numba加速旋轉矩陣計算
- 15. 關於旋轉的進度不見android
- 16. 加速和旋轉
- 17. 使用加速度計在cocos2d中旋轉精靈
- 18. 加速度計檢測和精靈旋轉
- 19. 使用加速度計時旋轉設備
- 20. Cocos2d iPhone:使用加速度計旋轉Sprite
- 21. iphone加速度計:問題旋轉圖像
- 22. 檢測iPhone已經使用phonegap旋轉(加速度計)
- 23. 使用加速度計在精靈旋轉時彈出精靈
- 24. 基於旋轉角度計算X Y移動?
- 25. 旋轉動畫速度
- 26. 度數轉換數據加速度計
- 27. ImageView不旋轉
- 28. 旋轉ImageView
- 29. iOS4 iPhone關閉屏幕,保持加速度計
- 30. 關閉iPhone加速度計,但不使用它
嘗試view.setRotation,查看setRotationX和view.setRotationY。 – rupps 2014-12-06 03:01:37