Android:从位图中删除一部分并进行缩放

时间:2013-01-05 23:25:53

标签: android bitmap textview android-canvas

首先我要说的是,我真的读了很多,但我没有找到这个问题......

目前我的代码运行良好。我有一个位图,然后我剪切了一部分位图并绘制了它。

this.card = new ImageView(this); //this is my ImageView
//and my bitmap...
this.bmCards = BitmapFactory.decodeResource(getResources(), R.drawable.cardgame);

//and here I set the bitmap as Image of this ImageView
this.card.setImageBitmap(getCard(Stapel.getCard()));

调用的方法如下:

private Bitmap getCard(Card c) {

    //some arithmetic to calculate the size...

    Bitmap result = Bitmap.createBitmap(cardW, cardH, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(result);
    canvas.drawBitmap(bmCards, new Rect(x, y, x + cardW + toleranzW, y + cardH + toleranzH), 
            new Rect(0, 0, cardW, cardH), new Paint());

    return result;
}

到目前为止它的工作原理..但我怎么能调整这个图像的大小? 我的意思是在画布上我绘制了Bitmap的右边部分,但我想稍微缩放这部分,因为它太小了......可能是实际的H * 1,5和实际的W * 1,5或者其他东西。

2 个答案:

答案 0 :(得分:3)

我认为你可以使用它:

Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
resizedBitmap = Bitmap.createBitmap(originalBitmap, 0, 0, originalBitmap.getWidth(),
                originalBitmap.getHeight(), matrix, false);

并且在获得调整大小的位图后,您可以在画布上绘制它..

修改

请试试这个......我没试过......你应该像我在评论中写的那样玩你的价值观......你还应该阅读createBitmap()postScale()文件要了解你在那里做了什么:)

private Bitmap getCard(Card c) {

    //some arithmetic to calculate the size...

    Bitmap result = Bitmap.createBitmap(cardW, cardH, Bitmap.Config.ARGB_8888);

    Matrix matrix = new Matrix();
    matrix.postScale(20, 20);
    result = Bitmap.createBitmap(result, 0, 0, result.getWidth(),
                    result.getHeight(), matrix, false);

    Canvas canvas = new Canvas(result);
    canvas.drawBitmap(bmCards, new Rect(x, y, x + cardW + toleranzW, y + cardH + toleranzH), 
            new Rect(0, 0, cardW, cardH), new Paint());

    return result;
}

答案 1 :(得分:2)

使用矩阵进行翻译&在绘制之前旋转。这通常是最简单的方法。