Android图片旋转

Android中,我们可以使用矩阵实现图像旋转

首先,创建一个布局xml文件:

<?xml version="1.0" encoding="utf-8"?><br />
<LinearLayout android:id="@+id/LinearLayout01"<br />
android:layout_width="fill_parent"<br />
android:layout_height="fill_parent"<br />
xmlns:android="http://schemas.android.com/apk/res/android"<br />
android:background="#ffffff"<br />
android:gravity="center"><br />
<ImageView android:id="@+id/ImageView01"<br />
android:layout_width="wrap_content"<br />
android:layout_height="wrap_content"<br />
android:src="@drawable/refresh" /><br />
</LinearLayout><br />



创建主Activity类文件:

public class ExampleApp extends Activity
{
private ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img=(ImageView)findViewById(R.id.ImageView01);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.refresh);
// Getting width & height of the given image.
int w = bmp.getWidth();
int h = bmp.getHeight();
// Setting post rotate to 90
Matrix mtx = new Matrix();
mtx.postRotate(90);
// Rotating Bitmap
Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, w, h, mtx, true);
BitmapDrawable bmd = new BitmapDrawable(rotatedBMP);
img.setImageDrawable(bmd);
}
}


运行结果如下所示:(第一张是图像旋转之前,第二张是图像旋转之后)



你可能感兴趣的:(android,xml,layout,Class,Matrix,encoding)