Sunday 30 September 2012

Android-Rotate an image

Rotateimage.java


package com.example.gallery;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import android.graphics.Matrix;

import android.view.Display;
import android.widget.ImageView;

public class Rotateimage extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gallery);
              // or just load a resource from the res/drawable directory:
              Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.flo);
               
              // find the width and height of the screen:
              Display d = getWindowManager().getDefaultDisplay();
              int x = d.getWidth();
              int y = d.getHeight();
               
              // get a reference to the ImageView component that will display the image:
              ImageView img1 = (ImageView)findViewById(R.id.imageView1);
               
              // scale it to fit the screen, x and y swapped because my image is wider than it is tall
              Bitmap scaledBitmap = Bitmap.createScaledBitmap(myBitmap, y, x, true);
               
              // create a matrix object
              Matrix matrix = new Matrix();
             
              matrix.postRotate(45, 90, 180);
               
              // create a new bitmap from the original using the matrix to transform the result
              Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap , 0, 0, scaledBitmap .getWidth(), scaledBitmap .getHeight(), matrix, true);
               
              // display the rotated bitmap
              img1.setImageBitmap(rotatedBitmap);
}
}




main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/flo"
        
        />

</RelativeLayout> 






Screenshot.






No comments:

Post a Comment