2016-09-19 85 views
0

我正在處理從圖標選擇窗口向主菜單添加圖標的過程。如果用戶選擇了一些圖標,在下次用戶點擊添加圖標按鈕時,窗口應該出現帶勾號的圖標。 這裏是我的代碼如何從可繪製的圖像中獲取唯一的ID!

private void getAlreadyTicked() { 
    Log.d("getAlreadyTicked", "getAlreadyTicked"); 
    //getting already ticked values for selection page 
    String icon_name_val = null, icon_link_val = null, icon_flag_val = null; 
    List<HomeIconset> icondetails = dbh.geticondetails(); 
    for (HomeIconset sp : icondetails) { 
     icon_name_val = sp.getIcon_name(); 
     icon_link_val = sp.getIcon_link(); 
     icon_flag_val = sp.getIcon_flag(); 

     intSelected = null; 
     intSelected = new Integer[19]; 
     String[] stringIconName = {"paymoneyimg", "recieve", 
       "load", "buysell", 
       "gas", "electricity", 
       "mobileimg", "movie", 
       "buyforex", "sellforex", 
       "travelcard", "send", 
       "flight", "bus", 
       "hotel", "holiday", 
       "gold", "vehicleloan", 
       "personalloan", "insuranceimg"}; 

     for (int i = 0; i < 19; i++) { 
      String imgName; 
      Drawable drawableImgName = null; 
      Bitmap result; 
      Drawable[] drawableImgNameArray = new Drawable[19]; 
      if (icon_name_val.equals(stringIconName[i])) { 
       int resID = getResources().getIdentifier(icon_name_val, "drawable", getActivity().getPackageName()); 
       Bitmap bitmapbus = BitmapFactory.decodeResource(getResources(), R.drawable.bus); 

       Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), resID); 
       Bitmap resized = Bitmap.createScaledBitmap(bitmap1, bitmapbus.getWidth(), bitmapbus.getHeight(), true); 

       Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.tick); 
       // Bitmap resultBitmap = Bitmap.createBitmap(bitmap2.getWidth(), bitmap2.getHeight(), Bitmap.Config.ARGB_8888); 
       Bitmap resultBitmap = Bitmap.createBitmap(bitmapbus.getWidth(), bitmapbus.getHeight(), Bitmap.Config.ARGB_8888); 
       Canvas c = new Canvas(resultBitmap); 
       c.drawBitmap(resized, 0, 0, null); 
       c.drawBitmap(bitmap2, 0, 0, null); 
       drawableImgName = new BitmapDrawable(getResources(), resultBitmap); 

       result = resultBitmap; 
       ImageView im = new ImageView(getActivity()); 
       //im.setTag(icon_name_val+i); 
       im.setImageDrawable(drawableImgName); 
       // String name = getActivity().getResources().getResourceEntryName(drawableImgName); 
       Log.d("tickedimage", String.valueOf(String.valueOf(drawableImgName)) + " ..." + im.getResources()); 



      } else { 
       int resID = getResources().getIdentifier(icon_name_val, "drawable", getActivity().getPackageName()); 
       Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), resID); 

       drawableImgName = getResources().getDrawable(getResources().getIdentifier(icon_name_val, "drawable", getActivity().getPackageName())); 
       result = bitmap1; 
      } 
      // drawableImgNameArray[i]=drawableImgName; 
      // Bitmap bitmap = BitmapFactory.decodeResource(getResources(), drawableImgNameArray[19]); 


      intSelected[i] = getResources().getIdentifier(drawableImgName, "drawable", myContext.getPackageName()); 

     } 
     for (int j = 0; j < intSelected.length; j++) { 
      Log.d("imgarraysf", String.valueOf(intSelected[j])); 
     } 


    } 
} 
+0

intSelected [i] = getResources()。getIdentifier(drawableImgName,「drawable」,myContext.getPackageName());這裏我需要替換drawableImgName,因爲參數應該是一個字符串 – gStephin

回答

0

如果我理解正確的代碼,你有問題,使用icon_name_val。 也就是, intSelected[i] = getResources().getIdentifier(icon_name_val, "drawable", myContext.getPackageName());

+0

雅,但是,我需要將drawableImgName的值設置爲整數數組。 – gStephin

+0

是否要將整數數組中的drawable轉換?爲什麼? – Vyacheslav

+0

這裏drawableimagename由兩個其他圖像組成,這個drawableimagename不存儲在任何地方... – gStephin