總網頁瀏覽量

關於我自己

我的相片
人生的必修課是接受無常,人生的選修課是放下執著。

2012年3月10日 星期六

Bitmap + Drawable 使用與轉換

建立Drawable
Drawable d = getResources().getDrawable(R.drawable.ic_launcher);

建立Bitmap

➀ 型態為int的Drawable → Bitmap
    Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

➁ Drawable → Bitmap
    Bitmap drawableToBitmap(Drawable drawable)
    {         
        Bitmap.Config c =drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
        Bitmap bitmap = Bitmap.createBitmap( drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),  c);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.draw(canvas);
        return bitmap;
    }

➂ File → Bitmap
    String filePic = "/mnt/sdcard/dcim/sample.png";
    File f = new File(filePic);
    if(f.exists())
    {
        Bitmap bm = BitmapFactory.decodeFile(f);
    }

➃ FileDescriptor → Bitmap
    Bitmap bm = BitmapFactory.decodeFileDescriptor(FileDescriptor fd);

➄ Stream → Bitmap
    try
    {
        URL url = new URL(imageUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.connect();
        InputStream is = conn.getInputStream();
        Bitmap bm = BitmapFactory.decodeStream(is);
        is.close();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }

⑥ byte[] → Bitmap
    byte[] b;
    if(b.length!=0)
    {
        Bitmap bm = BitmapFactory.decodeByteArray(b, 0, b.length);
    }

建立BitmapDrawable
BitmapDrawable bmd = new BitmapDrawable (bm);

=====================其它轉換=====================
Drawable → BitmapDrawabl → Bitmap
    BitmapDrawable  bd = (BitmapDrawable)d;
    Bitmap bitmap = bd.getBitmap();

Bitmap → Drawable
    Drawable drawable = new BitmapDrawable(bm);

Bitmap → byte[]
    byte[] BitmapToByte(Bitmap bm)
    {
            ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
            bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
            return baos.toByteArray();
    }

=====================顯示=====================
ImageView iv = (ImageView) findViewById (R.id.iv_pic); iv.setImageBitmap(Bitmap bm);
iv.setImageResource(int resId);
iv.setImageDrawable(Drawable drawable);
iv.setImageDrawable(BitmapDrawable bmd);
iv.setImageBitmap(Bitmap bm);

=====================附錄=====================


➀ BitmapFactory.decodeByteArray(byte[] data, int offset, int length)
➁ BitmapFactory.decodeByteArray(byte[] data, int offset, int length, BitmapFactory.Option opts)
➂ BitmapFactory.decodeFile(String pathName)
➃ BitmapFactory.decodeFile(String pathName, BitmapFactory.Option opts)
➄ BitmapFactory.decodeFileDescriptor(FileDescriptor fd)
➅ BitmapFactory.decodeFileDescriptor(FileDescriptor fd, Rect outPadding, BitmapFactory.Option opts)
➆ BitmapFactory.decodeResource(Resources res, int id)
➇ BitmapFactory.decodeResource(Resources res, int id, BitmapFactory.Option opts)
➈ BitmapFactory.decodeStream(InputStream Is)
➉ BitmapFactory.decodeStream(InputStream Is, Rect outPadding, BitmapFactory.Option opts)

沒有留言:

張貼留言