Android based on TextView implementation of the results of the case _android

Source: Internet
Author: User
Tags int size static class

This article is an example of an Android-based TextView implementation of the marquee effect. Share to everyone for your reference, specific as follows:

Package sweet.venst.act;
Import Java.io.BufferedReader;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import java.io.UnsupportedEncodingException;
Import Android.content.Context;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Paint;
Import Android.os.Parcel;
Import android.os.Parcelable;
Import Android.util.AttributeSet;
Import Android.util.DisplayMetrics;
Import Android.view.Display;
Import Android.view.View;
Import Android.view.WindowManager;
Import Android.view.View.MeasureSpec;
Import Android.view.View.OnClickListener;
Import Android.widget.TextView; public class Autoscrolltextview extends TextView {public final static String TAG = AutoScrollTextView.class.getSimpleNam
 E ();
 private float textlength = 0f;//text length private int viewwidth = 0;
 private int viewhight = 300; Private float step = horizontal axis pri of 0f;//textvate float y = 0f;//of text ordinate private float temp_view_plus_text_length = 0.0f;//temporary variable for calculation private float TEMP_VIEW_PLUS_TW O_text_length = 0.0f;//Temporary variable public boolean isstarting for calculation = false;//start scrolling private Paint Paint = null;//drawing style privat
 E String Text = "";//text content Public Autoscrolltextview (context) {super);
 Public Autoscrolltextview (context, AttributeSet attrs) {Super (context, attrs);
 Public Autoscrolltextview (context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); 
 @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {//TODO auto-generated method stub
 Super.onmeasure (Widthmeasurespec, Heightmeasurespec);
 int measurewidth = Measurewidth (Widthmeasurespec);
 int measurehight = Measureheight (Heightmeasurespec);
 int measurewidth = 1024;
 int measurehight = 300;
 Viewwidth = Measurewidth;
 Viewhight = Measurehight;
 Setmeasureddimension (Viewwidth, viewhight); } privateint measurewidth (int widthmeasurespec) {int specmode = Measurespec.getmode (Widthmeasurespec);
 int specsize = measurespec.getsize (Widthmeasurespec);
 if (Specmode = = measurespec.unspecified) {return viewwidth;
  else {return viewwidth;
 } private int Measureheight (int widthmeasurespec) {int specmode = Measurespec.getmode (Widthmeasurespec);
 int specsize = measurespec.getsize (Widthmeasurespec);
 if (Specmode = = measurespec.unspecified) {return viewhight;
 else {return viewhight;
 } public void Setscrolltext (String text) {this.text = text;
 Inittextlength (text);
 Invalidate ();
 public void init (context cx) {paint = Getpaint ();
 Paint.settextsize (40);
 Paint.setcolor (Color.White);
 Text = GetText (). toString ();
 TextLength = Paint.measuretext (text);
 Viewwidth = GetWidth ();
  if (Viewwidth = = 0) {displaymetrics dm = new Displaymetrics ();
  DM = Cx.getapplicationcontext (). Getresources (). Getdisplaymetrics ();
 Viewwidth = Dm.widthpixels; } step= TextLength;
 Temp_view_plus_text_length = Viewwidth + textlength;
 Temp_view_plus_two_text_length = Viewwidth + textlength * 2;
 y = gettextsize () + getpaddingtop ();
 private void Inittextlength (String text) {textlength = Paint.measuretext (text);
 Step = TextLength;
 Temp_view_plus_text_length = Viewwidth + textlength;
 Temp_view_plus_two_text_length = Viewwidth + textlength * 2;
 @Override public parcelable onsaveinstancestate () {parcelable superstate = super.onsaveinstancestate ();
 savedstate ss = new savedstate (superstate);
 Ss.step = step;
 ss.isstarting = isstarting;
 return SS; @Override public void Onrestoreinstancestate (parcelable state) {if (!
  State instanceof Savedstate)) {super.onrestoreinstancestate (state);
 Return
 } savedstate SS = (savedstate) state;
 Super.onrestoreinstancestate (Ss.getsuperstate ());
 Step = Ss.step;
 isstarting = ss.isstarting;
 public static class Savedstate extends Basesavedstate {public Boolean isstarting = false; PublIC Float step = 0.0f;
 Savedstate (parcelable superstate) {super (superstate);
  @Override public void Writetoparcel (Parcel out, int flags) {Super.writetoparcel (out, flags);
  Out.writebooleanarray (new boolean[] {isstarting});
 Out.writefloat (step); public static final parcelable.creator<savedstate> Creator = new parcelable.creator<savedstate> () {Publi
  C savedstate[] NewArray (int size) {return new savedstate[size];
  Public savedstate Createfromparcel (Parcel in) {return new savedstate (in);
 }
 };
  Private savedstate (Parcel in) {super (in);
  Boolean[] B = null;
  In.readbooleanarray (b);
  if (b!= null && b.length > 0) isstarting = b[0];
 Step = In.readfloat ();
 The public void Startscroll () {isstarting = true;
 Invalidate ();
 public void Stopscroll () {isstarting = false;
 Invalidate ();
 public void OnDraw (Canvas Canvas) {canvas.save ();
 Canvas.drawtext (text, Temp_view_plus_text_length-step, y, paint); if (!isstarting) {return;
 Step + = 3.2;//0.5 is the text scrolling speed.
 if (Step > temp_view_plus_two_text_length) step = textlength;
Canvas.restore ();
 Invalidate ();
 Postinvalidate ();

 }
}

For more information on Android-related content readers can view the site topics: "Android Development Introduction and Advanced Course", "Android Multimedia operating skills Summary (audio, video, recording, etc.)", "Android Basic Components Usage Summary", " Android View tips Summary, Android layout layout tips and a summary of Android controls usage

I hope this article will help you with the Android program.

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.