我在尝试创建一个位图
BitmapDrawable img = new BitmapDrawable(getResources(), "res/drawable/wrench.png");
Bitmap wrench = img.getBitmap();
//Bitmap wrench = BitmapFactory.decodeResource(getResources(), R.drawable.wrench);
canvas.drawColor(Color .BLACK);
Log.d("OLOLOLO",Integer.toString(wrench.getHeight()));
canvas.drawBitmap(wrench, left, top, null);但是当我调用wrench.getHeight()程序时,NullPoinerException失败了。(我把文件放在可绘制的目录中)如何解决我的问题?
发布于 2012-04-11 20:47:43
好的..。我想我已经处理好你的问题了。就像我说的,您不能通过路径访问您的绘图,所以如果您想要一个人类可读的接口和您可以编程构建的绘图,那么在类中的某个地方声明一个HashMap:
private static HashMap<String, Integer> images = null;然后在构造函数中初始化它:
public myClass() {
if (images == null) {
images = new HashMap<String, Integer>();
images.put("Human1Arm", R.drawable.human_one_arm);
// for all your images - don't worry, this is really fast and will only happen once
}
}现在可以进入了-
String drawable = "wrench";
// fill in this value however you want, but in the end you want Human1Arm etc
// access is fast and easy:
Bitmap wrench = BitmapFactory.decodeResource(getResources(), images.get(drawable));
canvas.drawColor(Color .BLACK);
Log.d("OLOLOLO",Integer.toString(wrench.getHeight()));
canvas.drawBitmap(wrench, left, top, null);发布于 2012-04-11 20:22:58
试试这个:
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.wrench);
Matrix matrix=new Matrix();
matrix.postScale(0.2f, 0.2f);
Bitmap dstbmp=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(),
bmp.getHeight(),matrix,true);
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(dstbmp, 10, 10, null); “res/ drawable /扳手. you”无效路径,所以如果您使用的是可绘制的图像,则使用R.drawable.wrench。
无法获得存储在可绘制中的图像的确切路径。
为什么不呢?当您将应用程序编译到*.apk文件时,所有的资源(除了/raw中的资源)都会被编译。你只能访问他们,使用他们的R.ID。
解决方案?不是真的,您可以将它们复制到sd卡上的某个位置。不,你知道地点:)
发布于 2012-04-11 20:52:38
你可以这样做:
String imageNameWithoutExtension = "wrench";
int id = getResources().getIdentifier(imageNameWithoutExtension, "drawable", getPackageName());
Bitmap dd = BitmapFactory.decodeResource(getResources(), id);
logo.setImageBitmap(dd);https://stackoverflow.com/questions/10113067
复制相似问题