MainActivity is as follows:
package c.x;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity { private Button mStartbButton; private Button mPauseButton; private Button mRestartbButton; private MarqueeText mMarqueeText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ mMarqueeText=(MarqueeText) findViewById(R.id.marqueeText); mStartbButton=(Button) findViewById(R.id.start_Button); mStartbButton.setOnClickListener(new OnClickListener() {public void onClick(View v) { mMarqueeText.startScroll();} }); mPauseButton=(Button) findViewById(R.id.pause_Button); mPauseButton.setOnClickListener(new OnClickListener() {public void onClick(View v) {mMarqueeText.pauseScroll();} }); mRestartbButton=(Button) findViewById(R.id.restart_Button); mRestartbButton.setOnClickListener(new OnClickListener() {public void onClick(View v) {mMarqueeText.restartScroll();} }); } }
MarqueeText is as follows:
Package C. x; import android. content. context; import android. graphics. canvas; import android. graphics. paint; import android. util. attributeset; import android. widget. textview;/*** Requirement Description: * Implementation of textview marquee ** implementation principle: * continuous scrollto () display the content of different coordinates in the view ** problems encountered ** 1. Note that scrollto (int x, int y) * moves the content in the view, such as the text in textview. * x the X position to scroll to * y the Y position to scroll to ** 2 scrollto (int x, int y) coordinates Problem * Reference: * http://www.open-open.com/lib/view/open1328834050046.html * personal understanding: * this coordinate is the coordinate of the content in the view. although the starting point of the coordinate is in the upper left corner of the content *, the positive direction of the Y axis is vertical and upward, and the X axis is horizontal to the right. * This understanding is awaiting further verification. **/public class marqueetext extends textview implements runnable {private int contentwidth = 0; private int scrolltox = 0; private Boolean isstop = false; private Boolean isrun = true; private Boolean ismeasurecontentwidth = false; Public marqueetext (context cont EXT, attributeset attrs) {super (context, attrs) ;}@ overrideprotected void ondraw (canvas) {super. ondraw (canvas); If (! Ismeasurecontentwidth) {// get the text length: paint = This. getpaint (); string content = This. gettext (). tostring (); contentwidth = (INT) paint. measuretext (content); ismeasurecontentwidth = true ;}} public void run () {If (isrun) {If (scrolltox> = contentwidth) {// restart scrolltox =-150 ;} scrollto (scrolltox, 0); scrolltox = scrolltox + 5; postdelayed (this, 150) ;}// Click start to run the public void startscroll () thread () {post (this) ;}// click to pause public void pausescroll () {isrun = false;} // click to restart public void restartscroll () {isrun = true; scrolltox = 0; startscroll ();}}
Main. xml is as follows:
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent"> <LinearLayout android: id = "@ + id/linerLayout" android: layout_width = "match_parent" android: layout_height = "80dip" android: layout_alignParentTop = "true" android: orientation = "horizontal"> <RelativeLayout android: layout_width = "fill_parent" android: layout_height = "fill_parent"> <Button android: id = "@ + id/start_Button" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignParentLeft = "true" android: text = ""/> <Button android: id = "@ + id/pause_Button" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_centerHorizontal = "true" android: text = "pause"/> <Button android: id = "@ + id/restart_Button" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignParentRight = "true" android: text = "restart"/> </RelativeLayout> </LinearLayout> <c. x. marqueeText android: id = "@ + id/marqueeText" android: layout_width = "200dip" android: layout_height = "100dip" android: layout_below = "@ id/linerLayout" android: text = "bosh 29 points, 4 rebounds, 2 steals, 4 blocks, 4 blocks, Ray-Allen 15 points" android: textColor = "@ android: color/black" android: background = "@ android: color/white "android: ellipsize =" marquee "android: singleLine =" true "android: gravity =" center_vertical "/> </RelativeLayout>