0

i want to rotate my bitmap img which is a car steering using one axis of accelerometer. I have no idea how i can use it with matrix or calling the sensorchanged method in other class. My app is in landscape mode and i have two classes one for sensor and one for bitmap. I am completely lost.

    public class CustomDrawableView extends View {

    AnimationSteeringActivity sens = new AnimationSteeringActivity();

    public CustomDrawableView(Context context) {
        // TODO Auto-generated constructor stub

        super(context);

    }

    @Override
    public void onDraw(Canvas canvas) {
        Bitmap bmp = BitmapFactory.decodeResource(getResources(),
                R.drawable.steering);

        int canvasWidth = canvas.getWidth();
        int canvasHeight = canvas.getHeight();
        int bitmapWidth = bmp.getWidth();
        int bitmapHeight = bmp.getHeight();

        int centreX = (canvasWidth - bitmapWidth) / 2;

        int centreY = (canvasWidth - bitmapHeight) / 2;

        canvas.drawColor(Color.WHITE);


        canvas.drawBitmap(bmp, centreX, centreY, null);

and

public class AnimationSteeringActivity extends Activity implements
    SensorEventListener {
/** Called when the activity is first created. */

/** Called when the activity is first created. */
CustomDrawableView mCustomDrawableView = null;
ShapeDrawable mDrawable = new ShapeDrawable();
public static int x;
public static int y;

private SensorManager sensorManager = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    // Get a reference to a SensorManager
    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    mCustomDrawableView = new CustomDrawableView(this);
    setContentView(mCustomDrawableView);
    // setContentView(R.layout.main);

}

// This method will update the UI on new sensor events
public void onSensorChanged(SensorEvent sensorEvent) {
    {
        if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            // the values you were calculating originally here were over
            // 10000!
            x = (int) Math.pow(sensorEvent.values[1], 2);
            y = (int) Math.pow(sensorEvent.values[2], 2);

        }

        if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) {

        }
    }
}
user1422066
  • 29
  • 1
  • 7

2 Answers2

2

Look for doing actually rotation at this link

rotating a bitmap

so in the onSensorChanged method there you will rotate with the wanted x or y value depending on what you want

QVDev
  • 1,093
  • 10
  • 17
  • I know how to do this with matrix but i dont know how to pass the x and y from the sensor class – user1422066 Jun 03 '12 at 09:00
  • get the right value from the onsensorchanged and put it in matrix.postRotate(-90); // anti-clockwise by 90 where it says 90 – QVDev Jun 03 '12 at 16:33
  • how to put the sensor value in the other class with matrix i cant figure out please look at my code. – user1422066 Jun 03 '12 at 18:56
  • make a variable in youre custom drawable with public getter and setter this will then replace the rotation value mentioned above. then in youre sensorchanged call mdrawable.setRotation(value); and then force the view ro be updated. – QVDev Jun 03 '12 at 21:26
  • Thanks its done but there is a problem with the cordinate system – user1422066 Jun 04 '12 at 11:35
  • What do you mean with the coordinate system? Can you give a more detailed description? Maybe screenshot or tell me what is working not and what not? – QVDev Jun 04 '12 at 12:30
  • ok i will meanwhile can you answer this please? http://stackoverflow.com/questions/10824867/android-executing-the-thread-in-a-loop-with-ontouchlistener – user1422066 Jun 04 '12 at 18:11
  • try executing the code you will see the problem. My bitmap is a png image of a car steering i want to rotate is in realtime when the sensor is changed on Y axis i.s is left or right. But using this code the bitmap is redrawn on random positions. – user1422066 Jun 09 '12 at 18:17
  • If it appears on different positions, you probably setting it the x y value wrong not at the rotation level: matrix.postRotate(-90); // anti-clockwise by 90 degrees – QVDev Jun 11 '12 at 11:51
  • The problem i am also having is i dont know what the sensor is giving me the value can u tell me what values are returned on sensor change so i can use is in If Statments – user1422066 Jun 11 '12 at 12:03
  • http://developer.android.com/reference/android/hardware/SensorEvent.html Look at the website there is an example!!! read careful and debug it with: `Log.d("Debug","print values and do what is right for you")` – QVDev Jun 11 '12 at 13:25
0
Bitmap myBitmap = BitmapFactory.decodeStream(downloadImageFromWeb());

Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.android1);

Display d = getWindowManager().getDefaultDisplay();

int x = d.getWidth();

int y = d.getHeight();

ImageView img1 = (ImageView)findViewById(R.id.img1);

Bitmap scaledBitmap = Bitmap.createScaledBitmap(myBitmap, y, x, true);

Matrix matrix = new Matrix();

matrix.postRotate(-90);

Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap , 0, 0, scaledBitmap .getWidth(), scaledBitmap .getHeight(), matrix, true);


img1.setImageBitmap(rotatedBitmap);
Bassetassen
  • 20,852
  • 8
  • 39
  • 40