Code example for Drawable

Methods: drawgetIntrinsicHeightgetIntrinsicWidthgetOpacitysetBounds

0
	 * @param drawable 
	 * @return 
	 */ 
	public static Bitmap drawableToBitmap(Drawable drawable) {  
		if (drawable != null) {
			int w = drawable.getIntrinsicWidth();  
	        int h = drawable.getIntrinsicHeight();  
	        Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888  
	                : Bitmap.Config.RGB_565;  
	        Bitmap bitmap = Bitmap.createBitmap(w, h, config);  
	        Canvas canvas = new Canvas(bitmap);  
	        drawable.setBounds(0, 0, w, h);  
	        drawable.draw(canvas);  
	        return bitmap;
		}else { 
			return null; 
		} 
    }   
	 
	/** 
	 * Bitmap convert to drawable 
	 * @param bitmap 
	 * @return 
	 */ 
	public static Drawable bitmapToDrawable(Bitmap bitmap){
		if (bitmap != null) {
			return new BitmapDrawable(bitmap);
		}else { 
			return null; 
		} 
	} 
	 
	/** 
	 * Stream convert to bitmap 
	 * @param inputStream 
	 * @return 
	 * @throws Exception 
	 */ 
	public static Bitmap inputStreamToBitmap(InputStream inputStream)
			throws Exception { 
		if (inputStream != null) {
			return BitmapFactory.decodeStream(inputStream);
		}else { 
			return null; 
		} 
	} 
 
	/** 
	 * Bytes convert to bitmap 
	 * @param byteArray 
	 * @return 
	 */ 
	public static Bitmap bytesToBitmap(byte[] byteArray) {
		if (byteArray != null && byteArray.length != 0) {
			return BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
		} else { 
			return null; 
		} 
	} 
 
	/** 
	 * Bytes convert to drawable 
	 * @param byteArray 
	 * @return 
	 */ 
	public static Drawable byteToDrawable(byte[] byteArray) {
		if (byteArray != null && byteArray.length > 0) {
			ByteArrayInputStream ins = new ByteArrayInputStream(byteArray);
			return Drawable.createFromStream(ins, null);
		}else { 
			return null; 
		} 
		 
	} 
 
	/** 
	 * Bitmap convert to bytes 
	 *  
	 * @param byteArray 
	 * @return 
	 */ 
	public static byte[] bitmapToBytes(Bitmap bm) {
		if (bm != null) {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
			byte[] bytes = baos.toByteArray();
			return bytes;
		}else { 
			return null; 
		} 
		 
	} 
 
	/** 
	 * Drawable convert to bytes 
	 * @param drawable 
	 * @return 
	 */ 
	public static byte[] drawableToBytes(Drawable drawable) {
		if (drawable != null) {
			BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
			Bitmap bitmap = bitmapDrawable.getBitmap();
			byte[] bytes = bitmapToBytes(bitmap);
			; 
			return bytes;
		}else { 
			return null; 
		} 
		 
	} 
 
	/** 
	 * Base64 convert to byte[] 
	 */ 
	public static byte[] base64ToBytes(String base64) throws IOException {
		if (base64 != null && !base64.equals("")) {
			byte[] bytes = Base64.decode(base64);
			return bytes;
		}else { 
			return null; 
		} 
	} 
 
	/** 
	 * Bytes convert to Base64 
	 */ 
	public static String bytesToBase64(byte[] bytes) {
		if (bytes != null && bytes.length > 0) {
			String base64 = Base64.encode(bytes);
			return base64;
		}else { 
			return null; 
		} 
	} 
	 
	/** 
	 * Get reflection image from bitmap 
	 * @param bitmap 
	 * @return 
	 */ 
	public static Bitmap getReflectionImageWithBitmap(Bitmap bitmap) {
		if (bitmap != null) {
			final int reflectionGap = 4;  
		    int w = bitmap.getWidth();  
		    int h = bitmap.getHeight();  
		    Matrix matrix = new Matrix();  
		    matrix.preScale(1, -1);  
		    Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, h / 2, w,  
		            h / 2, matrix, false);  
		   
		    Bitmap bitmapWithReflection = Bitmap.createBitmap(w, (h + h / 2),  
		            Config.ARGB_8888);  
		   
		    Canvas canvas = new Canvas(bitmapWithReflection);  
		    canvas.drawBitmap(bitmap, 0, 0, null);  
		    Paint deafalutPaint = new Paint();  
		    canvas.drawRect(0, h, w, h + reflectionGap, deafalutPaint);  
		   
		    canvas.drawBitmap(reflectionImage, 0, h + 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, h, w, bitmapWithReflection.getHeight()  
		            + reflectionGap, paint);  
		    return bitmapWithReflection; 
		}else { 
			return null; 
		} 
	}   
 
	/** 
	 * Get rounded corner image 
	 * @param bitmap 
	 * @param roundPx 5 10 
	 * @return 
	 */ 
	public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
		if (bitmap != null) {
			 int w = bitmap.getWidth();  
			    int h = bitmap.getHeight();  
			    Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888);  
			    Canvas canvas = new Canvas(output);  
			    final int color = 0xff424242;  
			    final Paint paint = new Paint();  
			    final Rect rect = new Rect(0, 0, w, h);  
			    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;  
		}else { 
			return null; 
		} 
	}   
	 
	/** 
	 * Resize the image 
	 * @param bitmap 
	 * @param width 
	 * @param height 
	 * @return 
	 */ 
	public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) {
		if (bitmap != null) {
			int w = bitmap.getWidth();  
		    int h = bitmap.getHeight();  
		    Matrix matrix = new Matrix();  
		    float scaleWidth = ((float) width / w);  
		    float scaleHeight = ((float) height / h);  
		    matrix.postScale(scaleWidth, scaleHeight);  
		    Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);  
		    return newbmp;  
		}else { 
			return null; 
		} 
	} 
	 
	/** 
	 * Resize the drawable 
	 * @param drawable 
	 * @param w 
	 * @param h 
	 * @return 
	 */ 
	public static Drawable zoomDrawable(Drawable drawable, int w, int h) {
		if (drawable != null) {
			int width = drawable.getIntrinsicWidth();  
		    int height = drawable.getIntrinsicHeight();  
		    Bitmap oldbmp = drawableToBitmap(drawable);  
		    Matrix matrix = new Matrix();  
		    float sx = ((float) w / width);  
		    float sy = ((float) h / height);  
		    matrix.postScale(sx, sy);