Drawable -作为Android平下通用的图形对象,它可以装载常用格式的图像,比如GIF、PNG、JPG,当然也支持BMP,当然还提供一些高级的可视化对象,比如渐变、图形等。

   Bitmap -称作位图,一般位图的文件格式后缀为bmp,当然编码器也有很多如RGB565、RGB888。作为一种像素的显示对象执行效率高,但是缺点也很明显存储效率低。我们理解为一种存储对象比较好。

android在处理一写图片资源的时候,会进行一些类型的转换,现在有空整理一下:

1、Drawable → Bitmap 的简单方法 

Drawable d = XXX;

BitmapDrawable  bd =(BitmapDrawable)d;

Bitmap  b =bd.getBitmap();

可简化为:((BitmapDrawable)res.getDrawable(R.drawable.youricon)).getBitmap();
 
Java代码
public static Bitmap drawableToBitmap(Drawable drawable){   
           
            Bitmap bitmap= Bitmap   
                                .createBitmap(   
                                                    drawable.getIntrinsicWidth(),   
                                                    drawable.getIntrinsicHeight(),   
                                                    drawable.getOpacity() != PixelFormat.OPAQUE ?Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);  //按指定参数创建一个空的Bitmap对象
            Canvas canvas= new Canvas(bitmap);   
            //canvas.setBitmap(bitmap);   
            drawable.setBounds(0,0, drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());   
            drawable.draw(canvas);   
            returnbitmap;   
}   


3.Bitmap→Drawable    的简单方法
BitmapDrawable bitmapDrawable= (BitmapDrawable)bitmap;    
Drawable drawable = (Drawable)bitmapDrawable;     
     
Bitmap bitmap = newBitmap (...);    
Drawable drawable = new BitmapDrawable(bitmap);  


3、从资源中获取Bitmap
Java代码
Bitmap bmp=BitmapFactory.decodeResource(getResources(),R.drawable.pic);   

4、Bitmap → Byte[]
Java代码
private byte[] Bitmap2Bytes(Bitmapbm){   
    ByteArrayOutputStream baos =new ByteArrayOutputStream(); 
    bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
    returnbaos.toByteArray();   
}   

5、 byte[] → Bitmap
Java代码
private Bitmap Bytes2Bimap(byte[]b){   
                if(b.length!=0){   
                    returnBitmapFactory.decodeByteArray(b, 0,b.length);   
                }   
                else{   
                    returnnull;   
                }   
        }

6、保存Bmp文件
static  boolean  saveBitmap2file(Bitmap  bmp,String  filename){
CompressFormat  format=  Bitmap.CompressFormat.JPEG;
int  quality  =  100;
OutputStream  stream  =  null;
try  {
            stream  =  new  FileOutputStream("/sdcard/"  +  filename);
}  catch  (FileNotFoundException  e)  {
            e.printStackTrace();
}
return  bmp.compress(format,  quality,  stream);
}
7、将图片按自己的要求缩放
//  图片源
Bitmap  bm  =  BitmapFactory.decodeStream(getResources().openRawResource(R.drawable.dog));
//  获得图片的宽高
int  width  =  bm.getWidth();
int  height  =  bm.getHeight();
//  设置想要的大小
int  newWidth  =  320;
int  newHeight  =  480;
//  计算缩放比例
float  scaleWidth  =  ((float)  newWidth)  /  width;
float  scaleHeight  =  ((float)  newHeight)  /  height;
//  取得想要缩放的matrix参数
Matrix  matrix  =  new  Matrix();
matrix.postScale(scaleWidth,  scaleHeight);
//  得到新的图片
Bitmap  newbm  =  Bitmap.createBitmap(bm,  0,  0,  width,  height,  matrix,
true);
//  放在画布上
canvas.drawBitmap(newbm,  0,  0,  paint);
相关知识链接:http://www.eoeandroid.com/thread-3162-1-1.html
8、//放大缩小图片
public  static  Bitmap  zoomBitmap(Bitmap  bitmap,int  w,int  h){
//获得原始图片宽高
int  width  =  bitmap.getWidth();
int  height  =  bitmap.getHeight();
Matrix  matrix  =  new  Matrix();
//计算缩放比(目标宽高/原始宽高)
float  scaleWidht  =  ((float)w  /  width);
float  scaleHeight  =  ((float)h  /  height);
matrix.postScale(scaleWidht,  scaleHeight);
Bitmap  newbmp  =  Bitmap.createBitmap(bitmap,  0,  0,  width,  height,  matrix,
true);
return  newbmp;
}
9、//将Drawable转化为Bitmap
public  static  Bitmap  drawableToBitmap(Drawable  drawable){

int  width  =  drawable.getIntrinsicWidth();
int  height  =  drawable.getIntrinsicHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height,
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);//创建一个指定高、宽的可变的Bitmap图像
Canvas  canvas  =  new  Canvas(bitmap);
drawable.setBounds(0,0,width,height);
drawable.draw(canvas);
return  bitmap;
}
10、//获得圆角图片的方法
public  static  Bitmap  getRoundedCornerBitmap(Bitmap  bitmap,float  roundPx){
Bitmap  output  =  Bitmap.createBitmap(bitmap.getWidth(),  bitmap
.getHeight(),  Config.ARGB_8888);
Canvas  canvas  =  new  Canvas(output);
final  int  color  =  0xff424242;
final  Paint  paint  =  new  Paint();
final  Rect  rect  =  new  Rect(0,  0,  bitmap.getWidth(),  bitmap.getHeight());
final  RectF  rectF  =  new  RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0,  0,  0,  0);
paint.setColor(color);
canvas.drawRoundRect(rectF,  roundPx,  roundPx,  paint);
paint.setXfermode(new  PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap,  rect,  rect,  paint);
return  output;
}
11、//获得带倒影的图片方法
public  static  Bitmap  createReflectionImageWit hOrigin(Bitmap  bitmap){
final  int  reflectionGap  =  4;
int  width  =  bitmap.getWidth();
int  height  =  bitmap.getHeight();
Matrix  matrix  =  new  Matrix();
matrix.preScale(1, -1);
Bitmap  reflectionImage  =  Bitmap.createBitmap(bitmap,
0,  height/2,  width,  height/2,  matrix,  false);
Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height/2),
Config.ARGB_8888);
Canvas  canvas  =  new  Canvas(bitmapWithReflection);
canvas.drawBitmap(bitmap,  0,  0,  null);
Paint  deafalutPaint  =  new  Paint();
canvas.drawRect(0,  height,width,height  +  reflectionGap,
deafalutPaint);
canvas.drawBitmap(reflectionImage,  0,  height  +  reflectionGap,  null);
Paint  paint  =  new  Paint();
LinearGradient  shader  =  new  LinearGradient(0,
bitmap.getHeight(),  0,  bitmapWithReflection.getHeight()
+  reflectionGap,  0x70ffffff,  0x00ffffff,  TileMode.CLAMP);
paint.setShader(shader);
//  Set  the  Transfer  mode  to  be  porter  duff  and  destination  in
paint.setXfermode(new  PorterDuffXfermode(Mode.DST_IN));
//  Draw  a  rectangle  using  the  paint  with  our  linear  gradient
canvas.drawRect(0,  height,  width,  bitmapWithReflection.getHeight()
+  reflectionGap,  paint);
return  bitmapWithReflection;
}
}
12、图片水印的生成方法
生成水印的过程。其实分为三个环节:第一,载入原始图片;第二,载入水印图片;第三,保存新的图片。
 
    private Bitmap createBitmap(Bitmap src, Bitmap watermark)   
    {   
            String tag ="createBitmap";   
            Log.d( tag, "create a new bitmap");   
            if( src == null )   
            {   
                    return null;   
            }   
 
            int w = src.getWidth();   
            int h = src.getHeight();   
            int ww =watermark.getWidth();   
            int wh =watermark.getHeight();   
            //create the new blankbitmap   
            Bitmap newb = Bitmap.createBitmap( w, h, Config.ARGB_8888);//创建一个新的和SRC长度宽度一样的位图   
            Canvas cv = new Canvas( newb);   
            //draw src into   
            cv.drawBitmap( src, 0, 0, null );//在0,0坐标开始画入src   
            //draw watermark into   
            cv.drawBitmap( watermark, w - ww + 5, h - wh + 5, null);//在src的右下角画入水印   
            //save all clip   
            cv.save( Canvas.ALL_SAVE_FLAG);//保存   
            //store   
            cv.restore();//存储   
            return newb;   
    } 
13、bitmap的用法小结
BitmapFactory.Options  option  =  new  BitmapFactory.Options();
option.inSampleSize  =  2;  //将图片设为原来宽高的1/2,防止内存溢出
Bitmap  bm  =  BitmapFactory.decodeFile("",option);//文件流
URL  url  =  new  URL("");
InputStream  is  =  url.openStream();
Bitmap  bm  =  BitmapFactory.decodeStream(is);

android:scaleType:控制图片如何resized/moved来匹对ImageView的size。ImageView.ScaleType  /

android:scaleType值的意义区别:

CENTER  /center  按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分

显示

CENTER_CROP  /  centerCrop  按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长

(宽)

CENTER_INSIDE  /  centerInside  将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片

长/宽等于或小于View的长/宽

Generated  by  Foxit  PDF  Creator  ?  Foxit  Software

http://www.foxitsoftware.com  For  evaluation  only.

FIT_CENTER  /  fitCenter  把图片按比例扩大/缩小到View的宽度,居中显示

FIT_END  /  fitEnd  把图片按比例扩大/缩小到View的宽度,显示在View的下部分位置

FIT_START  /  fitStart  把图片按比例扩大/缩小到View的宽度,显示在View的上部分位置

FIT_XY  /  fitXY  把图片  不按比例  扩大/缩小到View的大小显示

MATRIX  /  matrix  用矩阵来绘制,动态缩小放大图片来显示。