现在的位置: 首页 > 综合 > 正文

Android ImageView 使用assets里的图片

2013年05月06日 ⁄ 综合 ⁄ 共 1333字 ⁄ 字号 评论关闭
package com.nobeg.adapter;
import java.io.InputStream;
/*
 * 
 * 自定义主页面导航列表适配器
 */
public class MainAdapter extends BaseAdapter {
	
	private Context context;
	private List<Map<String, Object>> rs=null;
	AssetManager assetManager =null;
	public MainAdapter(Context context,List<Map<String, Object>>  rs) {
		// TODO Auto-generated constructor stub
		this.context=context;
		this.rs=rs;
		
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return rs.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	public View getView(int position, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		LayoutInflater flater = LayoutInflater.from(context);
	    /* 使用grid.xml为每几个item的Layout */
	    View v = (View) flater.inflate(R.layout.main_grid_item, null);
	    /* 取得View */
	    ImageView iv = (ImageView) v.findViewById(R.id.grid_img);
	    TextView tv = (TextView) v.findViewById(R.id.grid_text);
	    TextView moduleID = (TextView) v.findViewById(R.id.gongneng_flag);
	    
	    /* 设置显示的文文字 */
	    tv.setText(rs.get(position).get("naviName").toString());
	    moduleID.setText(rs.get(position).get("moduleID").toString());
	    //这里才是重点
	    assetManager=context.getAssets();
	    try {
	    	InputStream in=assetManager.open("img/main."+rs.get(position).get("moduleID").toString()+".png");
	    	Bitmap bmp=BitmapFactory.decodeStream(in);
	    	iv.setImageBitmap(bmp);
		} catch (Exception e) {
			// TODO: handle exception
		}
	    return v;
	}
}

抱歉!评论已关闭.