Android-the strongest and Android-the biggest

Source: Internet
Author: User

Android-the strongest and Android-the biggest

There are already many Android marquee versions, from the most basic TextView, to the rewriting of TextView to cancel the focus limit of TextView, and the rewriting of TextView to write using the ScrollTo method, can basically meet the general needs. However, some exceptions are found during use-sometimes the stream is not played, and the refresh thread continues, but the stream is not played, at last, I found a project on github that uses animations to implement the marquee (Project address: https://github.com/ened/android-marqueeview, and I would like to thank the author again). After some transformation, I finally got it okay. In the future, you will no longer have to worry about the drive lights.


Features:

1. The text length has the effect of a lawn lamp.

2. Controllable speed



Code:

Package com. example. test_marquee; import android. content. context; import android. graphics. paint; import android. text. editable; import android. text. textWatcher; import android. util. attributeSet; import android. util. log; import android. view. gravity; import android. view. view; import android. view. viewGroup; import android. view. animation. animation; import android. view. animation. interpolator; import android. view. ani Mation. linearInterpolator; import android. view. animation. translateAnimation; import android. widget. linearLayout; import android. widget. scrollView; import android. widget. textView;/*** as the parent View, LinearLayout must have a child TextView ** implemented by animation */public class MarqueeView extends LinearLayout {private static final int TEXTVIEW_VIRTUAL_WIDTH = 2000; /* default TextView width */private static final int DEFAULT_SPEED = 35;/* default scroll speed A greater degree of scrolling slows down */private static final int DEFAULT_ANIMATION_PAUSE = 0;/* time interval between going out of the animation and entering the animation */private static final String TAG = MarqueeView. class. getSimpleName (); private TextView mTextField;/* TextView of the horse lamp's grandson */private ScrollView mScrollView;/* mScrollView of the child View of the horse lamp */private Animation mMoveTextOut = null; /* act on the TextView Animation -- go out */private Animation mMoveTextIn = null;/* act on the TextView Animation -- enter */private Pa Int mPaint; private int mSpeed = DEFAULT_SPEED; private int mAnimationPause = DEFAULT_ANIMATION_PAUSE; private Interpolator mInterpolator = new LinearInterpolator (); private Runnable mAnimationStartRunnable; /** interval between strings */private String interval = ""; private String stringOfItem = "";/** str + interval Length */private float widthOfItem = 0; private float widthOfTextView; private String stringOfTextView = ""; Private float startXOfOut = 0; private float endXOfOut = 0; private float startXOfIn = 0; private float endXOfIn = 0; public MarqueeView (Context context) {super (context ); init (context);} public MarqueeView (Context context, AttributeSet attrs) {super (context, attrs); init (context);} public MarqueeView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); init (context);} private Void init (Context context) {// init helpermPaint = new Paint (); mPaint. setAntiAlias (true); mPaint. setStrokeWidth (1); mPaint. setStrokeCap (Paint. cap. ROUND); mInterpolator = new LinearInterpolator () ;}// called when the position and size of the subview are assigned. @ Overrideprotected void onLayout (boolean changed, int l, int t, int r, int B) {super. onLayout (changed, l, t, r, B); Logcat. d (TAG, "onLayout called! "+" Changed: "+ changed); if (getChildCount () = 0 | getChildCount ()> 1) {throw new RuntimeException (" MarqueeView must have exactly one child element. ") ;}// if (changed) {View v = getChildAt (0); if (! (V instanceof TextView) {throw new RuntimeException ("The child view of this MarqueeView must be a TextView instance. ");} initView (getContext (); mTextField. setText (mTextField. getText () ;}}/** Starts the configured marquee effect. */public void startMarquee () {Logcat. d (TAG, "startMarquee called"); startTextFieldAnimation () ;}// the listener is responsible for starting the animation. Private void startTextFieldAnimation () {mAnimationStartRunnable = new Runnable () {public void run () {mTextField. startAnimation (mMoveTextOut) ;}}; postDelayed (mAnimationStartRunnable, mAnimationPause);}/*** Disables the animations. */public void reset () {if (mAnimationStartRunnable = null) return; removeCallbacks (mAnimationStartRunnable); mTextField. clearAnimation (); mMoveTextOut. reset (); mMoveTextIn. res Et (); invalidate ();} private void prepareAnimation () {// MeasuremPaint. setTextSize (mTextField. getTextSize (); mPaint. setTypeface (mTextField. getTypeface (); float mTextWidth = mPaint. measureText (mTextField. getText (). toString (); float width = getMeasuredWidth (); startXOfOut =-(mTextWidth-width) % widthOfItem; endXOfOut =-mTextWidth + width; startXOfIn =-(mTextWidth-width) % widthOfItem; endXOfIn =-m TextWidth + width; final int duration = (int) Math. abs (startXOfOut-endXOfOut) * mSpeed); if (BuildConfig. DEBUG) {Log. d (TAG, "(int) Math. abs (startXOfOut-endXOfOut): "+ (int) Math. abs (startXOfOut-endXOfOut); Log. d (TAG, "mSpeed:" + mSpeed); Log. d (TAG, "startXOfOut:" + startXOfOut); Log. d (TAG, "endXOfOut:" + endXOfOut); Log. d (TAG, "startXOfIn:" + startXOfIn); Log. d (TAG, "endXOfIn:" + endXO FIn); Log. d (TAG, "duration:" + duration);} mMoveTextOut = new TranslateAnimation (startXOfOut, endXOfOut, 0, 0); mMoveTextOut. setDuration (duration); mMoveTextOut. setInterpolator (mInterpolator); mMoveTextOut. setFillAfter (true); mMoveTextIn = new TranslateAnimation (startXOfIn, endXOfIn, 0, 0); mMoveTextIn. setDuration (duration); mMoveTextIn. setStartOffset (mAnimationPause); mMoveTextIn. setInterpolator (mInterp Olator); mMoveTextIn. setFillAfter (true); mMoveTextOut. setAnimationListener (new Animation. animationListener () {public void onAnimationStart (Animation animation) {} public void onAnimationEnd (Animation animation) {mTextField. startAnimation (mMoveTextIn);} public void onAnimationRepeat (Animation animation) {}}); mMoveTextIn. setAnimationListener (new Animation. animationListener () {public void onAnimationStar T (Animation animation) {}public void onAnimationEnd (Animation animation) {startTextFieldAnimation ();} public void onAnimationRepeat (Animation animation ){}});} /** initialize the sub-View */private void initView (Context context) {// Scroll ViewLayoutParams sv1lp = new LayoutParams (LayoutParams. MATCH_PARENT, LayoutParams. WRAP_CONTENT); sv1lp. gravity = Gravity. CENTER_HORIZONTAL; mScrollView = new ScrollView (context );// Scroll View 1-Text FieldmTextField = (TextView) getChildAt (0); removeView (mTextField); mScrollView. addView (mTextField, new ScrollView. layoutParams (TEXTVIEW_VIRTUAL_WIDTH, LayoutParams. WRAP_CONTENT); mTextField. addTextChangedListener (new TextWatcher () {@ Overridepublic void beforeTextChanged (CharSequence charSequence, int I, int i2, int i3) {}@ Overridepublic void onTextChanged (CharSequence charSeque Nce, int I, int i2, int i3) {}@ Overridepublic void afterTextChanged (Editable editable) {Logcat. d (TAG, "afterTextChanged called"); // if the provided string has not been processed, it will be processed first; otherwise, the animation will start if (! StringOfTextView. equals (editable. toString () {String str = editable. toString (); mPaint. setTextSize (mTextField. getTextSize (); mPaint. setTypeface (mTextField. getTypeface (); stringOfItem = str + interval; widthOfItem = mPaint. measureText (stringOfItem); stringOfTextView = stringOfItem; widthOfTextView = widthOfItem; while (widthOfTextView <= 2 * getMeasuredWidth () {stringOfTextView + = stringOfItem; widthOfT ExtView = mPaint. measureText (stringOfTextView);} Logcat. d (TAG, "string of TextView deal OK! ### "); Logcat. d (TAG, "lengthOfll:" + getMeasuredWidth () + "###"); Logcat. d (TAG, "lengthOfTextView:" + widthOfTextView + "###"); Logcat. d (TAG, "CONTENT:" + stringOfTextView + "###"); // set the start mTextField. setText (stringOfTextView); return;} reset (); prepareAnimation (); expandTextView (); post (new Runnable () {@ Overridepublic void run () {startMarquee () ;}}) ;}}); addView (mScrollView, sv1lp);} private void expandTextView () {ViewGroup. layoutParams lp = mTextField. getLayoutParams (); lp. width = (int) widthOfTextView + 5; mTextField. setLayoutParams (lp );}}

Usage:

<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"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <com.example.test_marquee.MarqueeView        android:id="@+id/marqueeView"        android:layout_width="match_parent"        android:layout_height="wrap_content" >        <TextView            android:id="@+id/tv_marquee"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:ellipsize="end"            android:singleLine="true"            android:text="Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do."            android:textSize="20sp"            android:textStyle="bold" />    </com.example.test_marquee.MarqueeView></RelativeLayout>

Demo:

Http://download.csdn.net/detail/u012587637/8219987

!!!!! There are some bugs where the text length exceeds the text box and no playback is performed. Change it later.



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.