SoFunction
Updated on 2025-03-02

Android can easily implement image reflection effect example code

Main Activity

Copy the codeThe code is as follows:

package ;
import ;
import ;
import ;
import ;
import ;
import ;

public class ImageActivity extends Activity {

// Declare the control
private ImageView mImageView01, mImageView02;

@Override
public void onCreate(Bundle savedInstanceState) {
(savedInstanceState);
setContentView();
setupViews();

}
private void setupViews() {
mImageView01 = (ImageView) findViewById(.image01);
mImageView02 = (ImageView) findViewById(.image02);
// Get the wallpaper return value is Drawable
Drawable drawable = getWallpaper();
// Convert Drawable to Bitmap
Bitmap bitmap = (drawable);
// Zoom the picture
Bitmap zoomBitmap = (bitmap, 300, 300);
// Get rounded corner pictures
Bitmap roundBitmap = ImageUtil
.getRoundedCornerBitmap(zoomBitmap, 10.0f);
// Get reflection pictures
Bitmap reflectBitmap = ImageUtil
.createReflectionImageWithOrigin(roundBitmap);
// Here you can convert Bitmap into Drawable
// Drawable roundDrawable = new BitmapDrawable(roundBitmap);
// Drawable reflectDrawable = new BitmapDrawable(reflectBitmap);
// (roundDrawable);
// (reflectDrawable);
(roundBitmap);
(reflectBitmap);
}
}


Layout file
Copy the codeThe code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp" />

    <ImageView
        android:
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp" />

</LinearLayout>


Image processing
Copy the codeThe code is as follows:

package ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

public class ImageUtil {
/**
* Zoom in and out of picture
* @param bitmap
* @param w
* @param h
* @return
*/
public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
int width = ();
int height = ();
Matrix matrix = new Matrix();
float scaleWidht = ((float) w / width);
float scaleHeight = ((float) h / height);
(scaleWidht, scaleHeight);
Bitmap newbmp = (bitmap, 0, 0, width, height,
matrix, true);
return newbmp;
}

/**
* Convert Drawable to Bitmap
* @param drawable
* @return
*/
public static Bitmap drawableToBitmap(Drawable drawable) {
int width = ();
int height = ();
Bitmap bitmap = (width, height, drawable
.getOpacity() != ? .ARGB_8888
: .RGB_565);
Canvas canvas = new Canvas(bitmap);
(0, 0, width, height);
(canvas);
return bitmap;
}

/*
* Methods to get rounded corner pictures
*/
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
Bitmap output = ((),
(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, (), ());
final RectF rectF = new RectF(rect);
(true);
(0, 0, 0, 0);
(color);
(rectF, roundPx, roundPx, paint);
(new PorterDuffXfermode(Mode.SRC_IN));
(bitmap, rect, rect, paint);
return output;
}

/**
* How to get a picture with reflection
* @param originalImage
* @return
*/
public static Bitmap createReflectionImageWithOrigin(Bitmap originalImage) {
final int reflectionGap = 4;
int width = ();
int height = ();
Matrix matrix = new Matrix();
// Picture matrix transformation (reflection from the low part to the top) // Implement image flip 90 degrees
(1, -1);
// Create the reversed image Bitmap object, the image height is half the original image
Bitmap reflectionImage = (originalImage, 0, height / 2,
width, height / 2, matrix, false);
// Create a standard Bitmap object, the width is the same as the original image and the height is 1.5 times that of the original image
Bitmap bitmapWithReflection = (width,
(height + height / 2), Config.ARGB_8888);
// Create a canvas object and draw the original picture on the canvas. The starting point is the origin position
Canvas canvas = new Canvas(bitmapWithReflection);
(originalImage, 0, 0, null);
// Draw the reversed picture into the canvas
Paint deafalutPaint = new Paint();
(0, height, width, height + reflectionGap, deafalutPaint);
(reflectionImage, 0, height + reflectionGap, null);
Paint paint = new Paint();
// Create a linear gradient LinearGradient object
LinearGradient shader = new LinearGradient(0, (), 0,
() + reflectionGap, 0x70ffffff,
0x00ffffff, );
(shader);
// Set the Transfer mode to be porter duff and destination in
(new PorterDuffXfermode(Mode.DST_IN));
// Draw a rectangle using the paint with our linear gradient
// Draw the size area of ​​the inverted image and add the gradient effect to it, and the image reflection effect appears
(0, height, width, ()
+ reflectionGap, paint);
return bitmapWithReflection;
}
}