A small example of Android Image Processing (type conversion, proportional scaling, reflection, and rounded corners)

Source: Internet
Author: User

1. Zoom in and out the image

Copy codeThe Code is as follows: public static Bitmap zoomBitmap (Bitmap bitmap, int w, int h ){
Int width = bitmap. getWidth ();
Int height = bitmap. getHeight ();
Matrix matrix = new Matrix ();
Float scaleWidht = (float) w/width );
Float scaleHeight = (float) h/height );
Matrix. postScale (scaleWidht, scaleHeight );
Bitmap newbmp = Bitmap. createBitmap (bitmap, 0, 0, width, height, matrix, true );
Return newbmp;
}

2. How to obtain the rounded corner Image

Copy codeThe Code is as follows: public static Bitmap getRoundedCornerBitmap (Bitmap bitmap, float roundPx ){

Bitmap output = Bitmap. createBitmap (bitmap. getWidth (), bitmap. getHeight (), Config. ARGB_8888 );
Canvas canvas = new Canvas (output );

Final int color = 0xff0000242;
Final Paint paint = new Paint ();
Final Rect rect = new Rect (0, 0, bitmap. getWidth (), bitmap. getHeight ());
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 porterduxfermode (Mode. SRC_IN ));
Canvas. drawBitmap (bitmap, rect, rect, paint );

Return output;
}

3. How to obtain images with reflections

Copy codeThe Code is as follows: public static Bitmap createReflectionImageWithOrigin (Bitmap bitmap ){
Final int reflectionGap = 4;
Int width = bitmap. getWidth ();
Int height = bitmap. getHeight ();

Matrix matrix = new Matrix ();
Matrix. preScale (1,-1 );

Bitmap reflectionImage = Bitmap. createBitmap (bitmap, 0, height/2, width, height/2, matrix, false );

Bitmap bitmapWithReflection = Bitmap. createBitmap (width, (height + height/2), Config. ARGB_8888 );

Canvas canvas = new Canvas (bitmapWithReflection );
Canvas. drawBitmap (bitmap, 0, 0, null );
Paint deafalutPaint = new Paint ();
Canvas. drawRect (0, height, width, height + reflectionGap, deafalutPaint );

Canvas. drawBitmap (reflectionImage, 0, height + 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 porterduxfermode (Mode. DST_IN ));
// Draw a rectangle using the paint with our linear gradient
Canvas. drawRect (0, height, width, bitmapWithReflection. getHeight ()
+ ReflectionGap, paint );

Return bitmapWithReflection;
}

4. Convert Drawable to Bitmap

Copy codeThe Code is as follows: public static Bitmap drawableToBitmap (Drawable drawable ){
Int width = drawable. getIntrinsicWidth ();
Int height = drawable. getIntrinsicHeight ();
Bitmap bitmap = Bitmap. createBitmap (width, height,
Drawable. getOpacity ()! = PixelFormat. OPAQUE? Bitmap. Config. ARGB_8888: Bitmap. Config. RGB_565 );
Canvas canvas = new Canvas (bitmap );
Drawable. setBounds (0, 0, width, height );
Drawable. draw (canvas );
Return bitmap;
}

Related 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.