Android-special image effects Processing

Source: Internet
Author: User

Android-Image Processing Method

Conversion-drawable to bitmap

Zoom

Rounded corner-round corner

Reflection-reflected


Bitmapprcess code:

Package COM. learn. games; </P> <p> 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. g Raphics. bitmap. config; <br/> Import android. graphics. porterduff. mode; <br/> Import android. graphics. shader. tilemode; <br/> Import android. graphics. drawable. drawable; </P> <p> public class bitmapprocess {</P> <p> // zoom <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 (); </P> <p> float Scalewidth = W/(float) width; <br/> float scaleheight = H/(float) height; <br/> matrix. postscale (scalewidth, scaleheight); </P> <p> bitmap bitmap2 = bitmap. createbitmap (bitmap, 0, 0, width, height, matrix, true); <br/> return bitmap2; <br/>}</P> <p> // drawable to bitmap <br/> Public static bitmap drawable2bitmap (drawable) {<br/> int width = drawable. getintrinsicheight (); <br/> int Height = drawable. g Etintrinsicheight (); </P> <p> Bitmap bitmap = bitmap. createbitmap (width, height, drawable. getopacity () <br/>! = Pixelformat. opaque? Bitmap. config. argb_8888: bitmap. config. rgb_565); </P> <p> canvas = new canvas (Bitmap); <br/> drawable. setbounds (0, 0, width, height); <br/> drawable. draw (canvas); </P> <p> return bitmap; <br/>}</P> <p> // round corner bitmap <br/> Public static bitmap getroundcornerbitmap (Bitmap bitmap, float roundpx) {<br/> int width = bitmap. getwidth (); <br/> int Height = bitmap. getheight (); </P> <p> bitmap bitmap2 = bitmap. createbitmap (width, height, config. argb_8888); <br/> canvas = new canvas (bitmap2); </P> <p> final int color = 0xff0000242; <br/> final paint = new paint (); <br/> final rect = new rect (0, 0, width, height ); <br/> final rectf = new rectf (rect); </P> <p> paint. setcolor (color); <br/> paint. setantialias (true); <br/> canvas. drawargb (0, 0, 0, 0); <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 bitmap2; <br/>}</P> <p> // reflect bitmap <br/> Public static bitmap createreflectedbitmap (Bitmap bitmap) {<br/> final int reflectedgap = 4; <br/> int width = bitmap. getwidth (); <br/> int Height = bitmap. getheight (); </P> <p> matrix = new matrix (); <br/> matrix. prescale (1,-1); </P> <p> bitmap reflectedimage = bitmap. createbitmap (bitmap, 0, height/2, width, height/2, matrix, false); <br/> bitmap reflectedbitmap = bitmap. createbitmap (width, (height + height/2), config. argb_8888); </P> <p> canvas = new canvas (reflectedbitmap); <br/> canvas. drawbitmap (bitmap, 0, 0, null); <br/> paint defaultpaint = new paint (); <br/> canvas. drawrect (0, height, width, height + reflectedgap, defaultpaint); <br/> canvas. drawbitmap (reflectedimage, 0, height + reflectedgap, null); </P> <p> paint = new paint (); <br/> lineargradient shader = new lineargradient (0, bitmap. getheight (), 0, <br/> reflectedbitmap. getheight () + reflectedgap, 0x70ffffff, 0x00ffffff, tilemode. clamp); <br/> paint. setshader (shader); <br/> paint. setxfermode (New porterduduxfermode (mode. dst_in); <br/> canvas. drawrect (0, height, width, reflectedbitmap. getheight () + reflectedgap, paint); </P> <p> return reflectedbitmap; <br/>}</P> <p >}< br/>

Java code:

Package COM. learn. games; </P> <p> Import android. app. activity; <br/> Import android. graphics. bitmap; <br/> Import android. graphics. drawable. bitmapdrawable; <br/> Import android. graphics. drawable. drawable; <br/> Import android. OS. bundle; <br/> Import android. widget. imageview; </P> <p> public class mybitmapprocessactivity extends activity {<br/> private imageview imgview1; <br/> private imageview imgview2; <br/> private imageview imgview3; <br/> private imageview imgview4; </P> <p>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); </P> <p> drawbitmap (); <br/>}</P> <p> private void drawbitmap () {<br/> imgview1 = (imageview) findviewbyid (R. id. imgview1); <br/> imgview2 = (imageview) findviewbyid (R. id. imgview2); <br/> imgview3 = (imageview) findviewbyid (R. id. imgview3); <br/> imgview4 = (imageview) findviewbyid (R. id. imgview4); </P> <p> drawable = This. getwallpaper (); </P> <p> Bitmap bitmap = bitmapprocess. drawable2bitmap (drawable); // drawable to bitmap <br/> bitmap zoombitmap = bitmapprocess. zoombitmap (bitmap, 100,100); // zoom <br/> bitmap roundbitmap = bitmapprocess. getroundcornerbitmap (zoombitmap, 10.0f); // round corner <br/> bitmap reflectedbitmap = bitmapprocess. createreflectedbitmap (zoombitmap); // reflect bitmap </P> <p> // drawable to bitmap <br/> bitmap; <br/> Merge (zoombitmap ); <br/> imgview3.setimagebitmap (roundbitmap); <br/> imgview4.setimagebitmap (reflectedbitmap ); </P> <p> // bitmap to drawable <br/> drawable rounddrawable = new bitmapdrawable (roundbitmap); <br/> drawable reflecteddrawable = new bitmapdrawable (scheme ); <br/> imgview1.setbackgrounddrawable (rounddrawable); <br/> imgview2.setbackgrounddrawable (reflecteddrawable); <br/>}< br/>}

XML:

<? 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/> </P> <p> <imageview Android: id = "@ + ID/imgview1" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content"/> </P> <p> <imageview Android: Id = "@ + ID/imgview2" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content"/> </P> <p> <imageview Android: id = "@ + ID/imgview3" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content"/> </P> <p> <imageview Android: Id = "@ + ID/imgview4" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content"/> </P> <p> </linearlayout> <br/>
:

Drawable2bitmap

Zoom

Round Corner

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