Android - 以编程方式剪切可绘制图像

标签 android drawable

如何从可绘制图像中剪切一些像素?比如,我有一个单杠,我只想显示该单杠的 50%。我该怎么做?

最佳答案

这是从给定图像裁剪图像的方式:

// Given image is named "girl" in drawable folder
Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.girl).copy(Config.ARGB_8888, true);
// Create a bit map with width and heigh = 100 pixel
Bitmap image2 = Bitmap.createBitmap(100, 100, Config.ARGB_8888);
for(int i = 0; i < 100; i++){
    for(int j = 0; j < 100; j++){
            // Set color of each pixel of created image to color of given image                     
            mage2.setPixel(i, j, image.getPixel(i, j));
    }
}

对于您的问题,只需创建一个宽度和高度等于给定图像宽度和高度一半的图像,然后在您想要的任何位置设置任何像素!

关于Android - 以编程方式剪切可绘制图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12318786/

相关文章:

android - 更改菜单 actionLayout 值?

Android:检查网络和真实的互联网连接

android - 报错android SurfaceFlinger

Android:圆形图像的框架

android - RingShapeDrawable 的半径设置为父级的高度而不是宽度

android - setCompoundDrawables EditText 文字重叠

android - 在 Nexus 5 (Android 5.1.1) 上从图库中选取图像返回 null,在其他设备上工作

android - 在RelativeLayout中将ImageView垂直居中到TextView

Android - ClipDrawable、ScaleDrawable,它是如何工作的?

android - 需要定义可绘制对象的 'drawable' 属性或子标记