转位并保持其大小 [英] rotate bitmap and maintain size

查看:164
本文介绍了转位并保持其大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个瓷砖的棋盘游戏。 我想旋转一个位图平铺一块几pre确定的程度。
当我转动我的位图,大小的变化。
例如,如果我想要一个75x75三角形瓷砖片,在旋转我得到一个68x68从这个code回来。我怎样才能让事情的大小相同,所以一切仍是主板的尺寸?

下面是我用什么来旋转:

 公共类RotatebitmapActivity延伸活动{
@覆盖
公共无效的onCreate(包冰柱){
    super.onCreate(冰柱);
    的LinearLayout linLayout =新的LinearLayout(本);

    //加载origial位图(500×500像素)
    位图bitmapOrg = BitmapFactory.de codeResource(getResources()
           R.drawable.t123);

    INT宽度= bitmapOrg.getWidth();
    INT高= bitmapOrg.getHeight();
    INT newWidth = 75;
    INT newHeight = 75;

    //计算比例 - 在这种情况下= 0.4f
    浮动scaleWidth =((浮点)newWidth)/宽度;
    浮动scaleHeight =((浮点)newHeight)/身高;

    // createa矩阵用于操作
    字模=新的Matrix();
    //调整位图
    matrix.postScale(scaleWidth,scaleHeight);
    //旋转位图
    matrix.postRotate(120);

    //重新创建新的位图
    位图resizedBitmap = Bitmap.createBitmap(bitmapOrg,0,0,
                      宽度,高度,矩阵,真);


    //使从位图被拉伸以允许设置位图
    //为ImageView的,ImageButton的或什么都
    BitmapDrawable BMD =新BitmapDrawable(resizedBitmap);

    ImageView的ImageView的=新ImageView的(这一点);

    //设置绘制对象上的ImageView
    imageView.setImageDrawable(BMD);

    //中心图片
    imageView.setScaleType(ScaleType.CENTER);

    //添加的ImageView的布局
    linLayout.addView(ImageView的,
        新LinearLayout.LayoutParams(
                  LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT
            )
    );

    //设置的LinearLayout为内容查看
    的setContentView(linLayout);
}
 

解决方案

什么是发生在你的code,是创建的位图是正确的大小。然而,所有的位图自动缩放到适合屏幕上当前正在使用的密度。

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

  1. 您可以创建后,并包裹在一个绘制之前设置位图的密度。要做到这一点,添加code类似于:

    resizedBitmap.setDensity(DisplayMetric.DENSITY_HIGH);

    这code设置位图是专为密度,并能prevent的Andr​​oid从自动缩放图像。

  2. 第二种溶液是多一个比一个具体的解决这个问题的特定方法论。我不会去详细地说,即见支持多个屏幕| Android开发者

希望有人将沿着这一目标可以回答你的问题比我好的人。

I am creating a tile board game. I want to rotate a bitmap tile piece by a few pre-determined degrees.
When I rotate my bitmap, the size changes.
For example, if I want a 75x75 triangle tile piece, on rotation I get a 68x68 back from this code. How can I keep things the same size so everything remains the size for the board?

Here's what I'm using to rotate:

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

解决方案

What is happening in your code, is that the bitmap that is created is the correct size. However, all Bitmaps are automatically scaled to fit the density of the screen that is currently in use.

There are two ways (that I know of) to get the result you desire.

  1. You can set the density of a bitmap after creating it and before wrapping it in a drawable. To do this, add code similar to:

    resizedBitmap.setDensity(DisplayMetric.DENSITY_HIGH);

    This code sets the density that the bitmap is designed for, and can prevent Android from auto-scaling the image.

  2. The second solution is more of a methodology than a specific solution to this particular problem. I won't go into detail, for that see Supporting Multiple Screens | Android Developers

Hopefully someone will happen along that can answer your question better than I can.

这篇关于转位并保持其大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆