编写了一个简单的Android 程序来读取android系统中的图标,为大家android编程过程中系统图标引用提供方便:

Activity代码

import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.Gallery;import android.widget.ImageView;import android.widget.Toast;import com.gionee.icon.R;public class AndroidImageActivity extends Activity {    private Gallery gallery;    private ImageView imageview;    private ImageAdapter imageadapter;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        imageadapter=new ImageAdapter(this);        /* 通过findViewById 取得 资源对象*/        gallery=(Gallery)findViewById(R.id. Gallery_preView);        imageview=(ImageView)findViewById(R.id. ImageView_photo );        /*给Gallery设置适配器 把Ex_Ctrl_10ME类传入参数*/        gallery.setAdapter(imageadapter);        /*设置Gallery的点击事件监听器*/                gallery.setOnItemClickListener(new Gallery.OnItemClickListener(){        public void onItemClick(AdapterView<?> parent, View v, int position,long id) {                /*显示该图片是几号*/                Toast.makeText(AndroidImageActivity.this,"图片:" + imageadapter.map.get(position).get("name").toString(), Toast.LENGTH_SHORT).show();                /*设置大图片*/                imageview.setBackgroundResource(Integer.parseInt(imageadapter.map.get(position).get("id").toString()));                }                });    }}

图片适配器代码:

import android.content.Context;import android.content.res.TypedArray;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageView;import java.util.ArrayList;import java.util.HashMap;public class ImageAdapter extends BaseAdapter {    int mGalleryItemBackground;    private Context mContext;    public ArrayList<HashMap<String, Object>> map;    public ImageAdapter(Context context) {        this.mContext = context;        /*         * 使用在res/values/attrs.xml 中的<declare-styleable>定义 的Gallery 属性.         */        TypedArray typed_array = context.obtainStyledAttributes(R.styleable.Gallery);        /* 取得Gallery 属性的Index id */        mGalleryItemBackground = typed_array.getResourceId(                R.styleable.Gallery_android_galleryItemBackground, 0);        /* 让对象的styleable 属性能够反复使用 */        typed_array.recycle();        ReadProperties rp = new ReadProperties();        map = rp.getImage(mContext);    }    @Override    public int getCount() {        return map.size();    }    @Override    public Object getItem(int position) {        return position;    }    @Override    public long getItemId(int position) {        return position;    }    @Override    public View getView(int position, View convertView, ViewGroup parent) {        /* 产生ImageView 对象 */        ImageView imageview = new ImageView(mContext);        /* 设置图片给imageView 对象 */        imageview.setImageResource(Integer.parseInt(map.get(position).get("id").toString()));        /* 重新设置图片的宽高 */        imageview.setScaleType(ImageView.ScaleType.FIT_XY);        /* 重新设置Layout 的宽高 */        imageview.setLayoutParams(new Gallery.LayoutParams(80, 80));        /* 设置Gallery 背景图 */        imageview.setBackgroundResource(mGalleryItemBackground);        /* 返回imageView 对象 */        return imageview;    }}

读取配置文件:

import android.content.Context;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.Enumeration;import java.util.HashMap;import java.util.Properties;public class ReadProperties {    @SuppressWarnings("unchecked")    public ArrayList<HashMap<String, Object>> getImage(Context context) {        ArrayList<HashMap<String, Object>> image_map = new ArrayList<HashMap<String, Object>>();                Properties pro = new Properties();        InputStream is;        try {            is = context.getResources().openRawResource(R.raw.info);            pro.load(is);        } catch (IOException e) {            e.printStackTrace();        }                //返回属性列表中所有键的枚举,如果在主属性列表中未找到同名的键,则包括默认属性列表中不同的键        Enumeration<String> enumvalue = (Enumeration<String>) pro.propertyNames();        while(enumvalue.hasMoreElements()) {            HashMap<String, Object> map = new HashMap<String, Object>();            String key = enumvalue.nextElement();            String value = pro.getProperty(key);            map.put("name", "android.R.drawable" + key);            map.put("id", value);            image_map.add(map);        }                return image_map;    }}

配置文件:放置在res/raw/info.properties

(因为文件有点大就不写出内容了)

更多相关文章

  1. Android Studio Gradle多渠道打包(动态设定App名称,应用图标,背景
  2. Android图片剪裁-调用系统实现,完美适配魅族等机型
  3. Android之JAVASe基础篇-面向对象-IO(九)
  4. Android 添加图片水印(图片+文字)
  5. android上传图片到服务器,求服务器那边和android的Activity的完整
  6. 关于android原生Webview的一些属性整理(持续补充~)

随机推荐

  1. Android的广播机制——Broadcast Reciver
  2. Android实现视频播放的3种实现方式
  3. 使用 Android 实现联网
  4. android中常用布局
  5. Android 秒级编译方案-Freeline安装使用
  6. Android(安卓)adapter中调用activity中的
  7. android基于tcpdump的数据包捕获完整解决
  8. Android适配(屏幕适配、国际化适配)
  9. Android(安卓): java.lang.UnsatisfiedLi
  10. android平台下基于MediaRecorder和AudioR