ImageView显示图像

来源:互联网 发布:cocos2dx程序员工资 编辑:程序博客网 时间:2024/05/29 16:05

ImageView显示图像有多个方法

setImageResource(int resId);

setImageURI(Uri uri);

setImageDrawable(Drawable drawable);

setImageBitmap(Bitmap bm);

对于资源图像,可以直接应用调用资源来显示,但是有时候我们需要对显示的图像先做某种操作,因此需要把资源转换为BitmapDrawable或者Bitmap

1、从资源中获取BitmapDrawble

[java] view plaincopyprint?
  1. private BitmapDrawable getDrawableFromId(int id){ 
  2.     Resources res = getResources(); 
  3.     return (BitmapDrawable)res.getDrawable(id); 

2、从资源中获取Bitmap图像

[java] view plaincopyprint?
  1. private Bitmap getBitmapFromId(int id){ 
  2.     Resources res = getResources(); 
  3.     BitmapDrawable bitDraw = new BitmapDrawable(res.openRawResource(R.drawable.lander_firing)); 
  4.     Bitmap bm = bitDraw.getBitmap(); 
  5.     return bm; 
  6.     //mImageView.setImageBitmap(bm); 

原创粉丝点击