Android expert advanced tutorial (22)-highlights of several special image effects processing in Android !!

Source: Internet
Author: User

Hello everyone, this section will share with you some tips for special image effects processing in Android, such as rounded corners, reflections, image scaling, and drawable conversion to bitmap, bitmap is converted to drawable.

Let's talk a little bit about it. Let's explain the example of today. In this example, we first get the wallpaper (getwallpaper () and then process some special effects of the current wallpaper. Let's proceed step by step:

Step 1: Create an android project named imagedemo. The project structure is as follows:

Step 2: Create a. Java file named imageutil. java. Define some image processing methods in the file. The Code is as follows:

Package COM. android. tutor; <br/> Import android. graphics. bitmap; <br/> Import android. graphics. canvas; <br/> Import android. graphics. lineargradient; <br/> Import android. graphics. matrix; <br/> Import android. graphics. paint; <br/> Import android. graphics. pixelformat; <br/> Import android. graphics. porterduxfermode; <br/> Import android. graphics. rect; <br/> Import android. graphics. rectf; <br/> Import android. Graphics. bitmap. config; <br/> Import android. graphics. porterduff. mode; <br/> Import android. graphics. shader. tilemode; <br/> Import android. graphics. drawable. drawable; <br/> public class imageutil {</P> <p> // zoom in and out the image <br/> Public static bitmap zoombitmap (Bitmap bitmap, int W, int H) {<br/> int width = bitmap. getwidth (); <br/> int Height = bitmap. getheight (); <br/> matrix = new matrix (); <br/> float scalew Idht = (float) W/width); <br/> float scaleheight = (float) h/height); <br/> matrix. postscale (scalewidht, scaleheight); <br/> bitmap newbmp = bitmap. createbitmap (bitmap, 0, 0, width, height, matrix, true); <br/> return newbmp; <br/>}< br/> // converts drawable to bitmap <br/> Public static bitmap drawabletobitmap (drawable) {<br/> int width = drawable. getintrinsicwidth (); <br/> int Height = drawabl E. getintrinsicheight (); <br/> Bitmap bitmap = bitmap. createbitmap (width, height, <br/> drawable. getopacity ()! = Pixelformat. opaque? Bitmap. config. argb_8888 <br/>: bitmap. config. rgb_565); <br/> canvas = new canvas (Bitmap); <br/> drawable. setbounds (0, 0, width, height); <br/> drawable. draw (canvas); <br/> return bitmap; </P> <p >}</P> <p> // method for obtaining the rounded corner image <br/> Public static bitmap getroundedcornerbitmap (Bitmap bitmap, float roundpx) {</P> <p> bitmap output = bitmap. createbitmap (bitmap. getwidth (), bitmap <br/>. getheight (), config. argb_8888); <br/> canvas = new canvas (output); </P> <p> final int color = 0xff0000242; <br/> final paint = new paint (); <br/> final rect = new rect (0, 0, bitmap. getwidth (), bitmap. getheight (); <br/> final rectf = new rectf (rect); </P> <p> paint. setantialias (true); <br/> canvas. drawargb (0, 0, 0, 0); <br/> paint. setcolor (color); <br/> canvas. drawroundrect (rectf, roundpx, roundpx, paint); </P> <p> paint. setxfermode (New porterduduxfermode (mode. src_in); <br/> canvas. drawbitmap (bitmap, rect, rect, paint); </P> <p> return output; <br/>}< br/> // method for obtaining images with reflections <br/> Public static bitmap createreflectionimagewithorigin (Bitmap bitmap) {<br/> final int reflectiongap = 4; <br/> int width = bitmap. getwidth (); <br/> int Height = bitmap. getheight (); </P> <p> matrix = new matrix (); <br/> matrix. prescale (1,-1); </P> <p> bitmap reflectionimage = bitmap. createbitmap (bitmap, <br/> 0, height/2, width, height/2, matrix, false); </P> <p> bitmap bitmapwithreflection = bitmap. createbitmap (width, (height + height/2), config. argb_8888); </P> <p> canvas = new canvas (bitmapwithreflection); <br/> canvas. drawbitmap (bitmap, 0, 0, null); <br/> paint deafalutpaint = new paint (); <br/> canvas. drawrect (0, height, width, height + reflectiongap, <br/> deafalutpaint); </P> <p> canvas. drawbitmap (reflectionimage, 0, height + reflectiongap, null); </P> <p> paint = new paint (); <br/> lineargradient shader = new lineargradient (0, <br/> bitmap. getheight (), 0, bitmapwithreflection. getheight () <br/> + reflectiongap, 0x70ffffff, 0x00ffffff, tilemode. clamp); <br/> paint. setshader (shader); <br/> // set the transfer mode to be Porter Duff and destination in <br/> paint. setxfermode (New porterduduxfermode (mode. dst_in); <br/> // draw a rectangle using the paint with our linear gradient <br/> canvas. drawrect (0, height, width, bitmapwithreflection. getheight () <br/> + reflectiongap, paint); </P> <p> return bitmapwithreflection; <br/>}</P> <p >}< br/>

Step 3: Modify the main. xml layout file and put two imageview controls. The Code is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <imageview <br/> Android: id = "@ + ID/image01" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: padding = "10px" <br/> <imageview <br/> Android: Id = "@ + ID/image02" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: padding = "10px" <br/> </linearlayout> <br/>

Step 4: Modify the main core program, imagedemo. java. The Code is as follows:

Package COM. android. tutor; <br/> Import android. app. activity; <br/> Import android. graphics. bitmap; <br/> Import android. graphics. drawable. drawable; <br/> Import android. OS. bundle; <br/> Import android. widget. imageview; <br/> public class imagedemo extends activity {<br/> private imageview mimageview01, mimageview02; </P> <p> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> setupviews (); <br/>}</P> <p> private void setupviews () {<br/> mimageview01 = (imageview) findviewbyid (R. id. image01); <br/> mimageview02 = (imageview) findviewbyid (R. id. image02); </P> <p> // the return value of the wallpaper is drawable <br/> drawable = getwallpaper (); <br/> // convert drawable to bitmap <br/> Bitmap bitmap = imageutil. drawabletobitmap (drawable); <br/> // scale the image <br/> bitmap zoombitmap = imageutil. zoombitmap (bitmap, 100,100); <br/> // obtain the rounded corner image <br/> bitmap roundbitmap = imageutil. getroundedcornerbitmap (zoombitmap, 10.0f); <br/> // get the reflected image <br/> bitmap reflectbitmap = imageutil. createreflectionimagewithorigin (zoombitmap); <br/> // here bitmap can be converted to drawable <br/> // drawable rounddrawable = new bitmapdrawable (roundbitmap ); <br/> // drawable reflectdrawable = new bitmapdrawable (reflectbitmap); <br/> // round (rounddrawable); <br/> // round (reflectdrawable ); </P> <p> mimageview01.setimagebitmap (roundbitmap); <br/> mimageview02.setimagebitmap (reflectbitmap); <br/>}</P> <p>}

Step 5: run the above project and check the effect as follows:

 

OK !!

 

References: http://wiki.impjq.net/doku.php? Id = Code: Android-code: Image-convert & Rev = 1275640889 & mddo = print

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.