Image reflection effect in Android

Source: Internet
Author: User

1.:



2. Core code:

Package Com.example.pic_reflection;import Android.app.activity;import Android.graphics.bitmap;import Android.graphics.bitmap.config;import Android.graphics.canvas;import Android.graphics.lineargradient;import Android.graphics.matrix;import Android.graphics.paint;import Android.graphics.porterduff.mode;import Android.graphics.porterduffxfermode;import Android.graphics.shader.tilemode;import Android.graphics.drawable.bitmapdrawable;import Android.os.bundle;import Android.widget.imageview;public Class    Mainactivity extends Activity {private ImageView img = null;    Private ImageView orginalimg;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.main);        This.orginalimg = (ImageView) This.findviewbyid (r.id.img);        This.orginalImg.setBackgroundResource (r.drawable.b);        This.img = (ImageView) This.findviewbyid (R.id.reflection1); This.img.setImageBitmap (Createreflectedimage (((Bitmapdrawable) this.getresources (). getdrawable (r.drawable.b)). Getbitmap (), 110); } public static Bitmap createreflectedimage (Bitmap originalimage, int reflectionheight) {int width = Originalim        Age.getwidth ();        int height = originalimage.getheight ();        Matrix matrix = new Matrix ();        Achieve picture rollover 90 degrees Matrix.prescale (1,-1);        if (reflectionheight > Height) reflectionheight = height; Create a reflection picture (half the size of the original picture) Bitmap Reflectionimage = Bitmap.createbitmap (originalimage, 0, Height-reflectionheight, wid        Th, Reflectionheight, Matrix, false);        Create a reflection image Bitmap Finalreflection = Bitmap.createbitmap (width, reflectionheight, config.argb_8888);        Create canvas canvases canvas = new canvas (finalreflection);        Canvas.drawbitmap (originalimage, 0, 0, NULL);        Draw the reflection picture onto the canvas canvas.drawbitmap (reflectionimage, 0, 0, NULL);        Paint Shaderpaint = new paint (); Create a linear gradient LinearGradient object lineArgradient shader = new LinearGradient (0, 0, 0, finalreflection.getheight () + 1, 0x70ffffff, 0X00FFFFFF, Tilemode.mirror);        Shaderpaint.setshader (shader);        Shaderpaint.setxfermode (New Porterduffxfermode (mode.dst_in));        The image is reflected when the canvas is reversed, and the gradient is added to the image's size area.        Canvas.drawrect (0, 0, Width, finalreflection.getheight (), shaderpaint);    return finalreflection; }}
3, Main.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" wrap_content "    android:layout_height=" wrap_content "    android:o rientation= "vertical" >    <include layout= "@layout/activity_main"/>    <imageview        android:id= "@+id/reflection1"        android:layout_width= "wrap_content"        android:layout_height= "Wrap_content"/></ Linearlayout>

4, Activity_main.xml

<?xml version= "1.0" encoding= "Utf-8"? ><framelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" wrap_content "    android:layout_height=" wrap_content ">    < ImageView        android:id= "@+id/img"        android:layout_width= "match_parent" android:layout_height= "Match_        Parent "        android:scaletype=" Fitxy "/>    <textview        android:id=" @+id/lblbanner "        android:layout _width= "Match_parent"        android:layout_height= "wrap_content"        android:layout_gravity= "Bottom        " Android:background= "#88000000"        android:ellipsize= "End"        android:gravity= "Center_horizontal|center_ Vertical "        android:singleline=" true "        android:text=" car One "        android:textcolor=" #FFF "/></ Framelayout>



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.