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

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

问题描述

我试图把动态不同的图像(.JPG,.png格式)转换成的ListView 自R ES /绘制。 从图像的名字,我从数据库中获取。 这些图像本身是在 RES /绘制文件夹。

这是我已经有了,随着一个错误:D

字符串imgName; - >还有img的名字,我需要从数据库

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

然后,我进入这一个的ArrayList 为数据库中的每个图像(+ - 200图像):

 的ArrayList< HashMap的<字符串,对象>>名单=新的ArrayList< HashMap的<字符串,对象>>();
HashMap的<字符串,对象>图=新的HashMap<字符串,对象>();

图=新的HashMap<字符串,对象>();
map.put(IMG,绘制);

          map.put(文字,一些文本);
          list.add(图)
 

现在我回到列表;

在类,我呼吁:

  listReceive = Class.getImages(appContext);

listSetup =新SimpleAdapter(这一点,listReceive,R.layout.row,
                        新的String [] {IMG,文本},新的INT [] {R.id.IMG_CELL,R.id.TEXT_CELL});
                lvList.setAdapter(listSetup);
 

XML行是一个的ImageView 的TextView

  

的System.out:resolveUri失败坏   位图网址:   android.drawable.bitmapDrawable@405739   resolveUri没有坏的位图网址:   android.drawable.bitmapDrawable@405639   resolveUri没有坏的位图网址:   android.drawable.bitmapDrawable@405959   resolveUri没有坏的位图网址:   android.drawable.bitmapDrawable@405677 ...   ......

我得到它的工作,当我保存的图像到本地或SD卡存储,然后把路径中的数组列表里面,如:

  map.put(IMG,/数据/数据​​/ com.example.test /图片/+ imgName);
 

我不能用这个,因为那我就需要将照片从 RES /绘制到本地或SD.This占用了2倍的内存复制。不能有的。

必须有一种方法来从绘制动态获取的图像。

任何人知道我在这里缺少什么? 谢谢你。

解决方案

该SimpleAdapter要求整数或字符串,它指定一个资源或图片网址:

 公共无效setViewImage(ImageView的V,字符串值)
自:API级别1
 

  

调用由bindView()设置为一个ImageView的图像,但只有当没有现有ViewBinder或者如果现有ViewBinder不能处理结合至ImageView的。 默认情况下,该值将被视为一个图像资源。如果该值不能用作图像资源,该值被用作图像乌里:此方法称为setViewImage(ImageView的,INT),而不是如所提供的数据不是int或整型

我认为应该用<一个href="http://developer.android.com/reference/android/widget/SimpleAdapter.html#setViewBinder%28android.widget.SimpleAdapter.ViewBinder%29"相对=nofollow> setViewBinder 提供 ViewBinder SimpleAdapter 处理结合绘制对象数据的的ImageView 。基本上,<一href="http://developer.android.com/reference/android/widget/SimpleAdapter.ViewBinder.html#setViewValue%28android.view.View,%20java.lang.Object,%20java.lang.String%29"相对=nofollow> setViewValue()方法应该返回false,除非它被称为你的图像视图。当它被称为你的图像显示,该方法应该设置在视图中的数据并返回true。返回值表示ViewBinder是否能够设置视图或适配器是否应尝试通过它的默认行为数据本身绑定。

也许是这样的:

 私人最终SimpleAdapter.ViewBinder mViewBinder =
    新SimpleAdapter.ViewBinder(){
        @覆盖
        公共布尔setViewValue(
                最终的视图中查看,
                最终目标数据,
                最后弦乐textRe presentation){

            如果(查看的instanceof ImageView的){
                ((ImageView的)视图).setImageDrawable((绘制对象)的数据);
                返回true;
            }

            返回false;
        }
    };

...

listSetup .setViewBinder(mViewBinder);
 

我已经做了<一href="http://stackoverflow.com/questions/4852828/android-how-to-set-list-item-checked/4852967#4852967">similar事情与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天全站免登陆