- package com.example.testicon;
- import java.lang.reflect.Field;
- import java.util.HashMap;
- import java.util.Map;
- import android.app.Activity;
- import android.content.Context;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- private TextView testV;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- testV = (TextView) findViewById(R.id.myid);
- testV.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- getSourceIDS(MainActivity.this, R.drawable.class);
- }
- });
- getSourceIDS(this, R.drawable.class);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.activity_main, menu);
- return true;
- }
- /**
- * @param context
- * @param classType
- * 资源类型,ex: R.drawable.class; R.attr.class; R.id.class;
- * R.layout.class; R.string.class; R.menu.class; R.style.class;
- * @param names
- * 可选参数 "in,out" 资源名字请用“,”分开,注意资源名字和资源类型名字要对应上
- * @return Map<String, Integer> 其中key为资源名字,value为资源的int类型的ID值
- * @throws IllegalArgumentException
- */
- public static Map<String, Integer> getSourceIDS(Context context,
- Class<?> classType, String... names)
- throws IllegalArgumentException {
- // 如果想直接返回Integer[]数组请直接改方法返回类型int[]及返回结果修改为ids
- Field[] fIDs = classType.getFields();
- String sourceType = classType.getSimpleName();
- long start = System.currentTimeMillis();
- Map<String, Integer> sourceIDS = new HashMap<String, Integer>();
- int[] ids = new int[fIDs.length];
- int count = fIDs.length;
- String[] soureNames = null;
- // 标识是否要取指定的sourceID
- boolean singelID = false;
- if (names.length > 0 && !names[0].trim().equals("")) {
- soureNames = names[0].split(",");
- count = soureNames.length;
- ids = new int[count];
- singelID = true;
- }
- try {
- for (int i = 0; i < count; i++) {
- Field fld = fIDs[i];
- int nID = 0;
- String sourceName = fld.getName();
- if (singelID) {
- nID = context.getResources().getIdentifier(soureNames[i],
- sourceType, context.getPackageName());
- } else {
- nID = fld.getInt(null);
- }
- ids[i] = nID;
- sourceIDS.put(sourceName, nID);
- }
- } catch (Exception e) {
- Log.e("get SourceIDS Excetion ", e.getMessage());
- return sourceIDS;
- }
- for (String key : sourceIDS.keySet()) {
- int myid = sourceIDS.get(key);
- Log.e(key + " = ", myid + "");
- }
- long total = System.currentTimeMillis() - start;
- Log.e("total time", total + "");
- Toast.makeText(context,
- " 取得【" + ids.length + "】张图片ID 用时(毫秒):" + total,
- Toast.LENGTH_LONG).show();
- return sourceIDS;
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/0708201410170.html
来源: http://www.codesnippet.cn/detail/0708201410170.html