Android Display TextView Text reflection effect implementation code _android

Source: Internet
Author: User
Tags current time dateformat reflection

Today, record the TextView reflection effect, display a string of text, and then in the text below display its reflection, first on the effect of the picture:

The most important thing is the View Getdrawingcache () method, which gets the image in the cache and draws it.

Needless to say, I want to write a time with reflection, time can walk. First write a view with time to move around, this is very simple, get the current time, then open a thread, every second to get the current time, and then display on the TextView, of course, we write the control, we need to inherit TextView, the code is as follows:

Copy Code code as follows:

Package Com.alex.reflecttextview;

Import Java.util.Calendar;

Import Android.content.Context;
Import Android.os.Handler;
Import Android.os.Message;
Import Android.text.format.DateFormat;
Import Android.util.AttributeSet;
Import Android.widget.TextView;

public class Timeview extends TextView {

private static final int message_time = 1;

Public Timeview (context context, AttributeSet Attrs) {
Super (context, attrs);
New Timethread (). Start ();
}

public class Timethread extends Thread {
@Override
public void Run () {
do {
try {
msg = new Message ();
Msg.what = Message_time;
Mhandler.sendmessage (msg);
Thread.Sleep (1000);
catch (Interruptedexception e) {
E.printstacktrace ();
}
} while (true);
}
}

Private Handler Mhandler = new Handler () {

@Override
public void Handlemessage (msg) {
Super.handlemessage (msg);
Switch (msg.what) {
Case Message_time:
SetTime ();
Break

Default
Break
}
}
};

public void SetTime () {
Long systime = System.currenttimemillis ();
Calendar calendar = Calendar.getinstance ();
Calendar.settimeinmillis (Systime);
String systimestr = Dateformat.format ("hh:mm", Systime). toString ();
if (Calendar.get (calendar.am_pm) = = 0) {
Systimestr + = "AM";
} else {
Systimestr + + "PM";
}
SetText (Systimestr.replace ("1", "1"));
}
}

Now you just need to call the control in the layout file to make it a walking time.

The second step is to give this moving time to add a reflection, we need to write a control to inherit the above a time to move around the control, you can realize the time with the reflection of the view, the following is a reflection of the code:

Copy Code code as follows:

Package Com.alex.reflecttextview;


Import Android.content.Context;
Import Android.graphics.Bitmap;
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.util.AttributeSet;

public class Reflecttextview extends Timeview {

Private Matrix Mmatrix;
Private Paint Mpaint;

Public Reflecttextview (context context, AttributeSet Attrs) {
Super (context, attrs);
Init ();
}

private void init () {
Mmatrix = new Matrix ();
Mmatrix.prescale (1,-1);
}

@Override
protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
Super.onmeasure (Widthmeasurespec, Heightmeasurespec);
Setmeasureddimension (Getmeasuredwidth (), (int) (Getmeasuredheight () *1.67));
}

@Override
protected void OnDraw (Canvas Canvas) {
Super.ondraw (canvas);
int height = getheight ();
int width = getwidth ();
Setdrawingcacheenabled (TRUE);
Bitmap originalimage = Bitmap.createbitmap (Getdrawingcache ());
Bitmap reflectionimage = Bitmap.createbitmap (originalimage, 0, HEIGHT/5, width, HEIGHT/2, Mmatrix, false);
Canvas.drawbitmap (reflectionimage, 0, height/3f, NULL);
if (Mpaint = = null) {
Mpaint = new Paint ();
LinearGradient shader = new LinearGradient (0, HEIGHT/2, 0,
Height, 0x7fffffff, 0x0fffffff, Tilemode.clamp);
Mpaint.setshader (shader);
Mpaint.setxfermode (New Porterduffxfermode (mode.dst_in));
}
Canvas.drawrect (0, height/2f, width, height, mpaint);
}

@Override
protected void ontextchanged (charsequence text, int start,
int lengthbefore, int lengthafter) {
Super.ontextchanged (text, start, Lengthbefore, Lengthafter);
Builddrawingcache ();
Postinvalidate ();
}
}

The main function in the OnDraw method, the first call setdrawingcacheenabled (true), let the cache available, and then through the cache to create a picture and the original image, through Mmatrix.prescale (1,-1); Turn the picture upside down, call Bitmap.createbitmap (originalimage, 0, HEIGHT/5, width, HEIGHT/2, Mmatrix, false); Create an inverted image, Call Canvas.drawbitmap (reflectionimage, 0, height/3f, null); Draw the inverted image onto the canvas. by calling LinearGradient shader = new LinearGradient (0, HEIGHT/2, 0,
Height, 0x7fffffff, 0x0fffffff, Tilemode.clamp);
Mpaint.setshader (shader);
Mpaint.setxfermode (New Porterduffxfermode (mode.dst_in)) to make the image of the reflection of the color gradient, from gray to black.

Call Builddrawingcache () when the time is moving;
Postinvalidate ();

Let the reflection be drawn anew.

Call Setmeasureddimension (Getmeasuredwidth (), (int) (Getmeasuredheight () *1.67)), and set the width and height of the image.

OK, now that the control is finished, just call the control in the layout and you can display a view of the time in the activity with a reflection, and write a layout file first:

Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
Android:background= "#000000"
android:paddingtop= "@dimen/activity_vertical_margin" >

<com.alex.reflecttextview.reflecttextview
Android:id= "@+id/timeview"
Android:textsize= "@dimen/reflect_size"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:layout_alignparentbottom= "true"
android:gravity= "Top|center_horizontal"/>
</RelativeLayout>

And then show this layout in the activity, and I set the font of the control to a new one, and let it show the square.

Copy Code code as follows:

Package Com.alex.reflecttextview;

Import android.app.Activity;
Import Android.graphics.Typeface;
Import Android.os.Bundle;
Import Android.view.Window;
Import Android.view.WindowManager;

public class Mainactivity extends activity {

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Final Window win = GetWindow ();
Win.addflags (WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
Win.addflags (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
Setcontentview (R.layout.activity_main);
Timeview TV = (Timeview) Findviewbyid (R.id.timeview);
Tv.settypeface (Typeface.createfromasset (getassets), "fonts/ds-digii." TTF "));
}
}

Run the code, the phone back to show a reflection of the time view, time will also walk, is not very fun.

All right, here we go.

SOURCE Download Address: Http://xiazai.jb51.net/201402/yuanma/ReflectTextView (jb51.net). rar

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.