我有一个包含两个图像的图像(精灵)(所选图标和非选定图标)。图像的宽度相同,但由于第二张图像,其高度是两倍。如何从一个图像中分别获取这两个图像?
答案 0 :(得分:0)
我用这段代码解决了这个问题:
private Bitmap getCroppedBitmap(boolean isSelected){
Bitmap fullImage = BitmapFactory.decodeResource(getResources(), R.drawable.sprite);
Bitmap piece = null;
int height = fullImage.getHeight()/2;
if(isSelected){
// show first image
piece = Bitmap.createBitmap(fullImage, 0, 0, fullImage.getWidth(), height);
}else{
// show second image
piece = Bitmap.createBitmap(fullImage, 0, height, fullImage.getWidth(), height);
}
return piece;
}