2012-01-07 204 views
1

我创建一个瓷砖的棋盘游戏。 我想旋转一个位图瓷砖块几个预先确定的程度。
当我旋转我的位图时,大小改变。
例如,如果我想要一个75x75三角瓦块,对旋转我得到一个68x68从这个代码回来。我怎样才能保持相同的尺寸,所以一切都保持在董事会的规模?旋转位并保持其大小

下面是我使用的旋转什么:

public class RotatebitmapActivity extends Activity { 
@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    LinearLayout linLayout = new LinearLayout(this); 

    // load the origial BitMap (500 x 500 px) 
    Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), 
      R.drawable.t123); 

    int width = bitmapOrg.getWidth(); 
    int height = bitmapOrg.getHeight(); 
    int newWidth = 75; 
    int newHeight = 75; 

    // calculate the scale - in this case = 0.4f 
    float scaleWidth = ((float) newWidth)/width; 
    float scaleHeight = ((float) newHeight)/height; 

    // createa matrix for the manipulation 
    Matrix matrix = new Matrix(); 
    // resize the bit map 
    matrix.postScale(scaleWidth, scaleHeight); 
    // rotate the Bitmap 
    matrix.postRotate(120); 

    // 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 
    BitmapDrawable bmd = new BitmapDrawable(resizedBitmap); 

    ImageView imageView = new ImageView(this); 

    // set the Drawable on the ImageView 
    imageView.setImageDrawable(bmd); 

    // center the Image 
    imageView.setScaleType(ScaleType.CENTER); 

    // add ImageView to the Layout 
    linLayout.addView(imageView, 
     new LinearLayout.LayoutParams(
        LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT 
      ) 
    ); 

    // set LinearLayout as ContentView 
    setContentView(linLayout); 
} 
+0

检查我的答案[这里] [1],workd没有问题 [1]:http://stackoverflow.com/questions/8712652/rotating-image-on-a-canvas-in -android/28293393#28293393 – MSaudi 2015-02-03 07:23:28

回答

1

什么是你的代码发生的事情,是创建的位图的尺寸是否合适。但是,所有Bitmaps会自动缩放以适应当前正在使用的屏幕密度。

有两种方式(即我所知道的),以得到你想要的结果。

  1. 您可以在创建位图之后并在将其包装到绘图之前设置位图的密度。要做到这一点,添加类似代码:

    resizedBitmap.setDensity(DisplayMetric.DENSITY_HIGH);

    这个代码设置该位图被设计为密度,并且可以防止从Android的自动缩放的图像。

  2. 第二种解决方案是超过一个特定的解决方案,这个特定的问题的方法的。我不细讲,对于看Supporting Multiple Screens | Android Developers

希望有人会发生沿,可以回答你的问题不是我能更好。

+0

没有一点区别。无论SetMinimums是否被使用,imageView仍然是68x68。 FIT_XY只是将整个imageView缩放到全屏 – Martin 2012-01-07 05:20:30

+0

它与ImageView无关 - 它是BitMap。当位图被调整大小和旋转时,它不是正确的大小。在ImageView上使用包装内容将仅适用于其当前(不正确)尺寸。 – Martin 2012-01-08 01:33:51

+0

例如,如果我有一个150x150位图,希望它缩小到75x75并旋转,在上面的代码中,resizedBitmap从102x102处的createBitmap出来。然后创建新的BitmapDrawable,bmd,它是68x68。我无法弄清楚如何旋转一个对象,并保持瓷砖相同的大小 – Martin 2012-01-08 01:36:47