如何在Android中旋转具有矩形形状的Drawable?

时间:2013-02-02 16:11:21

标签: android

我使用此代码将可绘制的45度旋转:

public static Drawable rotateImage(Context ctx) {
      // load the origial Bitmap
      Bitmap BitmapOrg = BitmapFactory.decodeResource(ctx.getResources(),
              R.drawable.car);



      int width = BitmapOrg.getWidth();
      int height = BitmapOrg.getHeight();
      int newWidth = 90;
      int newHeight = newWidth * height / width;

      // calculate the scale
      float scaleWidth = ((float) newWidth) / width;
      float scaleHeight = ((float) newHeight) / height;

      // create a matrix for the manipulation
      Matrix matrix = new Matrix();
      // resize the Bitmap
      matrix.postScale(scaleWidth, scaleHeight);
      // if you want to rotate the Bitmap
      matrix.postRotate(-45);

      // recreate the new Bitmap
      Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0,
                                                 width, height, matrix, true);

      // make a Drawable from Bitmap to allow to set the Bitmap
      // to the ImageView, ImageButton or what ever
      return new BitmapDrawable(resizedBitmap);

    }

问题是虽然我给它一个newWidth,但Drawable的大小仍然相同,旋转的图像显示为拉伸。如果它是一个矩形,可以旋转drawable,以获得旋转图像的良好外观?

1 个答案:

答案 0 :(得分:0)

我试过你哥们:)我认为你在xml中设置的图像尺寸很小。所以,任何图像都会设置为小。只需尝试以下内容......

在您的XML

   <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ic_launcher" >

  <ImageView
    android:id="@+id/imageButton1"
    android:layout_width="1000dp"
    android:layout_height="5000dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="49dp"
    android:src="@drawable/ic_launcher"
    android:contentDescription="Rakesh" />

</RelativeLayout>

并在您的Java文件中

 public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   /* this.requestWindowFeature(Window.FEATURE_NO_TITLE); */
    setContentView(R.layout.activity_main);
    ImageView im=(ImageView)findViewById(R.id.imageButton1);
  /*  this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);*/
im.setImageDrawable(rotateImage(this));
}

public static Drawable rotateImage(Context ctx) {
    // load the origial Bitmap
    Bitmap BitmapOrg = BitmapFactory.decodeResource(ctx.getResources(),
            R.drawable.vijay);



    int width = BitmapOrg.getWidth();
    int height = BitmapOrg.getHeight();
    int newWidth = 90;
    int newHeight = newWidth * height / width;

    // calculate the scale
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;

    // create a matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the Bitmap
    matrix.postScale(scaleWidth, scaleHeight);
    // if you want to rotate the Bitmap
    matrix.postRotate(-45);

    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0,
                                               width, height, matrix, true);

    // make a Drawable from Bitmap to allow to set the Bitmap
    // to the ImageView, ImageButton or what ever
    return new BitmapDrawable(resizedBitmap);

  }
  }

现在你将获得你的大小的图像:)

如果有任何错误还原我..