1

我正在使用此代码将可绘制对象旋转 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的大小还是一样的,旋转的图像看起来被拉伸了。如果它是一个矩形,我如何旋转可绘制对象以获得旋转图像的良好外观?

4

1 回答 1

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);

  }
  }

现在你会得到你尺寸的图片:)

如果有任何错误让我恢复..

于 2013-02-02T17:38:36.523 回答