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:
- 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 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 ){
-
- Bitmapoutput = Bitmap. createBitmap (bitmap. getWidth (), bitmap
- . GetHeight (), Config. ARGB_8888 );
- Canvas canvas = new Canvas (output );
-
- Final intcolor = 0xff0000242;
- Final Paint paint = new Paint ();
- Final Rect rect = new Rect (0, 0, bitmap. getWidth (), bitmap. getHeight ());
- Final RectF rectF = new RectF (rect );
-
- Paint. setAntiAlias (true );
- Canvas. drawARGB (0, 0, 0, 0 );
- Paint. setColor (color );
- Canvas. drawRoundRect (rectF, roundPx, roundPx, paint );
-
- Paint. setXfermode (newporterduxfermode (Mode. SRC_IN ));
- Canvas. drawBitmap (bitmap, rect, rect, paint );
-
- Returnoutput;
- }
- // Method for obtaining images with reflections
- Public static Bitmap createReflectionImageWithOrigin (Bitmap bitmap ){
- Final int reflectionGap = 4;
- Int width = bitmap. getWidth ();
- Int height = bitmap. getHeight ();
-
- Matrixmatrix = new Matrix ();
- Matrix. preScale (1,-1 );
-
- BitmapreflectionImage = Bitmap. createBitmap (bitmap,
- 0, height/2, width, height/2, matrix, false );
-
- BitmapbitmapWithReflection = Bitmap. createBitmap (width, (height + height/2), Config. ARGB_8888 );
-
- Canvascanvas = 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 );
-
- ReturnbitmapWithReflection;
- }
-
- }
Step 3: Modify the main. xml layout file and put two ImageView controls. The Code is as follows:
- <?xmlversionxmlversion="1.0" encoding="utf-8"?>
- <LinearLayoutxmlns:androidLinearLayoutxmlns: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 main core program, ImageDemo. java. The Code is as follows:
- 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 voidonCreate (Bundle savedInstanceState ){
- Super. onCreate (savedInstanceState );
- SetContentView (R. layout. main );
- SetupViews ();
- }
-
- Private voidsetupViews (){
- MImageView01 = (ImageView) findViewById (R. id. image01 );
- MImageView02 = (ImageView) findViewById (R. id. image02 );
-
- // Obtain the wallpaper. The returned value is Drawable.
- 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: