ViewGroup enables you to scroll up or down or left and right to run the lantern. viewgroup

Source: Internet
Author: User

ViewGroup enables you to scroll up or down or left and right to run the lantern. viewgroup

First:




Code:

Package com. example. scrolltextview; import java. util. hashMap; import java. util. iterator; import java. util. map; import android. content. context; import android. content. res. typedArray; import android. graphics. paint; import android. OS. handler; import android. OS. message; import android. util. attributeSet; import android. util. log; import android. view. layoutInflater; import android. view. view; import android. view. viewGr Oup; import android. widget. arrayAdapter; import android. widget. linearLayout; import android. widget. scroller; import android. widget. spinnerAdapter; import android. widget. textView; public class ScrollTextView extends ViewGroup {private static final int SCROLL = 0; private static final int JUSTIFY = 1; private static final int ANIMATION_DURATION = 3000; private int mOrientation; private int mSize; private int m Index; private int mPosition; private Scroller mScroller; private Map <View, Integer> mViews; private SpinnerAdapter mAdapter; private int mPackedViews; private int mAnimationDuration; private float measuredWidth = 0; private float textSize = 0; private float measuredHeight = 0; private String text = null; private String [] data; private boolean flag = true; public ScrollTextView (Context context, AttributeSet att Rs) {super (context, attrs); int [] linerarLayoutAttrs = {android. r. attr. orientation,}; TypedArray a = context. obtainStyledAttributes (attrs, linerarLayoutAttrs); mOrientation =. getInteger (0, LinearLayout. HORIZONTAL);. recycle (); mAnimationDuration = ANIMATION_DURATION; mScroller = new Scroller (context); mIndex =-1; mPosition =-1; mPackedViews =-1; mViews = new HashMap <View, integer> (); setFocusable (true); SetFocusableInTouchMode (true);} public float getCharacterWidth (String text, float size) {if (null = text | "". equals (text) return 0; float width = 0; Paint paint = new Paint (); paint. setTextSize (size); float text_width = paint. measureText (text); // obtain the total length width = text_width/text. length (); // return width;} public void setText (String text) {this. text = text; data = new String [] {this. text}; LayoutInflater I Nflater = (LayoutInflater) getContext (). getSystemService (getContext (). LAYOUT_INFLATER_SERVICE); TextView view = (TextView) inflater. inflate (R. layout. scroll_text_view, null); textSize = view. getTextSize (); measuredHeight = textSize; textSize = getCharacterWidth (text, textSize);} public void beginScroll () {thread. start () ;}thread Thread = new thread (new Runnable () {boolean enabled = true; @ Overridepublic void Run () {// TODO Auto-generated method stubwhile (enabled) {try {Thread. sleep (4000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke. printStackTrace ();} setNextView (); enabled = mIndex + 1 <mAdapter. getCount () ;}}); @ Overrideprotected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {super. onMeasure (widthMeasureSpec, heightMeasureSpec); measuredWidth = getMeasuredWidth (); MSize = mOrientation = LinearLayout. HORIZONTAL? GetMeasuredWidth (): getMeasuredHeight (); if (flag & text! = Null) {int length = text. length (); Log. I ("AAA", "length * textSize:" + length * textSize + "measuredWidth:" + measuredWidth); if (length * textSize> = measuredWidth) {int textCountInOneLine = (int) (measuredWidth/textSize-1); int lineCount = (int) (length % textCountInOneLine = 0 )? (Length/textCountInOneLine) :( length/textCountInOneLine + 1); Log. I ("AAA", "length:" + length + "textCountInOneLine:" + textCountInOneLine + "lineCount:" + lineCount); data = new String [lineCount]; for (int I = 0; I <lineCount; I ++) {if (I + 1) * textCountInOneLine <length) data [I] = text. substring (I * textCountInOneLine, (I + 1) * textCountInOneLine); elsedata [I] = text. substring (I * textCountInOneLine, length); Log. I ("AAA", "data: "+ Data [I]) ;}} else {data = new String [] {text };} ArrayAdapter <String> adapter = new ArrayAdapter <String> (getContext (), r. layout. scroll_text_view, data); setAdapter (adapter); flag = false;} View view = getChildAt (0); if (view! = Null) {measureChild (view, widthMeasureSpec, widthMeasureSpec); measuredHeight = view. getMeasuredHeight ();} setMeasuredDimension (int) measuredWidth, (int) measuredHeight);} private int getPackedViews (int offset) {int size = mSize; int start = offset/size; int numViews = offset % size! = 0? 1: 0; return start <1 | numViews;} Handler handler = new Handler () {@ Overridepublic void handleMessage (Message msg) {mScroller. computescroloffset (); int currX = mScroller. getCurrX (); int delta = mPosition-currX; mPosition = currX; int packed = getPackedViews (mPosition); manageViews (packed); scroll (delta); if (! MScroller. isFinished () {handler. sendEmptyMessage (msg. what);} else {if (msg. what = SCROLL) {justify () ;}else {mIndex = mPosition/mSize ;}}; private void justify () {int offset = mPosition % mSize; if (offset! = 0) {int endPosition = mPosition-offset; if (offset> mSize/2) {endPosition + = mSize;} mScroller. startScroll (mPosition, 0, endPosition-mPosition, 0, mAnimationDuration); handler. sendEmptyMessage (JUSTIFY);} else {mIndex = mPosition/mSize;} private void scroll (int offset) {if (mOrientation = LinearLayout. HORIZONTAL) {for (View view: mViews. keySet () {view. offsetLeftAndRight (offset) ;}} else {f Or (View view: mViews. keySet () {view. offsetTopAndBottom (offset) ;}} invalidate () ;}public void setSelection (int index, boolean animate) {if (index = mIndex) {return ;} int endPosition = index * mSize; int diff = Math. abs (index-mIndex); int sign = index> mIndex? 1:-1; mIndex = index; if (diff> 1) {mPosition = endPosition-sign * mSize;} if (animate) {mScroller. startScroll (mPosition, 0, endPosition-mPosition, 0, mAnimationDuration); handler. removeMessages (JUSTIFY); handler. removeMessages (SCROLL); handler. sendEmptyMessage (JUSTIFY);} else {mPosition = endPosition; manageViews (index <1); invalidate () ;}} private void manageViews (int packedViews) {if (packedView S = mPackedViews) {return;} mPackedViews = packedViews; int startIdx = packedViews> 1; int endIdx = startIdx + (packedViews & 1); int viewIdx = startIdx; while (viewIdx <= endIdx) {if (! MViews. containsValue (viewIdx) {if (viewIdx> = 0 & viewIdx <mAdapter. getCount () {View view = mAdapter. getView (viewIdx, null, this); mViews. put (view, viewIdx); addView (view) ;}} viewIdx ++;} // remove not visible viewsIterator <View> iterator = mViews. keySet (). iterator (); while (iterator. hasNext () {View view = iterator. next (); int idx = mViews. get (view); if (idx <startIdx | idx> endIdx) {iterator. remov E (); removeView (view) ;}} public int getSelection () {return mIndex;} public void setpreviusview () {if (mAdapter! = Null & mIndex> 0) {setSelection (mIndex-1, true) ;}} public void setNextView () {if (mAdapter! = Null & mIndex + 1 <mAdapter. getCount () {setSelection (mIndex + 1, true) ;}} public void setAdapter (SpinnerAdapter adapter) {mAdapter = adapter; if (mAdapter! = Null) {setSelection (0, false) ;}@ Overrideprotected void onLayout (boolean changed, int l, int t, int r, int B) {for (View view: mViews. keySet () {if (view. getWidth () = 0) {// new View: not layout () edint idx = mViews. get (view); if (mOrientation = LinearLayout. HORIZONTAL) {int left = mSize * idx-mPosition; view. layout (left, 0, left + r-l, B-t);} else {int top = mSize * idx-mPosition; view. layout (0, top, r-l, top + B-t );}}}}}

Usage:

String temp = getResources().getString(R.string.info);    ScrollTextView switcher1 = (ScrollTextView) findViewById(R.id.switcher1);switcher1.setText(temp);switcher1.beginScroll();

Alas, I think I'm really lazy ....

Or directly to the project!

Source code download


Continuous upward and downward scrolling marquee ~ Urgent ~

Function Marquee (){
If (demo1.offsetHeight-demo. scrollTop <= 0)
Demo. scrollTop-= demo1.offsetHeight
Else {
Demo. scrollTop ++
}
}

TextView source code

Use custom TextViewpublic class extends TextView {public AlwaysMarqueeTextView (Context context) {super (context); // TODO Auto-generated constructor stub} public AlwaysMarqueeTextView (Context context, AttributeSet attrs) {super (context, attrs);} public AlwaysMarqueeTextView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle);} @ Override public boolean isFocused () {return true ;}@ Override protected void onFocusChanged (boolean focused, int direction, Rect previuslyfocusedrect) {if (focused) super. onFocusChanged (focused, direction, previuslyfocusedrect);} @ Override public void onWindowFocusChanged (boolean focused) {if (focused) super. onWindowFocusChanged (focused);} xml <com. example. usage: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_gravity = "center_vertical" android: layout_marginLeft = "10dp" android: ellipsize = "marquee" android: focusable = "true" android: focusableInTouchMode = "true" android: marqueeRepeatLimit = "marquee_forever" android: singleLine = "true" android: text = "here is the announcement of Tianxiang lighting .... use text marquee effects .... use text marquee effects .... use text marquee effects .... use text marquee effects .... use text marquee effects .... use text marquee effect "/>

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.