Android图片旋转

来源:互联网 发布:国学大师txt数据库 编辑:程序博客网 时间:2024/05/17 23:55

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

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

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?><br />  
  2. <LinearLayout android:id="@+id/LinearLayout01"<br />  
  3. android:layout_width="fill_parent"<br />  
  4. android:layout_height="fill_parent"<br />  
  5. xmlns:android="http://schemas.android.com/apk/res/android"<br />  
  6. android:background="#ffffff"<br />  
  7. android:gravity="center"><br />  
  8. <ImageView android:id="@+id/ImageView01"<br />  
  9. android:layout_width="wrap_content"<br />  
  10. android:layout_height="wrap_content"<br />  
  11. android:src="@drawable/refresh" /><br />  
  12. </LinearLayout><br />  



创建主Activity类文件:

[java] view plaincopy
  1. public class ExampleApp extends Activity  
  2. {  
  3. private ImageView img;  
  4. @Override  
  5. protected void onCreate(Bundle savedInstanceState) {  
  6. super.onCreate(savedInstanceState);  
  7. setContentView(R.layout.main);  
  8. img=(ImageView)findViewById(R.id.ImageView01);  
  9. Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.refresh);  
  10. // Getting width & height of the given image.  
  11. int w = bmp.getWidth();  
  12. int h = bmp.getHeight();  
  13. // Setting post rotate to 90  
  14. Matrix mtx = new Matrix();  
  15. mtx.postRotate(90);  
  16. // Rotating Bitmap  
  17. Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 00, w, h, mtx, true);  
  18. BitmapDrawable bmd = new BitmapDrawable(rotatedBMP);  
  19. img.setImageDrawable(bmd);  
  20. }  
  21. }  
0 0