Android Image Rotate Example

来源:互联网 发布:轴承型号查询软件 编辑:程序博客网 时间:2024/05/17 13:41

In android, we can rotate the image using matrix post rotate.
Example for Android Image Rotate :-

01<?xml version="1.0" encoding="utf-8"?>
02<LinearLayout android:id="@+id/LinearLayout01"
03android:layout_width="fill_parent"
04android:layout_height="fill_parent"
05xmlns:android="http://schemas.android.com/apk/res/android"
06android:background="#ffffff"
07android:gravity="center">
08<ImageView android:id="@+id/ImageView01"
09android:layout_width="wrap_content"
10android:layout_height="wrap_content"
11android:src="@drawable/refresh" />
12</LinearLayout>
01public class ExampleApp extends Activity
02{
03    private ImageView img;
04 
05    @Override
06    protected void onCreate(Bundle savedInstanceState) {
07        super.onCreate(savedInstanceState);
08        setContentView(R.layout.main);
09 
10        img=(ImageView)findViewById(R.id.ImageView01);
11        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.refresh);
12               // Getting width & height of the given image.
13        int w = bmp.getWidth();
14        int h = bmp.getHeight();
15               // Setting post rotate to 90
16        Matrix mtx = new Matrix();
17        mtx.postRotate(90);
18                // Rotating Bitmap
19        Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 00, w, h, mtx, true);
20        BitmapDrawable bmd = new BitmapDrawable(rotatedBMP);
21 
22        img.setImageDrawable(bmd);
23 
24    }
25 
26}

The output will looks like (1st image is Before rotating the image, 2nd image is After rotating the image)
imagerotatebeforeimagerotateafter