Android Set Image Reflection effect

Source: Internet
Author: User

First, Post:

1. layout file Main.xml

<?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:o rientation= "vertical" >    <textview        android:layout_width= "fill_parent"        android:layout_height= " Wrap_content "        android:text=" @string/hello "/>    <imageview        android:id=" @+id/picture_qian "        Android:layout_width= "Fill_parent"        android:layout_height= "fill_parent"        android:src= "@drawable/horse" /></linearlayout>

2.MainActivity Interface Java code:

1  Packagecom.easymorse.picture;2 3 Importandroid.app.Activity;4 ImportAndroid.graphics.Bitmap;5 ImportAndroid.graphics.Bitmap.Config;6 ImportAndroid.graphics.Canvas;7 Importandroid.graphics.LinearGradient;8 ImportAndroid.graphics.Matrix;9 ImportAndroid.graphics.Paint;Ten ImportAndroid.graphics.PorterDuff.Mode; One ImportAndroid.graphics.PorterDuffXfermode; A ImportAndroid.graphics.Shader.TileMode; - Importandroid.graphics.drawable.BitmapDrawable; - ImportAndroid.os.Bundle; the ImportAndroid.widget.ImageView; -  -  Public classMainactivityextendsActivity { -     /**Called when the activity is first created.*/ + @Override -      Public voidonCreate (Bundle savedinstancestate) { +         Super. OnCreate (savedinstancestate); A Setcontentview (r.layout.main); atImageView imageView2 =(ImageView) Findviewbyid (R.id.picture_qian); -  -Bitmap BMP =( (bitmapdrawable) getresources (). Getdrawable ( - r.drawable.horse)). Getbitmap (); -  - Imageview2.setimagebitmap (Createreflectedimage (BMP)); in     } -  to      Public StaticBitmap createreflectedimage (Bitmap originalimage) { +  -         Final intReflectiongap = 4; the  *         intwidth =originalimage.getwidth (); $         intHeight =originalimage.getheight ();Panax Notoginseng  -Matrix Matrix =NewMatrix (); theMatrix.prescale (1,-1); +  ABitmap reflectionimage = Bitmap.createbitmap (originalimage, 0, theHEIGHT/2, Width, height/2, matrix,false); +  -Bitmap bitmapwithreflection =bitmap.createbitmap (width, $(Height + HEIGHT/2), config.argb_8888); $  -Canvas Canvas =NewCanvas (bitmapwithreflection); -  theCanvas.drawbitmap (originalimage, 0, 0,NULL); - WuyiPaint Defaultpaint =NewPaint (); theCanvas.drawrect (0, height, width, height +Reflectiongap, defaultpaint); -  WuCanvas.drawbitmap (reflectionimage, 0, height + reflectiongap,NULL); -  AboutPaint paint =NewPaint (); $LinearGradient shader =NewLinearGradient (0, -Originalimage.getheight (), 0, Bitmapwithreflection.getheight () -+ Reflectiongap, 0X70FFFFFF, 0X00FFFFFF, - tilemode.mirror); A  + Paint.setshader (shader); the  -Paint.setxfermode (NewPorterduffxfermode (mode.dst_in)); $  theCanvas.drawrect (0, height, width, bitmapwithreflection.getheight () the+reflectiongap, paint); the  the         returnbitmapwithreflection; -     } in}

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.