Android easy to implement image reflection effect instance code _android

Source: Internet
Author: User
Tags reflection

Main activity

Copy Code code as follows:

Package com.mj.myweather;
Import android.app.Activity;
Import Android.graphics.Bitmap;
Import android.graphics.drawable.Drawable;
Import Android.os.Bundle;
Import Android.widget.ImageView;
Import Com.mj.myweather.utils.ImageUtil;

public class Imageactivity extends activity {

declaring controls
Private ImageView mImageView01, mImageView02;

@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.image);
Setupviews ();

}
private void Setupviews () {
mImageView01 = (ImageView) Findviewbyid (R.ID.IMAGE01);
mImageView02 = (ImageView) Findviewbyid (R.ID.IMAGE02);
Get wallpaper return value is drawable
drawable drawable = Getwallpaper ();
Convert drawable to Bitmap
Bitmap Bitmap = Imageutil.drawabletobitmap (drawable);
Zoom picture
Bitmap Zoombitmap = Imageutil.zoombitmap (Bitmap, 300, 300);
Get fillet picture
Bitmap Roundbitmap = Imageutil
. Getroundedcornerbitmap (Zoombitmap, 10.0f);
Get the Reflection picture
Bitmap Reflectbitmap = Imageutil
. Createreflectionimagewithorigin (Roundbitmap);
This will allow bitmap to be converted into drawable.
drawable rounddrawable = new bitmapdrawable (ROUNDBITMAP);
drawable reflectdrawable = new bitmapdrawable (REFLECTBITMAP);
Mimageview01.setbackgrounddrawable (rounddrawable);
Mimageview02.setbackgrounddrawable (reflectdrawable);
Mimageview01.setimagebitmap (ROUNDBITMAP);
Mimageview02.setimagebitmap (REFLECTBITMAP);
}
}


Layout file
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:orientation= "Vertical" >

<imageview
Android:id= "@+id/image01"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:padding= "10DP"/>

<imageview
Android:id= "@+id/image02"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:padding= "10DP"/>

</LinearLayout>


Picture processing
Copy Code code as follows:

Package com.mj.myweather.utils;

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.PorterDuffXfermode;
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 out picture
* @param bitmap
* @param w
* @param h
* @return
*/
Publi C 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
* @param drawable
* @return
*/
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;
}

/*
* Method of obtaining fillet picture
*/
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 = 0xff424242;
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 (New Porterduffxfermode (mode.src_in));
Canvas.drawbitmap (Bitmap, rect, rect, paint);
return output;
}

/**
* Get a picture method with reflection
* @param originalimage
* @return
*/
public static Bitmap Createreflectionimagewithorigin (Bitmap originalimage) {
Final int reflectiongap = 4;
int width = originalimage.getwidth ();
int height = originalimage.getheight ();
Matrix matrix = new Matrix ();
Image matrix transformation (from the lower part to the top of the reflection)//To achieve a picture flip 90 degrees
Matrix.prescale (1,-1);
Create inverted picture Bitmap object, picture height is half of the original artwork
Bitmap reflectionimage = Bitmap.createbitmap (originalimage, 0, HEIGHT/2,
width, HEIGHT/2, matrix, false);
Create a standard bitmap object with a uniform width and a height of 1.5 times times the original
Bitmap bitmapwithreflection = Bitmap.createbitmap (width,
(height + height/2), config.argb_8888);
Creates a canvas object, drawing the original picture to the canvas, starting at the origin position
Canvas Canvas = new Canvas (bitmapwithreflection);
Canvas.drawbitmap (originalimage, 0, 0, NULL);
Draw the inverted picture into the canvas
Paint deafalutpaint = new Paint ();
Canvas.drawrect (0, height, width, height + reflectiongap, deafalutpaint);
Canvas.drawbitmap (reflectionimage, 0, height + reflectiongap, null);
Paint Paint = new Paint ();
Create a linear gradient lineargradient object
LinearGradient shader = new LinearGradient (0, Originalimage.getheight (), 0,
Bitmapwithreflection.getheight () + Reflectiongap, 0X70FFFFFF,
0X00FFFFFF, Tilemode.clamp);
Paint.setshader (shader);
Set the Transfer mode to is Porter Duff and destination in
Paint.setxfermode (New Porterduffxfermode (mode.dst_in));
Draw a rectangle using the paint with our linear gradient
The canvas draws the inverted picture size area, then adds the gradient effect to it, then appears the picture the reflection effect
Canvas.drawrect (0, height, width, bitmapwithreflection.getheight ()
+ reflectiongap, paint);
return bitmapwithreflection;
}
}

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.