西城网站建设,移动网站建设报价表,艺之都网站建设微信app开发,深圳公布最新出行政策引言通常我们会在项目中频繁获取color、raw、drawable、mipmap、string等资源文件。因此#xff0c;今天整理下获取资源文件的工具类方法。最新通用方法ContextCompat.getColor(this,R.color.activity_bg);ContextCompat.getDrawable(this,R.drawable.leak_canary_icon);最近在…引言通常我们会在项目中频繁获取color、raw、drawable、mipmap、string等资源文件。因此今天整理下获取资源文件的工具类方法。最新通用方法ContextCompat.getColor(this,R.color.activity_bg);ContextCompat.getDrawable(this,R.drawable.leak_canary_icon);最近在写程序的时候遇到了一个问题就是textview已经指定了drawableTop的图片但是需要在Java中重新更换一张图片//获取更换的图片Drawable drawablegetResources().getDrawable(R.drawable.close);//setBounds(x,y,width,height)drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());//mDownLoad是控件的名称,setCompoundDrawables(left,top,right,bottom)mDownLoad.setCompoundDrawables(null,drawable,null,null);mDownLoad.setTextColor(ContextCompat.getColor(this, R.color.tap_grey));如果直接使用textView.setCompoundDrawables(null, ContextCompat.getDrawable(this, R.mipmap.homepage_fill_unchecked), null, null);方法即不设置setBounds方法将不会显示图片资源。(这是个坑原理是啥暂时不去深究)图片来源于drawabletextView.setBackgroundDrawable(getResources().getDrawable(R.drawable.search));textView..setBackgroundResource(R.drawable.search);转换字符串为int(颜色)textView.setBackgroundColor(Color.parseColor(#F5F5DC));使用String资源this.getResources().getString(R.string.setIP);简单示例1、Resources resources mContext.getResources();Drawable drawable resources.getDrawable(R.drawable.a);imageview.setBackground(drawable);2、Resources r this.getContext().getResources();Inputstream is r.openRawResource(R.drawable.my_background_image);BitmapDrawable bmpDraw new BitmapDrawable(is);Bitmap bmp bmpDraw.getBitmap();3、Bitmap bmpBitmapFactory.decodeResource(r, R.drawable.icon);Bitmap newb Bitmap.createBitmap( 300, 300, Config.ARGB_8888 );4、InputStream is getResources().openRawResource(R.drawable.icon);Bitmap mBitmap BitmapFactory.decodeStream(is);