安卓:切断部断位图,它的规模 [英] Android: cut a part off a bitmap and scale it

查看:249
本文介绍了安卓:切断部断位图,它的规模的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我想说,我真的读书不多,但我没有准确地找到这个问题...

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

目前我的code也工作得很好。我有一个位图,然后我把我的位图的一部分,并得出这样的。

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

到目前为止,它的工作原理..但我怎么能调整该图像?
我的意思是我画的位图的右边部分在画布上,但我想这个扩展部分一点点,因为它太小了...也许到actualH * 1,5和actualW * 1,5或东西。

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.

推荐答案

我想你可以使用这个:

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..

修改

请试试这个..我也没有尝试过了..你应该与价值观发挥我在评论中写道:你..,你也应该阅读<一href=\"http://developer.android.com/reference/android/graphics/Bitmap.html#createBitmap%28android.graphics.Bitmap,%20int,%20int,%20int,%20int,%20android.graphics.Matrix,%20boolean%29\"相对=nofollow> createBitmap()和<一个href=\"http://developer.android.com/reference/android/graphics/Matrix.html#postScale%28float,%20float%29\"相对=nofollow>分频器()单证去了解你在做什么有:)

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

这篇关于安卓:切断部断位图,它的规模的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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