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;
}