Android wraps the view to achieve a specular effect

Source: Internet
Author: User

This on TV with a lot of, online many are to ImageView pictures do, feel not very good, here to achieve a can to any view do reflection.

/** * * @className: MirrorView * @description: The normal view is encapsulated, so that the view has a reflection effect, note that after using this view, * will let the original view of the height     Expands refheight height to show reflective mirrors, * @author: Gaoshuai * @date: August 11, 2015 morning 11:31:16 */public class MirrorView extends framelayout{    Private View Mcontentview;    Protected Boolean mhasreflection = true;    private static int refheight =-1;        public static Paint refpaint = null;    Private Bitmap Mreflectbitmap;        Private Canvas Mreflectcanvas;                Public MirrorView (Context context) {super (context);        if (refheight = =-1) refheight = 100;            if (Refpaint = = null) {Refpaint = new Paint (Paint.anti_alias_flag);  Refpaint.setshader (New lineargradient (0, 0, 0, refheight, new int[] {0x77000000, 0x66aaaaaa, 0x0500000, 0x00000000}, new            Float[] {0.0f, 0.1f, 0.9f, 1.0f}, Shader.TileMode.CLAMP));        Refpaint.setxfermode (New Porterduffxfermode (PorterDuff.Mode.DST_IN)); } This.setclickAble (true); } public void Setcontentview (view view) {Framelayout.layoutparams LP = new Framelayout.layoutparams (LAYOUTP Arams.        Wrap_content, layoutparams.wrap_content);        Lp.bottommargin = Refheight;        Mcontentview = view;    AddView (view, LP);    Public View Getcontentview () {return mcontentview;    public void Setreflection (Boolean ref) {mhasreflection = ref;    } @Override public Boolean PerformClick () {return Mcontentview.performclick ();        } @Override protected void Dispatchdraw (canvas canvas) {super.dispatchdraw (canvas); if (mhasreflection) {if (Mreflectbitmap = = null) {Mreflectbitmap = Bitmap.createbitmap (mconten                Tview.getwidth (), Refheight, Bitmap.Config.ARGB_8888);            Mreflectcanvas = new Canvas (MREFLECTBITMAP); } drawreflection (Mreflectcanvas, Mcontentview);            Draw the reflection effect of this view canvas.save ();int dy = Mcontentview.getbottom ();            int dx = Mcontentview.getleft ();            Canvas.translate (dx, dy);            Canvas.drawbitmap (mreflectbitmap, 0, 0, NULL);        Canvas.restore ();    }} public Bitmap Getreflectbitmap () {return mreflectbitmap;        } public void Drawreflection (canvas canvas, view view) {Canvas.save ();        Canvas.cliprect (0, 0, view.getwidth (), refheight);        Canvas.save ();        Canvas.scale (1,-1);        Canvas.translate (0,-view.getheight ());        View.draw (canvas);        Canvas.restore ();        Canvas.drawrect (0, 0, view.getwidth (), Refheight, refpaint);    Canvas.restore (); }}


Test activity


public class Mainactivity extends activity{        @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_main);        Framelayout container = (framelayout) Findviewbyid (r.id.test);        MirrorView Mirroritemview = new MirrorView (this);        TextView TextView = new TextView (this);        Textview.setwidth (+);        Textview.settext ("I just want to test the Mirror View");        Textview.setbackgroundresource (r.drawable.ic_launcher);        Mirroritemview.setcontentview (TextView);        Container.addview (Mirroritemview);    }}



Android wraps the view to achieve a specular effect

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.