Android开发之通过ImageView名称从文件夹拿到ImageView的Drawable对象的方法
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Android开发之通过ImageView名称从文件夹拿到ImageView的Drawable对象的方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                代碼如下:
先看從Drawable文件夾中取出ImageView的Drawable對象
package com.Yhsh.mobile.agreepayment.utils;import android.content.Context; import android.graphics.drawable.Drawable;/*** @author :下一頁5(輕飛揚)* 創建時間:2018/6/28.15:28* 根據對應的銀行名稱所在drawable目錄拿到對應的銀行圖標*/ public class GetBankIconUtils {/*** 獲取銀行圖標*/public static Drawable getResBankDrawable(Context context, String bankNameString) {//圖片的名稱不包含后綴String bankName = "unknown";try {if (bankNameString != null) {switch (bankNameString) {case "?":bankName = "unknown";break;case "北京銀行":bankName = "bank_bj";break;case "光大銀行":bankName = "bank_gd";break;case "廣發銀行":bankName = "bank_gf";break;case "工商銀行":bankName = "bank_gs";break;case "華夏銀行":bankName = "bank_hx";break;case "建設銀行":bankName = "bank_js";break;case "交通銀行":bankName = "bank_jt";break;case "民生銀行":bankName = "bank_ms";break;case "農業銀行":bankName = "bank_ny";break;case "平安銀行":bankName = "bank_pa";break;case "浦發銀行":bankName = "bank_pf";break;case "上海銀行":bankName = "bank_sh";break;case "興業銀行":bankName = "bank_xy";break;case "郵政儲蓄":bankName = "bank_yz";break;case "中國銀行":bankName = "bank_zg";break;case "招商銀行":bankName = "bank_zs";break;case "中信銀行":bankName = "bank_zx";break;default:break;}}} catch (Exception e) {e.printStackTrace();}//bankName圖片的名稱不包含后綴,drawable返回圖片的類型,context.getPackageName()拿到項目的包名int resID = context.getResources().getIdentifier(bankName, "drawable", context.getPackageName());Drawable drawable = context.getResources().getDrawable(resID);return drawable;} }?
2.再看從access資源文件夾中取出ImageView的Drawable對象:
?
/*** 獲取銀行圖標*/public static Drawable getResBankDrawable(Context context, String bank_name) {InputStream is = null;String res_name = "unknown.png";//帶后綴的圖片的全名稱try {if (bank_name != null) {switch (bank_name) {case "?":res_name = "unknown.png";break;case "北京銀行":res_name = "bank_bj.png";break;case "光大銀行":res_name = "bank_gd.png";break;case "廣發銀行":res_name = "bank_gf.png";break;case "工商銀行":res_name = "bank_gs.png";break;case "華夏銀行":res_name = "bank_hx.png";break;case "建設銀行":res_name = "bank_js.png";break;case "交通銀行":res_name = "bank_jt.png";break;case "民生銀行":res_name = "bank_ms.png";break;case "農業銀行":res_name = "bank_ny.png";break;case "平安銀行":res_name = "bank_pa.png";break;case "浦發銀行":res_name = "bank_pf.png";break;case "上海銀行":res_name = "bank_sh.png";break;case "興業銀行":res_name = "bank_xy.png";break;case "郵政儲蓄":res_name = "bank_yz.png";break;case "中國銀行":res_name = "bank_zg.png";break;case "招商銀行":res_name = "bank_zs.png";break;case "中信銀行":res_name = "bank_zx.png";break;}}is = context.getAssets().open(res_name);} catch (IOException e) {e.printStackTrace();}Bitmap bitmap = BitmapFactory.decodeStream(is);Drawable drawable = new BitmapDrawable(bitmap);return drawable;}?
總結
以上是生活随笔為你收集整理的Android开发之通过ImageView名称从文件夹拿到ImageView的Drawable对象的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: Android开发之常用的自定义输入框之
 - 下一篇: Android开发之ListView子布