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 New. Java file named imageutil. Java, which defines some image processing methods,CodeAs follows:
View plaincopy to clipboardprint?
-
- Package com. Android. tutor;
-
- Import Android. Graphics. Bitmap;
-
- Import Android. Graphics. Canvas;
-
- Import Android. Graphics. lineargradient;
-
- Import Android. Graphics. matrix;
-
- Import Android. Graphics. paint;
-
- Import Android. Graphics. pixelformat;
-
- Import Android. Graphics. porterduduxfermode;
- Import Android. Graphics. rect;
-
- Import Android. Graphics. rectf;
-
- Import Android. Graphics. bitmap. config;
-
- Import Android. Graphics. porterduff. mode;
-
- Import Android. Graphics. shader. tilemode;
-
- Import Android. Graphics. drawable. drawable;
-
- Public class imageutil {
-
-
- // 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 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;
-
- }
-
- // 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;
-
-
- }
-
-
- // Obtain the 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;
-
- }
-
- }
Step 3: Modify the main. xml layout file and put two imageview controls. The Code is as follows:
View plaincopy to clipboardprint?
-
- <? XML version = "1.0" encoding = "UTF-8"?>
-
- <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
-
- Android: Orientation = "vertical"
-
- Android: layout_width = "fill_parent"
-
- Android: layout_height = "fill_parent"
-
- >
-
- <Imageview
-
- Android: Id = "@ + ID/image01"
-
- Android: layout_width = "wrap_content"
-
- Android: layout_height = "wrap_content"
- Android: padding = "10px"
-
- />
-
- <Imageview
-
- Android: Id = "@ + ID/image02"
-
- Android: layout_width = "wrap_content"
-
- Android: layout_height = "wrap_content"
-
- Android: padding = "10px"
-
- />
-
- </Linearlayout>
Step 4: Modify the master coreProgram, Imagedemo. Java, the Code is as follows:
View plaincopy to clipboardprint?
-
- Package com. Android. tutor;
-
- Import Android. App. activity;
- Import Android. Graphics. Bitmap;
-
- Import Android. Graphics. drawable. drawable;
-
- Import Android. OS. Bundle;
-
- Import Android. widget. imageview;
-
- Public class imagedemo extends activity {
-
- Private imageview mimageview01, mimageview02;
-
-
- 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 = imageutil. drawabletobitmap (drawable );
-
- // Scale the image
-
- Bitmap zoombitmap = imageutil. zoombitmap (bitmap, 100,100 );
-
- // Obtain the rounded corner Image
-
- Bitmap roundbitmap = imageutil. getroundedcornerbitmap (zoombitmap, 10.0f );
-
- // Obtain the reflected image
- Bitmap reflectbitmap = imageutil. 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 );
- }
-
-
-
- }
Step 5: run the above project and check the effect as follows:
OK !!
Author: android_tutor
Source: http://blog.csdn.net/Android_Tutor/archive/2010/11/02/5981753.aspx