Bitmap和Drawable之间的转换

来源:互联网 发布:coc暗黑训练营升级数据 编辑:程序博客网 时间:2024/05/22 03:15
文件布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity" >    <ImageView        android:id="@+id/image"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></RelativeLayout>

源代码:
 package com.example.bitmapanddrawable;

import android.app.Activity;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.Canvas;

import android.graphics.PixelFormat

;import android.graphics.drawable.BitmapDrawable;

import android.graphics.drawable.Drawable;

import android.os.Bundle;

import android.widget.ImageView;

public class MainActivity extends Activity {

           ImageView image;

         @Overrideprotected void onCreate(Bundle savedInstanceState) {

                    super.onCreate(savedInstanceState);

                   Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);

                   Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);

                    setContentView(R.layout.activity_main);

                   image = (ImageView) findViewById(R.id.image);

                   image.setImageBitmap(convertDrawable2BitmapByCanvas(drawable));

                 }

                public Bitmap convertDrawable2BitmapByCanvas(Drawable drawable) {

                   Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight(),drawable.getOpacity() !=          

                                                  PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888: Bitmap.Config.RGB_565);

                  Canvas canvas = new Canvas(bitmap);

                 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());drawable.draw(canvas);return bitmap;

           }

          public Drawable convertBitmap2Drawable(Bitmap bitmap) {

                    BitmapDrawable bd = new BitmapDrawable(this.getResources(), bitmap);

                      // 因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。

                     return bd;

          }

          public Bitmap convertDrawable2BitmapSimple(Drawable drawable) {

                    BitmapDrawable bd = (BitmapDrawable) drawable;

                  return bd.getBitmap();

         }

         // 从资源中获取Bitmap
        public static Bitmap getBitmapFromResources(Activity act, int resId) {
            Resources res = act.getResources();
            return BitmapFactory.decodeResource(res, resId);
         }

        // byte[] → Bitmap
        public static Bitmap convertBytes2Bimap(byte[] b) {
          if (b.length == 0) {
              return null;
          }
         return BitmapFactory.decodeByteArray(b, 0, b.length);
       }

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

}

 

 

 

 

原创粉丝点击