1.
  1. public static Drawable getDrawableByPicName(String picName, Context context) {
  2.         int id = context.getResources().getIdentifier(
  3.                 picName == null ? "no_picture" : picName, "drawable",
  4.                 GlobalConst.PACKAGE_NAME);
  5.         Drawable p_w_picpath = null;
  6.         if (id <= 0) {
  7.             p_w_picpath = context.getResources().getDrawable(R.drawable.no_picture);
  8.         } else {
  9.             p_w_picpath = context.getResources().getDrawable(id);
  10.         }
  11.         return p_w_picpath;
  12.     }
  13. 2.
  14. 可以使用反射,我写了一个小例子。    
                                
    1. public static void main() throws SecurityException, NoSuchFieldException,
    2.             IllegalArgumentException, IllegalAccessException,
    3.             ClassNotFoundException
    4.     {
    5.         String resourceName = "R.drawable.icon";
    6.         String packageName = "com.bbcode";
    7.         int resourceId = getResourceId(packageName, resourceName);
    8.         if(R.drawable.icon == resourceId){
    9.             //success
    10.             ;
    11.         }
    12.     }

    13.     public static int getResourceId(String packageName, String resourceName)
    14.             throws SecurityException, NoSuchFieldException,
    15.             IllegalArgumentException, IllegalAccessException,
    16.             ClassNotFoundException
    17.     {
    18.         String[] splitStr = resourceName.split("\\.");
    19.         String classStr = splitStr[0] + "$" + splitStr[1];
    20.         Class c = Class.forName(packageName + "." + classStr);
    21.         Field f = c.getDeclaredField(splitStr[2]);
    22.         return f.getInt(f.getName());
    23.     }
    24. 3.将图片放到 res/assets 下,不过该方式还没有试过

更多相关文章

  1. Android(安卓)5.0以上移动网络开关
  2. Android(安卓)IDL 小例子
  3. Android(安卓)线程超时的例子
  4. android ExpandableListView简单例子
  5. Android(安卓)长按setOnItemLongClickListener 显示context Menu
  6. android之sql例子
  7. android之sql例子
  8. android 小问题记录
  9. Android中利用DisplayMetrics取得手机屏幕大小

随机推荐

  1. Android实现CBC加解密
  2. Android(安卓)Studio增量更新(Mac)
  3. EditText无法获取焦点(android:descendant
  4. 【Android(安卓)Socket专题】:UDP通信客户
  5. Android 中文 API (24) ―― MultiAutoComp
  6. android 输入键盘遮挡页面的问题
  7. Google Issues Cease & Desist to Open S
  8. Cordova 3.x 基础(13) -- 为Android APK签
  9. 【ncnn android】算法移植(九)——DBface a
  10. dx 工具的使用