Android: cut a part off a bitmap and scale it

橙三吉。 提交于 2019-12-24 10:48:22

问题


First I want to say, that I really read much but I didn't find exactly this problem...

At the moment my code does work well. I have a bitmap, then I cut a part of my bitmap and draw this.

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

the method, that is called is the following:

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

so far it works.. but how could I resize this Image? I mean on the canvas I draw the right part of the Bitmap but I want to scale this part a little bit, because it's too small... maybe to actualH * 1,5 and actualW * 1,5 or something.


回答1:


I think you can use this:

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

and after you get the resized bitmap you can draw it on the canvas..

EDIT

Please try this.. I didn't tried it out.. you should play with the values as I wrote you in the comments.. and also you should read the createBitmap() and postScale() documentations to get to know what you are doing there :)

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



回答2:


Use a Matrix to do translations & rotations before you draw it. This is usually the easiest way.



来源:https://stackoverflow.com/questions/14177546/android-cut-a-part-off-a-bitmap-and-scale-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!