Android ImageView 使用assets里的图片

本文介绍如何创建一个自定义的主页面导航列表适配器,包括初始化上下文、数据集、布局加载、视图生成及图片资源加载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值