什么是createBitmap()参数中的“位图源”?

时间:2015-05-31 07:07:12

标签: android android-layout android-drawable android-image android-bitmap

我正在关注如何使用位图裁剪图像的this S.O. post

Android documentation中确认,有一个方法createBitmap()具有以下参数:

createBitmap(Bitmap source, int x, int y, int width, int height)

但我不明白这个Bitmap源需要什么。我相信它必须引用我想用于我的Bitmap对象的图像。我想使用的图像存储为一个名为rainbow的drawable,所以我尝试了以下内容:

    Bitmap cropedBitmap = Bitmap.createBitmap(R.id.rainbow, 0, 0, 300, 300);

在这种情况下,Android Studio报告“无法解析符号'彩虹'

我也尝试过:

      Bitmap cropedBitmap = Bitmap.createBitmap(R.drawable.rainbow, 0, 0, 300, 300);

和Android Studio报告“无法解析方法createBitmap(int,int,int,int,int)

我从文档中看到第一个参数是一个Bitmap。那不是用Bitmap来创建Bitmap吗?另外,我没有在其文档页面上看到Bitmap类的任何构造函数。

有关我缺少什么或接下来要尝试什么的任何建议?

2 个答案:

答案 0 :(得分:0)

R.drawable.rainbow是整数值而不是Bitmap,因为它只是生成的资源标识号。

您需要使用一种方法将可绘制资源ID作为this post创建到Bitmap对象中。

编辑:添加选项和可绘制的调用方法

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
Drawable drawable = getResources().getDrawable(R.drawable.rainbow);
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
Bitmap cropedBitmap = Bitmap.createBitmap(bitmap, options), 0, 0, 300, 300);

答案 1 :(得分:0)

如果你想裁剪掉一块Bitmap,你可以使用BitmapFactory.Options直接将资源从资源加载到ram中 当你在游戏中制作动画并且图像不是更大的精灵时,这很常见