In Android, Bitmap, byte [], Drawable converts each other, androiddrawable

Source: Internet
Author: User

In Android, Bitmap, byte [], Drawable converts each other, androiddrawable
1. Related Concepts

1. Drawable is a printable object. It may be a BitmapDrawable, A ShapeDrawable, or LayerDrawable ), based on the drawing requirements, we can create corresponding printable objects.
2. Canvas, the target area of the drawing, used for drawing
3. Bitmap for graph Processing
4. Matrix
Ii. Bitmap1. Obtain Bitmap from resources
Resources res = getResources();Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.icon);
2. Bitmap → byte []
public byte[] Bitmap2Bytes(Bitmap bm) {  ByteArrayOutputStream baos = new ByteArrayOutputStream();  bm.compress(Bitmap.CompressFormat.PNG, 100, baos);  return baos.toByteArray();}
3. byte [] → Bitmap
public Bitmap Bytes2Bimap(byte[] b) {        if (b.length != 0) {            return BitmapFactory.decodeByteArray(b, 0, b.length);        } else {            return null;        }    }
4. Bitmap Scaling
public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) {        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;    }
5. Convert Drawable into Bitmap
Public static Bitmap drawableToBitmap (Drawable drawable) {// obtain the length and width of drawable int w = drawable. getIntrinsicWidth (); int h = drawable. getIntrinsicHeight (); // retrieves the drawable color format Bitmap. config config = drawable. getOpacity ()! = PixelFormat. OPAQUE? Bitmap. config. ARGB_8888: Bitmap. config. RGB_565; // create the corresponding bitmap Bitmap bitmap = Bitmap. createBitmap (w, h, config); // create the Canvas canvas Canvas = new Canvas (bitmap); drawable. setBounds (0, 0, w, h); // draw the drawable content into the canvas. draw (canvas); return bitmap ;}
6. Obtain the rounded corner image:
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {        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;    }
7. Get images with reflections
public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {        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;    }
Iii. Drawable1. Converting Bitmap to Drawable
Bitmap bm = xxx; // xxx is obtained based on your situation
BitmapDrawable bd = new BitmapDrawable (getResource (), bm );
// Because BtimapDrawable is a subclass of Drawable, you can directly use the bd object.
2. Drawable Scaling
Public static Drawable zoomDrawable (Drawable drawable, int w, int h) {int width = drawable. getIntrinsicWidth (); int height = drawable. getIntrinsicHeight (); // drawable is converted to bitmap Bitmap oldbmp = drawableToBitmap (drawable); // The Matrix object Matrix = new matrix () used to create operation images (); // calculate the zoom ratio float sx = (float) w/width); float sy = (float) h/height); // set the zoom ratio matrix. postScale (sx, sy); // create a new bitmap, whose content is bitmap newbmp = Bitmap after the original Bitmap is scaled. createBitmap (oldbmp, 0, 0, width, height, matrix, true); return new BitmapDrawable (newbmp );}

This article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.