从资源/可绘制动态显示图像 [英] Dynamically show images from resource/drawable

查看:177
本文介绍了从资源/可绘制动态显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将不同的图像(.jpg,.png)动态地从r es / drawable ListView $ c>。
从数据库获取的图像中的名称。
图像本身在 res / drawable 文件夹中。



这是我已经拥有的,发生错误:D



String imgName; - >有数据库中需要的img名称

  Drawable drawable; 
drawable = Class.appContext.getResources()。getDrawable(Class.appContext.getResources()。getIdentifier(com.example.test:drawable /+ imgName,null,null));

然后我得到一个 ArrayList 数据库中的每个图像(+ - 200张图像):

  ArrayList< HashMap< String,Object>> list = new ArrayList< HashMap< String,Object>>(); 
HashMap< String,Object> map = new HashMap< String,Object>();

map = new HashMap< String,Object>();
map.put(img,drawable);

map.put(text,some text);
list.add(map);

现在我返回列表;



我调用的类:

  listReceive = Class.getImages(appContext); 

listSetup = new SimpleAdapter(this,listReceive,R.layout.row,
new String [] {img,text},new int [] { IMG_CELL,R.id.TEXT_CELL});
lvList.setAdapter(listSetup);

XML行是一个 ImageView code> TextView 。


System.out:resolveUri失败
位图uri :
android.drawable.bitmapDrawable@405739
resolveUri在坏位图上失败uri:
android.drawable.bitmapDrawable@405639
resolveUri失败在bitmap uri:
android .drawable.bitmapDrawable @ 405959
resolveUri在坏位图上失败uri:
android.drawable.bitmapDrawable@405677 ...
... ...


当我将图像保存到本地或SD卡内存中时,我将其工作,然后将路径放在数组列表中,如:



map.put(img,/ data / data / com.example.test / images /+ imgName);

  

我不能使用这个,因为我将需要从 res / drawable 到本地或SD。这占用内存的2倍。不能有。



必须有一种方式从drawable动态获取图像。



任何人都知道我在这里失踪
谢谢。

解决方案

SimpleAdapter希望指定一个资源或图像URI的整数或字符串:

  public void setViewImage(ImageView v,String value)
自:API级别1




由bindView()调用以设置ImageView的图像,但只有当没有现有ViewBinder或现有的ViewBinder无法处理绑定到ImageView。 默认情况下,该值将被视为图像资源。如果该值不能用作图像资源,则该值用作图像Uri。 如果提供的数据不是int或整数,则调用此方法而不是setViewImage(ImageView,int)


我相信应该使用 setViewBinder 提供一个 ViewBinder for SimpleAdapter 来处理绑定 Drawable 数据到 ImageView 。基本上, setViewValue()方法应该返回false,除非它被调用为您的图像视图。当您的图像视图被调用时,该方法应该在视图中设置数据并返回true。返回值指示ViewBinder是否能够设置视图,或者适配器是否应尝试通过其默认行为绑定数据本身。



也许像: p>

  private final SimpleAdapter.ViewBinder mViewBinder = 
new SimpleAdapter.ViewBinder(){
@Override
public boolean setViewValue(
final view view,
final Object data,
final String textRepresentation){

if(view instanceof ImageView){
(( ImageView)view).setImageDrawable((Drawable)data);
返回true;
}

return false;
}
};

...

listSetup .setViewBinder(mViewBinder);我已经做了一个 / android-how-to-set-list-item-checked / 4852967#4852967>类似的东西与SimpleCursorAdapter及其ViewBinder。


I'm trying to put different images (.jpg , .png) dynamically into a ListView from res/drawable . The names from the images I get from a database. The images themselves are in the res/drawable folder.

This is what I already have, With an error of :D

String imgName; --> There are the img names I need from the database

Drawable drawable;    
    drawable = Class.appContext.getResources().getDrawable(Class.appContext.getResources().getIdentifier("com.example.test:drawable/"+imgName,null,null));  

Then I get this into a ArrayList for every image in the database(+- 200 images):

ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map = new HashMap<String, Object>();

map = new HashMap<String, Object>();
map.put("img",drawable);

          map.put("text", "some text");
          list.add(map);

Now I RETURN list;

In the Class where I call :

listReceive = Class.getImages(appContext); 

listSetup = new SimpleAdapter(this, listReceive, R.layout.row,
                        new String[] {"img", "text"}, new int[] {R.id.IMG_CELL, R.id.TEXT_CELL});
                lvList.setAdapter(listSetup);

XML row is an ImageView and a TextView.

System.out :resolveUri failed on bad bitmap uri: android.drawable.bitmapDrawable@405739 resolveUri failed on bad bitmap uri: android.drawable.bitmapDrawable@405639 resolveUri failed on bad bitmap uri: android.drawable.bitmapDrawable@405959 resolveUri failed on bad bitmap uri: android.drawable.bitmapDrawable@405677... ... ...

I got it working when I saved images into local or SDcard memory, and then put the path inside the arraylist like:

map.put("img","/data/data/com.example.test/images/" + imgName);

I can't use this because then i will need to copy the pictures from res/drawable onto local or SD.This takes up 2 times the memory. can't have that of.

There must be a way to get images dynamically from the drawable.

Any one knows what I'm missing here? Thanks.

解决方案

The SimpleAdapter expects an integer or string that specifies a resource or image URI:

public void setViewImage (ImageView v, String value)
Since: API Level 1

Called by bindView() to set the image for an ImageView but only if there is no existing ViewBinder or if the existing ViewBinder cannot handle binding to an ImageView. By default, the value will be treated as an image resource. If the value cannot be used as an image resource, the value is used as an image Uri. This method is called instead of setViewImage(ImageView, int) if the supplied data is not an int or Integer.

I believe should use setViewBinder to provide a ViewBinder for the SimpleAdapter to handle binding the Drawable data to the ImageView. Basically, the setViewValue() method should return false unless it is called for your image view. When it is called for your image view, the method should set the data in the view and return true. The return value indicates whether the ViewBinder was able to set the view or whether the adapter should try to bind the data itself via its default behavior.

Maybe something like:

private final SimpleAdapter.ViewBinder mViewBinder =
    new SimpleAdapter.ViewBinder() {
        @Override
        public boolean setViewValue(
                final View view,
                final Object data,
                final String textRepresentation) {

            if (view instanceof ImageView) {
                ((ImageView) view).setImageDrawable((Drawable) data);
                return true;
            }

            return false;
        }
    };

...

listSetup .setViewBinder(mViewBinder);

I've done a similar thing with a SimpleCursorAdapter and its ViewBinder.

这篇关于从资源/可绘制动态显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆