简体   繁体   English

从路径创建BitmapDrawable

[英]creating BitmapDrawable from path

i'm trying to create a Bitmap 我正在尝试创建一个位图

 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);

but when i call wrench.getHeight() program failed with NullPoinerException. 但是当我通过NullPoinerException调用wrench.getHeight()程序时失败。 (i put the file in drawable directory) how can i solve my problem? (我将文件放在可绘制目录中)如何解决我的问题?

try this: 尝试这个:

            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/wrench.png" not valid path so either use images from sbcard if you are using images from drawable then use R.drawable.wrench “ res / drawable / wrench.png”路径无效,因此,如果使用的是来自drawable的图像,则使用R.drawable.wrench图像,然后使用R.drawable.wrench

It is not possible to get the exact path for an image , that is stored in drawable. 无法获取存储在drawable中的图像的确切路径。

Why not? 为什么不? When you compile your app to an *.apk file, all resources (ok, except from them in /raw) are compiled as well. 当您将应用程序编译为* .apk文件时,所有资源(除了/ raw中的资源)都将被编译。 You can only acces them, using their R. id. 您只能使用其R.id来访问它们。

Solution? 解? Not really, you could copy them to a location on the sd card for example. 并非如此,例如,您可以将它们复制到SD卡上的某个位置。 No you know the location :) 不知道位置:)

Ok... I think I have a handle on your problem now. 好吧...我想我现在可以解决您的问题了。 Like I said, you can't access your drawables via a path, so if you want a human readable interface with your drawables that you can build programatically, declare a HashMap somewhere in your class: 就像我说过的那样,您无法通过路径访问可绘制对象,因此,如果您希望通过可编程方式构建的可绘制对象具有人类可读的界面,请在类中的某个位置声明一个HashMap:

private static HashMap<String, Integer> images = null;

Then initialize it in your constructor: 然后在构造函数中对其进行初始化:

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
  }
}

Now for access - 现在可以访问-

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);

You can do like this: 您可以这样:

String imageNameWithoutExtension = "wrench";
int id = getResources().getIdentifier(imageNameWithoutExtension, "drawable", getPackageName());
Bitmap dd = BitmapFactory.decodeResource(getResources(), id);
logo.setImageBitmap(dd);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM