Android-Image Processing Method

Source: Internet
Author: User
Package com. IMG. util;

Import Android. Graphics. Bitmap;
Import Android. Graphics. bitmap. config;
Import Android. Graphics. Canvas;
Import Android. Graphics. lineargradient;
Import Android. Graphics. matrix;
Import Android. Graphics. paint;
Import Android. Graphics. pixelformat;
Import Android. Graphics. porterduff. mode;
Import Android. Graphics. porterduduxfermode;
Import Android. Graphics. rect;
Import Android. Graphics. rectf;
Import Android. Graphics. shader. tilemode;
Import Android. Graphics. drawable. drawable;

Public class imgutil {
// Zoom in and out the image
Public static bitmap zoombitmap (Bitmap bitmap, int W, int h ){
Int width = bitmap. getwidth ();
Int Height = bitmap. getheight ();
Matrix matrix = new matrix ();
Float scalewidth = (float) W/width );
Float scaleheight = (float) h/height );
Matrix. postscale (scalewidth, scaleheight );
Bitmap newbmp = bitmap. createbitmap (bitmap, 0, 0, width, height,
Matrix, true );
Return newbmp;
}

// Converts drawable to bitmap
Public static bitmap drawabletobitmap (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;
}

// Rounded corner Image
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 = new paint ();
Final rect = new rect (0, 0, bitmap. getwidth (), bitmap. getheight ());
Final 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;
}

// Method for obtaining images with reflections
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;
}
}

 

 

Package com. IMG. util;

Import Android. App. activity;
Import Android. Graphics. Bitmap;
Import Android. Graphics. drawable. drawable;
Import Android. OS. Bundle;
Import Android. widget. imageview;

Public class mainactivity extends activity {
Private imageview mimageview01, mimageview02;

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Setupviews ();
}

Private void setupviews (){
Mimageview01 = (imageview) findviewbyid (R. Id. image01 );
Mimageview02 = (imageview) findviewbyid (R. Id. image02 );

// Obtain the wallpaper. The returned value is drawable.
Drawable = getwallpaper ();
// Converts drawable to bitmap
Bitmap bitmap = imgutil. drawabletobitmap (drawable );
// Scale the image
Bitmap zoombitmap = imgutil. zoombitmap (bitmap, 100,100 );
// Obtain the rounded corner Image
Bitmap roundbitmap = imgutil
. Getroundedcornerbitmap (zoombitmap, 10.0f );
// Obtain the reflected image
Bitmap reflectbitmap = imgutil
. Createreflectionimagewithorigin (zoombitmap );
// Here bitmap can be converted to drawable
// Drawable rounddrawable = new bitmapdrawable (roundbitmap );
// Drawable reflectdrawable = new bitmapdrawable (reflectbitmap );
// Mimageview01.setbackgrounddrawable (rounddrawable );
// Mimageview02.setbackgrounddrawable (reflectdrawable );

Mimageview01.setimagebitmap (roundbitmap );
Mimageview02.setimagebitmap (reflectbitmap );
}
}

 

 

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.