---
title: "Bitmap与Drawable的转换"
author: "kylines"
date: "2012-08-23T18:24:57Z"
tags: []
categories: ["移动开发"]
slug: "bitmap-drawable"
draft: false
lastmod: "2012-08-23T18:24:57Z"
id: 1660184
url: "https://kylines.iteye.com/blog/1660184"
---

> [code=java]/**
> 	 * Bitmap转化为drawable
> 	 * 
> 	 * @param bitmap
> 	 * @return
> 	 */
> 	public static Drawable bitmap2Drawable(Bitmap bitmap) {
> 		return new BitmapDrawable(bitmap);
> 	}
> 
> 	/**
> 	 * Drawable 转 bitmap
> 	 * 
> 	 * @param drawable
> 	 * @return
> 	 */
> 	public static Bitmap drawable2Bitmap(Drawable drawable) {
> 		if (drawable instanceof BitmapDrawable) {
> 			return ((BitmapDrawable) drawable).getBitmap();
> 		} else if (drawable instanceof NinePatchDrawable) {
> 			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;
> 		} else {
> 			return null;
> 		}
> 	}[/code]