Implementation Code of the Android reflection Algorithm

Source: Internet
Author: User

Copy codeThe Code is as follows: public class MirrorView extends View {
Paint m_paint;
Int m_nShadowH;
Drawable m_dw;
Bitmap m_bitmap;
// Xxxx
Matrix mMatrix;
Int shadowHeight;
Public MirrorView (Context context, Bitmap bitmap ){
Super (context );
M_bitmap = bitmap;
_ Init ();
// TODO Auto-generated constructor stub
}
Private void _ Init (){
// M_dw = new BitmapDrawable (BitmapFactory. decodeResource (getResources (), R. drawable. icon ));
M_dw = new BitmapDrawable (m_bitmap );
M_dw.setBounds (0, 0, m_dw.getIntrinsicWidth (), m_dw.getIntrinsicHeight ());
M_nShadowH = m_dw.getIntrinsicHeight ()/1;
M_paint = new Paint (Paint. ANTI_ALIAS_FLAG );
LinearGradient lg = new LinearGradient (0, 0, 0, m_nShadowH, 0xB0FFFFFF, 0x00000000, Shader. TileMode. CLAMP );
M_paint.setShader (lg );
M_paint.setXfermode (new porterduxfermode (PorterDuff. Mode. MULTIPLY ));
// Xxxx
MMatrix = new Matrix ();
}
@ Override
Public void onDraw (Canvas canvas ){
Super. onDraw (canvas );
Int nX = 0;
Int nY = 0;
_ DrawNormalImg (canvas, nX, nY );
_ DrawMirror (canvas, nX, nY );
}
Private void _ DrawNormalImg (Canvas canvas, int nX, int nY ){
Canvas. save (Canvas. MATRIX_SAVE_FLAG );
Canvas. translate (nX, nY );
M_dw.draw (canvas );
Canvas. restore ();
}
Private void _ DrawMirror (Canvas canvas, int nX, int nY ){
Int nW = m_dw.getIntrinsicWidth ();
Int nH = m_dw.getIntrinsicHeight ();
// U. dout ("nW =" + nW + ", nH =" + nH );
// Xxxx
ShadowHeight = nH/2;
// Xxxx
Float [] src = {0, nH, nW, nH, nW, nH-m_nShadowH, 0, nH-m_nShadowH };
Float [] dst = {0, nH, nW, nH, nW, shadowHeight, 0, shadowHeight };
// Xxxx
Canvas. save ();
MMatrix. setPolyToPoly (src, 0, dst, 0, src. length> 1 );
Canvas. concat (mMatrix );
// Draw mirror image
Canvas. save (Canvas. MATRIX_SAVE_FLAG );
Canvas. scale (1.0f,-1.0f );
Canvas. translate (nX,-(nY + nH * 2 ));
Canvas. clipRect (0, nH, nW, nH-m_nShadowH );
M_dw.draw (canvas );
Canvas. restore ();
// Draw mask
Canvas. save ();
Canvas. translate (nX, nY + nH );
Canvas. drawRect (0, 0, nW, m_nShadowH, m_paint );
Canvas. restore ();
// Xxxx
Canvas. restore ();
}
}
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.