如何从可绘制的图像中获取唯一的ID !!

时间:2016-09-19 14:07:44

标签: android bitmap android-drawable

我正在处理从图标选择窗口向主菜单添加图标的过程。如果用户选择了一些图标,则在下次用户点击添加图标按钮时,该窗口应显示带有勾选的图标。  这是我的代码

 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]));
        }


    }
}

1 个答案:

答案 0 :(得分:0)

如果我正确理解您的代码和问题,则必须使用icon_name_val。 那是, intSelected[i] = getResources().getIdentifier(icon_name_val, "drawable", myContext.getPackageName());