首页 文章

Android - 从某个点开始剪切位图

提问于
浏览
0

我一直在这里搜索关于android中的切割位图,但是我不能让它对我起作用 . 我想切断一个特定位图的某些部分,但我想从图像的中心到底部开始 .

我已经尝试过使用:

createBitmap(android.graphics.Bitmap source,int x,int y,int width,int height)

然而,它总是开始从TOP切割我的图像,甚至改变x和y值 . 我想将我的位图剪切为蓝色方块下方 .

Changing x and y in createBitmap didnt work

有人知道如何剪切我的位图?

2 回答

  • 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

  • 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像素的图像 .

相关问题