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 New. Java file named imageutil. Java, which defines some image processing methods,CodeAs follows:

View plaincopy to clipboardprint?

  1. Package com. Android. tutor;
  2. Import Android. Graphics. Bitmap;
  3. Import Android. Graphics. Canvas;
  4. Import Android. Graphics. lineargradient;
  5. Import Android. Graphics. matrix;
  6. Import Android. Graphics. paint;
  7. Import Android. Graphics. pixelformat;
  8. Import Android. Graphics. porterduduxfermode;
  9. Import Android. Graphics. rect;
  10. Import Android. Graphics. rectf;
  11. Import Android. Graphics. bitmap. config;
  12. Import Android. Graphics. porterduff. mode;
  13. Import Android. Graphics. shader. tilemode;
  14. Import Android. Graphics. drawable. drawable;
  15. Public class imageutil {
  16. // Zoom in and out the image
  17. Public static bitmap zoombitmap (Bitmap bitmap, int W, int h ){
  18. Int width = bitmap. getwidth ();
  19. Int Height = bitmap. getheight ();
  20. Matrix matrix = new matrix ();
  21. Float scalewidht = (float) W/width );
  22. Float scaleheight = (float) h/height );
  23. Matrix. postscale (scalewidht, scaleheight );
  24. Bitmap newbmp = bitmap. createbitmap (bitmap, 0, 0, width, height, matrix, true );
  25. Return newbmp;
  26. }
  27. // Converts drawable to bitmap
  28. Public static bitmap drawabletobitmap (drawable ){
  29. Int width = drawable. getintrinsicwidth ();
  30. Int Height = drawable. getintrinsicheight ();
  31. Bitmap bitmap = bitmap. createbitmap (width, height,
  32. Drawable. getopacity ()! = Pixelformat. opaque? Bitmap. config. argb_8888
  33. : Bitmap. config. rgb_565 );
  34. Canvas canvas = new canvas (Bitmap );
  35. Drawable. setbounds (0, 0, width, height );
  36. Drawable. Draw (canvas );
  37. Return bitmap;
  38. }
  39. // Obtain the rounded corner Image
  40. Public static bitmap getroundedcornerbitmap (Bitmap bitmap, float roundpx ){
  41. Bitmap output = bitmap. createbitmap (bitmap. getwidth (), bitmap
  42. . Getheight (), config. argb_8888 );
  43. Canvas canvas = new canvas (output );
  44. Final int color = 0xff0000242;
  45. Final paint = new paint ();
  46. Final rect = new rect (0, 0, bitmap. getwidth (), bitmap. getheight ());
  47. Final rectf = new rectf (rect );
  48. Paint. setantialias (true );
  49. Canvas. drawargb (0, 0, 0, 0 );
  50. Paint. setcolor (color );
  51. Canvas. drawroundrect (rectf, roundpx, roundpx, paint );
  52. Paint. setxfermode (New porterduxfermode (mode. src_in ));
  53. Canvas. drawbitmap (bitmap, rect, rect, paint );
  54. Return output;
  55. }
  56. // Method for obtaining images with reflections
  57. Public static bitmap createreflectionimagewithorigin (Bitmap bitmap ){
  58. Final int reflectiongap = 4;
  59. Int width = bitmap. getwidth ();
  60. Int Height = bitmap. getheight ();
  61. Matrix matrix = new matrix ();
  62. Matrix. prescale (1,-1 );
  63. Bitmap reflectionimage = bitmap. createbitmap (bitmap,
  64. 0, height/2, width, height/2, matrix, false );
  65. Bitmap bitmapwithreflection = bitmap. createbitmap (width, (height + height/2), config. argb_8888 );
  66. Canvas canvas = new canvas (bitmapwithreflection );
  67. Canvas. drawbitmap (bitmap, 0, 0, null );
  68. Paint deafalutpaint = new paint ();
  69. Canvas. drawrect (0, height, width, height + reflectiongap,
  70. Deafalutpaint );
  71. Canvas. drawbitmap (reflectionimage, 0, height + reflectiongap, null );
  72. Paint paint = new paint ();
  73. Lineargradient shader = new lineargradient (0,
  74. Bitmap. getheight (), 0, bitmapwithreflection. getheight ()
  75. + Reflectiongap, 0x70ffffff, 0x00ffffff, tilemode. Clamp );
  76. Paint. setshader (shader );
  77. // Set the transfer mode to be Porter Duff and destination in
  78. Paint. setxfermode (New porterduxfermode (mode. dst_in ));
  79. // Draw a rectangle using the paint with our linear gradient
  80. Canvas. drawrect (0, height, width, bitmapwithreflection. getheight ()
  81. + Reflectiongap, paint );
  82. Return bitmapwithreflection;
  83. }
  84. }

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

View plaincopy to clipboardprint?

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
  3. Android: Orientation = "vertical"
  4. Android: layout_width = "fill_parent"
  5. Android: layout_height = "fill_parent"
  6. >
  7. <Imageview
  8. Android: Id = "@ + ID/image01"
  9. Android: layout_width = "wrap_content"
  10. Android: layout_height = "wrap_content"
  11. Android: padding = "10px"
  12. />
  13. <Imageview
  14. Android: Id = "@ + ID/image02"
  15. Android: layout_width = "wrap_content"
  16. Android: layout_height = "wrap_content"
  17. Android: padding = "10px"
  18. />
  19. </Linearlayout>

Step 4: Modify the master coreProgram, Imagedemo. Java, the Code is as follows:

View plaincopy to clipboardprint?

  1. Package com. Android. tutor;
  2. Import Android. App. activity;
  3. Import Android. Graphics. Bitmap;
  4. Import Android. Graphics. drawable. drawable;
  5. Import Android. OS. Bundle;
  6. Import Android. widget. imageview;
  7. Public class imagedemo extends activity {
  8. Private imageview mimageview01, mimageview02;
  9. Public void oncreate (bundle savedinstancestate ){
  10. Super. oncreate (savedinstancestate );
  11. Setcontentview (R. layout. Main );
  12. Setupviews ();
  13. }
  14. Private void setupviews (){
  15. Mimageview01 = (imageview) findviewbyid (R. Id. image01 );
  16. Mimageview02 = (imageview) findviewbyid (R. Id. image02 );
  17. // Obtain the wallpaper. The returned value is drawable.
  18. Drawable = getwallpaper ();
  19. // Converts drawable to bitmap
  20. Bitmap bitmap = imageutil. drawabletobitmap (drawable );
  21. // Scale the image
  22. Bitmap zoombitmap = imageutil. zoombitmap (bitmap, 100,100 );
  23. // Obtain the rounded corner Image
  24. Bitmap roundbitmap = imageutil. getroundedcornerbitmap (zoombitmap, 10.0f );
  25. // Obtain the reflected image
  26. Bitmap reflectbitmap = imageutil. createreflectionimagewithorigin (zoombitmap );
  27. // Here bitmap can be converted to drawable
  28. // Drawable rounddrawable = new bitmapdrawable (roundbitmap );
  29. // Drawable reflectdrawable = new bitmapdrawable (reflectbitmap );
  30. // Mimageview01.setbackgrounddrawable (rounddrawable );
  31. // Mimageview02.setbackgrounddrawable (reflectdrawable );
  32. Mimageview01.setimagebitmap (roundbitmap );
  33. Mimageview02.setimagebitmap (reflectbitmap );
  34. }
  35. }

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

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.