ImageView旋转基于加速度计

时间:2014-12-06 02:27:24

标签: android android-layout android-widget android-imageview

我正在制作一个使用Android手机的加速度计的应用程序,我目前将x值设置为变量mX(限制在0-10之间)现在我有一个ImageView小部件我想要向左旋转如果mX低于5并且如果mX高于5则向右旋转并且当mX为5时重置为水平。我的问题是我尝试使用动画,矩阵但它们不起作用。请提前帮助和谢谢。

2 个答案:

答案 0 :(得分: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;
}

}

答案 1 :(得分: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;
    }

}

在本课程中,&#34;目标&#34;是根据视口移动的图像。在&#34; setViewPort&#34;调用(x和y值从-1到1),视图无效,onDraw()方法将使用偏移量绘制图像。