Tutorial on image special effect processing in Android

Source: Internet
Author: User

Android has a lot of image special effect processing techniques, such as rounded corners and Reflections. We will share with you an example today. In this example, we will first get the wallpaper (getWallpaper ()), then some special effects of the current wallpaper are processed.

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:

 
 
  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.  
  17. // Zoom in and out the image
  18. Public static Bitmap zoomBitmap (Bitmap bitmap, int w, int h ){
  19. Int width = bitmap. getWidth ();
  20. Int height = bitmap. getHeight ();
  21. Matrix matrix = new Matrix ();
  22. Float scaleWidht = (float) w/width );
  23. Float scaleHeight = (float) h/height );
  24. Matrix. postScale (scaleWidht, scaleHeight );
  25. Bitmap newbmp = Bitmap. createBitmap (bitmap, 0, 0, width, height, matrix, true );
  26. Return newbmp;
  27. }
  28. // Converts Drawable to Bitmap
  29. Public static Bitmap drawableToBitmap (Drawable drawable ){
  30. Int width = drawable. getIntrinsicWidth ();
  31. Int height = drawable. getIntrinsicHeight ();
  32. Bitmap bitmap = Bitmap. createBitmap (width, height,
  33. Drawable. getOpacity ()! = PixelFormat. OPAQUE? Bitmap. Config. ARGB_8888
  34. : Bitmap. Config. RGB_565 );
  35. Canvas canvas = new Canvas (bitmap );
  36. Drawable. setBounds (0, 0, width, height );
  37. Drawable. draw (canvas );
  38. Return bitmap;
  39.  
  40. }
  41.  
  42. // Obtain the rounded corner Image
  43. Public static Bitmap getRoundedCornerBitmap (Bitmap bitmap, float roundPx ){
  44.  
  45. Bitmapoutput = Bitmap. createBitmap (bitmap. getWidth (), bitmap
  46. . GetHeight (), Config. ARGB_8888 );
  47. Canvas canvas = new Canvas (output );
  48.  
  49. Final intcolor = 0xff0000242;
  50. Final Paint paint = new Paint ();
  51. Final Rect rect = new Rect (0, 0, bitmap. getWidth (), bitmap. getHeight ());
  52. Final RectF rectF = new RectF (rect );
  53.  
  54. Paint. setAntiAlias (true );
  55. Canvas. drawARGB (0, 0, 0, 0 );
  56. Paint. setColor (color );
  57. Canvas. drawRoundRect (rectF, roundPx, roundPx, paint );
  58.  
  59. Paint. setXfermode (newporterduxfermode (Mode. SRC_IN ));
  60. Canvas. drawBitmap (bitmap, rect, rect, paint );
  61.  
  62. Returnoutput;
  63. }
  64. // Method for obtaining images with reflections
  65. Public static Bitmap createReflectionImageWithOrigin (Bitmap bitmap ){
  66. Final int reflectionGap = 4;
  67. Int width = bitmap. getWidth ();
  68. Int height = bitmap. getHeight ();
  69.  
  70. Matrixmatrix = new Matrix ();
  71. Matrix. preScale (1,-1 );
  72.  
  73. BitmapreflectionImage = Bitmap. createBitmap (bitmap,
  74. 0, height/2, width, height/2, matrix, false );
  75.  
  76. BitmapbitmapWithReflection = Bitmap. createBitmap (width, (height + height/2), Config. ARGB_8888 );
  77.  
  78. Canvascanvas = new Canvas (bitmapWithReflection );
  79. Canvas. drawBitmap (bitmap, 0, 0, null );
  80. Paint deafalutPaint = new Paint ();
  81. Canvas. drawRect (0, height, width, height + reflectionGap,
  82. DeafalutPaint );
  83.  
  84. Canvas. drawBitmap (reflectionImage, 0, height + reflectionGap, null );
  85.  
  86. Paint paint = new Paint ();
  87. LinearGradient shader = new LinearGradient (0,
  88. Bitmap. getHeight (), 0, bitmapWithReflection. getHeight ()
  89. + ReflectionGap, 0x70ffffff, 0x00ffffff, TileMode. CLAMP );
  90. Paint. setShader (shader );
  91. // Set the Transfer mode to be porter duff and destination in
  92. Paint. setXfermode (new porterduxfermode (Mode. DST_IN ));
  93. // Draw a rectangle using the paint with our linear gradient
  94. Canvas. drawRect (0, height, width, bitmapWithReflection. getHeight ()
  95. + ReflectionGap, paint );
  96.  
  97. ReturnbitmapWithReflection;
  98. }
  99.  
  100. }

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

 
 
  1. <?xmlversionxmlversion="1.0" encoding="utf-8"?>  
  2. <LinearLayoutxmlns:androidLinearLayoutxmlns: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 main core program, ImageDemo. java. The Code is as follows:

 
 
  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.  
  10. Public voidonCreate (Bundle savedInstanceState ){
  11. Super. onCreate (savedInstanceState );
  12. SetContentView (R. layout. main );
  13. SetupViews ();
  14. }
  15.  
  16. Private voidsetupViews (){
  17. MImageView01 = (ImageView) findViewById (R. id. image01 );
  18. MImageView02 = (ImageView) findViewById (R. id. image02 );
  19.  
  20. // Obtain the wallpaper. The returned value is Drawable.
  21. Drawable drawable = getWallpaper ();
  22. // Converts Drawable to Bitmap
  23. Bitmap bitmap = ImageUtil. drawableToBitmap (drawable );
  24. // Scale the image
  25. Bitmap zoomBitmap = ImageUtil. zoomBitmap (bitmap, 100,100 );
  26. // Obtain the rounded corner Image
  27. Bitmap roundBitmap = ImageUtil. getRoundedCornerBitmap (zoomBitmap, 10.0f );
  28. // Obtain the reflected image
  29. Bitmap reflectBitmap = ImageUtil. createReflectionImageWithOrigin (zoomBitmap );
  30. // Here Bitmap can be converted to Drawable
  31. // Drawable roundDrawable = new BitmapDrawable (roundBitmap );
  32. // Drawable reflectDrawable = new BitmapDrawable (reflectBitmap );
  33. // MImageView01.setBackgroundDrawable (roundDrawable );
  34. // MImageView02.setBackgroundDrawable (reflectDrawable );
  35.  
  36. MImageView01.setImageBitmap (roundBitmap );
  37. MImageView02.setImageBitmap (reflectBitmap );
  38. }
  39.  
  40.  
  41. }

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

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.