[英]How to rotate a Drawable with rectangle shape in 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的大小仍然是相同的,並且旋轉的圖像看起來是拉伸的。 如果它是一個矩形,如何旋轉可繪制對象以獲得良好外觀的旋轉圖像?
我為您嘗試過伙伴:)我想您已將xml中的圖片大小設置為small.so,因此任何圖片都將設置為small。只需嘗試以下操作...
在您的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);
}
}
現在,您將以自己的尺寸獲取您的圖片:)
如果有任何錯誤,請回復我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.