【问题标题】:How to setImageResource?如何设置图像资源?
【发布时间】:2017-04-10 10:05:29
【问题描述】:

我想在picture.setImageResource( ); 处将 ImageView 从 String 获取到 Int 我使用 String 到 Integer 更改,它没有显示在 GridView 中。如何获取图像大小和 setImageView?

菜单.java

public class Menu {

private Integer id;
private String menuImage;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getMenuImage() {
        return menuImage;
    }

    public void setMenuImage(String menuImage) {
        this.menuImage = menuImage;
    }

MyAdapter.java

public class MyAdapter extends BaseAdapter {
    List<Menu> mItems = new ArrayList<Item>(); // from server
    LayoutInflater mInflater;

    public MenuGridViewAdapter(Context context, List<Menu> menuList) {
        mInflater = LayoutInflater.from(context);
        this.menuList = menuList;
    }

    @Override
    public int getCount() {
        return mItems.size();
    }

    @Override
    public Menu getItem(int i) {
        return mItems.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View v = view;
        ImageView picture;
        TextView name;

        if (v == null) {
            v = mInflater.inflate(R.layout.grid_item, viewGroup, false);
            v.setTag(R.id.picture, v.findViewById(R.id.picture));
        }

        picture = (ImageView) v.getTag(R.id.picture);

        Item item = getItem(i);    
        picture.setImageResource( ); // 

        return v;
    }

【问题讨论】:

    标签: java android imageview baseadapter


    【解决方案1】:

    imageView.setImageResource() 需要一个来自 Drawable 的图像,这是一个 int,例如:R.drawable.sample 您可以使用 picasso 库而不是 setImageResource。为此,

    添加以下行以将添加图像加载到图像视图中。

    Picasso.with(context).load(R.drawable.drawableName).into(picture);
    

    将此添加到您的 build.gradle 文件中:

    compile 'com.squareup.picasso:picasso:2.5.2' 
    

    【讨论】:

      【解决方案2】:

      这将帮助您获取要在 setImageResource() 中使用的相应图像的 id

      int id = getResources().getIdentifier("drawable/your_image", "id", "your_package_name");
          imageView.setImageResource(id);
      

      至于高度和宽度,您可以设置这样的参数

      LinearLayout linearLayout = (LinearLayout) v.findViewById(R.id.imgLayout2);
          LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(340, 340);
          parms.gravity = Gravity.CENTER;
          parms.setMargins(20, 50, 20, 50);
          final ImageView imageView = new ImageView(getActivity());
          imageView.setLayoutParams(parms);
      

      【讨论】:

        【解决方案3】:

        使用此代码:

        public class GridViewAdapter extends BaseAdapter {
        
            private Context mContext;
        
            public GridViewAdapter(Context c) {
                mContext = c;
            }
        
            public int getCount() {
                return mThumbIds.length;
            }
        
            public Object getItem(int position) {
                return null;
            }
        
            public long getItemId(int position) {
                return 0;
            }
        
            // create a new ImageView for each item referenced by the Adapter
            public View getView(int position, View convertView, ViewGroup parent) {
                ImageView imageView;
               if (convertView == null) {
                    // if it's not recycled, initialize some attributes
                    imageView = new ImageView(mContext);
                    imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
                    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    imageView.setPadding(8, 8, 8, 8);
                } else {
                    imageView = (ImageView) convertView;
                }
        
               imageView.setImageUri(Uri.parse("your url here")); // updated code
                return imageView;
            }
        
            // references to our images
            private Integer[] mThumbIds = {
                    R.mipmap.ic_launcher, R.mipmap.ic_launcher,
                    R.mipmap.ic_launcher, R.mipmap.ic_launcher,
                    R.mipmap.ic_launcher, R.mipmap.ic_launcher,
                    R.mipmap.ic_launcher, R.mipmap.ic_launcher,
                    R.mipmap.ic_launcher, R.mipmap.ic_launcher,
            };
        
        }
        

        【讨论】:

        • 你好,@Simranjeet Singh 图片来自 List 中的服务器,getImage() 是 String NOT int,如何从中获取?
        猜你喜欢
        • 2014-03-10
        • 2017-04-27
        • 2014-01-28
        • 1970-01-01
        • 2019-12-11
        • 2011-06-24
        • 2017-05-19
        • 1970-01-01
        • 2019-01-04
        相关资源
        最近更新 更多