2013-02-02 78 views
1

I'm使用此代码以旋转抽拉45度:如何在Android中旋转矩形形状的Drawable?

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?

回答

0

好友:)我想你已经设置XML格式的图像尺寸为small.so,任何图像将被设置为小。只是试试下面的事情我想你...

在你的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); 

    } 
    } 

现在日子会把你让你的形象在你的尺寸:)

如果有什么错误还原我..

+0

问题是我没有.xml布局。我正在开发一款游戏,一切都是通过编程完成的。 –