setImageResource和setImageDrawable区别

ImageView设置图片的方式有很多钟,可以在xml里面写android:src=”@drawable/xxx”,也可以在java代码里面设置。

在java里面的设置方式也有多种,方法包括:setImageResource,setImageDrawable,setImageBitmap。

在xml里面设置实际上和在java里面调用setImageResource是一样的,当然xml多了一个解析的过程,放到java代码里调用会稍微好些(实际没什么区别)。

3种设置图片方式的区别:

(1)setImageResource的参数是resId,必须是drawable目录下的资源.另外,在setImageResource方法中有写明了注释

* <pclass="note">This does Bitmap reading and decoding on the UI

* thread, which can cause a latency hiccup.If that's a concern,

* consider using {@link #setImageDrawable(android.graphics.drawable.Drawable)}or

* {@link #setImageBitmap(android.graphics.Bitmap)} and

* {@link android.graphics.BitmapFactory} instead.</p>

这个方法是在UI线程中对图片读取和解析的,所以有可能对一个Activity的启动造成延迟。所以如果顾虑到这个官方建议用setImageDrawable和setImageBitmap来代替。

(2)setImageBitmap参数是Bitmap,可以解析不同来源的图片再进行设置。不过我们看看setImageBitmap的源码:

@android.view.RemotableViewMethod

public void setImageBitmap(Bitmap bm) {

// if this is used frequently, mayhandle bitmaps explicitly

// to reduce the intermediate drawable object

setImageDrawable(newBitmapDrawable(mContext.getResources(), bm));

}

实际上setImageBitmap做的事情就是把Bitmap对象封装成Drawable对象,然后调用setImageDrawable来设置图片。因此代码里面才写上了建议,如果需要频繁调用这个方法的话最好自己封装个固定的Drawable对象,直接调用setImageDrawable,这样可以减少Drawable对象。因为每次调用setImageBitmap方法都会对Bitmap对象new出一个Drawable。

(3)setImageDrawable参数是Drawable,也是可以接受不同来源的图片,方法中所做的事情就是更新ImageView的图片。上面两个方法实际上最后调用的都是setImageDrawable(setImageResource没有直接调用,不过更新的方法与setImageDrawable一样)。

所以综合来看setImageDrawable是最省内存高效的,如果担心图片过大或者图片过多影响内存和加载效率,可以自己解析图片然后通过调用setImageDrawable方法进行设置。

阅读更多

扫码向博主提问

灵思致远Leansmall

非学,无以致疑;非问,无以广识
  • 擅长领域:
  • android
  • c++
  • java
  • 单片机
  • unity
去开通我的Chat快问
换一批

((ImageView)viewHolder.image).setImageResource(id)

04-05

package com.tyut.gridview01;nnimport java.util.ArrayList;nimport java.util.List;nnimport android.app.Activity;nimport android.content.Context;nimport android.os.Bundle;nimport android.util.Log;nimport android.view.LayoutInflater;nimport android.view.View;nimport android.view.ViewGroup;nimport android.widget.AdapterView;nimport android.widget.AdapterView.OnItemClickListener;nimport android.widget.BaseAdapter;nimport android.widget.GridView;nimport android.widget.ImageView;nimport android.widget.TextView;nimport android.widget.Toast;nnpublic class PictureActivity extends Activity nnprivate String[] titles = "MM1","MM2","MM3","MM4","MM5","MM6","MM7","MM8","MM9","MM10";nprivate Integer[] ps =new Integer[]R.drawable.g1,R.drawable.g2,R.drawable.g3,R.drawable.g4,R.drawable.g5,R.drawable.g6,R.drawable.g7,R.drawable.g8,R.drawable.g9,R.drawable.g10;n n @Overriden protected void onCreate(Bundle savedInstanceState) n // TODO Auto-generated method stubn super.onCreate(savedInstanceState);n setContentView(R.layout.activity_grid);n GridView grid = (GridView) findViewById(R.id.grid);n grid.setAdapter(new PictureAdapter(titles, ps, this));n grid.setOnItemClickListener(new OnItemClickListener() nn @Overriden public void onItemClick(AdapterView arg0, View arg1, int position,n long arg3) n Log.i("MainActivity", "position:"+position);n Toast.makeText(PictureActivity.this, " "+position, Toast.LENGTH_SHORT).show();n n );n n nnnclass PictureAdapter extends BaseAdapternn private static final String TAG = "MainActivity";n n private LayoutInflater inflater;n private List pictures;n public PictureAdapter(String[] titles ,Integer[] ps,Context context)n super();n pictures = new ArrayList();n inflater = LayoutInflater.from(context);n for (int i = 0; i < ps.length; i++) n Picture p = new Picture(titles[i],ps[i]);n pictures.add(p);n n n @Overriden public int getCount() n // TODO Auto-generated method stubn if (null !=pictures) n return pictures.size();n elsen return 0;n n nn @Overriden public Object getItem(int position) n // TODO Auto-generated method stubn return pictures.get(position);n nn @Overriden public long getItemId(int id) n // TODO Auto-generated method stubn return id;n nn @Overriden public View getView(int position, View convertview, ViewGroup arg2) n // TODO Auto-generated method stubn ViewHolder viewHolder;n if (convertview == null) n convertview = inflater.inflate(R.layout.activity_griditem, null);n viewHolder = new ViewHolder();n Picture pic =pictures.get(position);n int id = pic.getId();n_## ((ImageView)viewHolder.image).setImageResource(id);_n String t =pic.getTitle();n ((TextView)viewHolder.title).setText(t);n convertview.setTag(viewHolder);n elsen viewHolder = (ViewHolder) convertview.getTag();n n viewHolder.title.setText(pictures.get(position).getTitle());n viewHolder.image.setImageResource(pictures.get(position).getId());n Log.i(TAG, "<<<<<<<<"+position);n return convertview;n n n class ViewHoldern public TextView title;n public ImageView image;n n public class Picture n private String title;n private int id ;n public Picture(String title,int id)n this.title =title;n this.id =id;n n public String getTitle() n return title;n n public void setTitle(String title) n this.title = title;n n public int getId() n return id;n n public void setId(int id) n this.id = id;n nnn问题:n## ((ImageView)viewHolder.image).setImageResource(id);n说空指针异常n

没有更多推荐了,返回首页