2014-09-03 34 views
0

我一直在这里搜索关于在an​​droid中切割位图,但我无法让它为我工作。 我想切断一个特定位图的某个部分,但我想从中间开始到图像的底部。Android - 从某点开始切割位图

我已经尝试过使用:

createBitmap(android.graphics.Bitmap源,INT的x,INT Y,INT宽度,INT高度)

然而,它总是开始切我从TOP的形象,甚至改变X和Y值。 我想把我的位图剪成蓝色方块。

Changing x and y in createBitmap didnt work

有人对如何把我的位图什么想法?

回答

0

试试这个:

Bitmap source = BitmapFactory.decodeResource(getResources(), 
      R.drawable.ic_launcher); 
    int width = source.getWidth(); 
    int height = source.getHeight(); 
    int required = 25;// 25 pixels width and height 
    int startX = (width - required)/2; 
    int startY = (height - required)/2; 
    Bitmap resized = Bitmap.createBitmap(source, startX, startY, required, 
      required); 

唯一相关的部分是运行startx和startY计算。该代码假设你需要一个25 * 25像素的图像。

1

你应该看看这个:

private Bitmap cropBitmap1() 
{ 
    Bitmap bmp2 = BitmapFactory.decodeResource(this.getResources(), R.drawable.image1); 
    Bitmap bmOverlay = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888); 

    Paint p = new Paint(); 
    p.setXfermode(new PorterDuffXfermode(Mode.CLEAR));    
    Canvas c = new Canvas(bmOverlay); 
    c.drawBitmap(bmp2, 0, 0, null); 
    c.drawRect(30, 30, 100, 100, p); 

return bmOverlay; 
} 

链接:Source