`
wpf814533631
  • 浏览: 192099 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Android 系统图标

 
阅读更多

编写了一个简单的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

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics