Android display textview text reflection effect implementation code

Today, record the reflection effect of textview, display a string of text, and then display its reflection below the text. First, the effect picture:

The most important is the getdrawingcache () method in view, which can obtain the images in the cache and draw them.

No more nonsense. I want to write a time with reflection. Time can move around. First, write a view with time walk. This is very simple. Get the current time, then start a thread, get the current time every second, and then display it on textview. Of course, when we write a control, we need to inherit textview. The code is as follows:

Now you just need to call the control in the layout file, and you can achieve a walking time.

The second step is to add a reflection to the walking time. We need to write a control to inherit the above time walking control, and then we can implement the time walking view with reflection. Here is the code with reflection:

The main function is in the OnDraw method. First call setdrawingcacheenabled (true); Make the cache available, and then create an image like the original image through the cache through mmatrix. Prescale (1, - 1); Turn the picture upside down and call bitmap. Createbitmap (originalimage, false); Create an inverted image and call canvas.drawbitmap (reflectionimage, null); Draw the inverted image on the canvas. Call LinearGradient shader = new LinearGradient (0, height, tilemode. Clamp); mPaint.setShader(shader); mPaint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); Change the color of the reflected image from gray to black.

Call builddrawingcache() when time moves around; postInvalidate();

Let the reflection draw again.

Call setmeasureddimension (getmeasuredwidth(), (int) (getmeasuredheight() * 1.67)); Sets the width and height of the image.

OK, the control has been finished. Now, as long as you call this control in the layout, you can display a View with reflection time in Activity. First, write a layout file:

Then display the layout in the activity. I reset the font of the control to make it display square.

Run the code, and a time view with reflection will be displayed on the mobile phone. Time will move around. Isn't it fun.

All right, that's it.

Source code download address: http://xiazai.jb51.net/201402/yuanma/ReflectTextView (jb51.net).rar

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>