01 August 2010
在實做widget的時候,

要在前景把圖片換掉

發現只有

setImageViewBitmap
setImageViewResource
setImageViewUri

這三個method可以用

但是我的圖片是從sqllite中抓出來的!

只能先存成drawable!!

ByteArrayInputStream stream = new ByteArrayInputStream( cursor.getBlob(1)); 
Drawable p_pic = Drawable.createFromStream(stream, "p_pic");
//fnCastToBitmap是等等自己撰寫的function..
Bitmap bitmapTmp = fnCastToBitmap(p_pic);



但是這樣的drawable根本沒有id,無法使用setImageViewResource來抓圖!

所以只好把它轉成bitmap..

如下段CODE:


public Bitmap fnCastToBitmap(Drawable drawable){

//先設定一個config給等等要產生的bitmap,先判斷他的透明度,在判斷他的格式
Bitmap.Config config =
drawable.getOpacity() != PixelFormat.OPAQUE
? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;

//產生一個bitmap...寬跟高如原來圖的一樣
Bitmap bitmap = Bitmap.createBitmap( drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), config );

//在建立一個canvas,並把圖畫在上面去即可!!!
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
drawable.draw(canvas);


return bitmap;
}













blog comments powered by Disqus