Android 应用中TextView垂直滚动

jopen 12年前

项目中欢迎词多的时候需要实现上下滚动,了解到在android中TextView可以轻松实现横向跑马灯效果,但是对垂直滚动没有直接的支持方法,于是百度上谷歌,谷歌上百度,最终还是没有发现一个拿来即用的demo,呵呵,于是自己研究了下,写了一个可以实现TextView垂直滚动的 demo,由于项目需要,在这里我使用的是AbsoluteLayout布局,左右键切换时更改滚动内容,希望此demo能给有同样需求的童鞋们带来帮助!

---写在前面

 

textscroll.xml配置:

    <?xml version="1.0" encoding="utf-8"?>        <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:orientation="vertical" >                    <TextView                android:id="@+id/tScroll"                android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:maxLines="5"                android:scrollbars="none"                android:singleLine="false"                android:textColor="#FF0000" >            </TextView>                </AbsoluteLayout>  
Java代码:
    package sue.test;                import java.util.ArrayList;        import java.util.List;                import com.amttgroup.element.Container;        import com.amttgroup.element.RootLayout;        import com.amttgroup.element.Text;        import com.amttgroup.utils.G;                import android.app.Activity;        import android.graphics.Color;        import android.os.Bundle;        import android.os.Handler;        import android.util.Log;        import android.view.Gravity;        import android.view.KeyEvent;        import android.widget.AbsoluteLayout;        import android.widget.TextView;                public class TextScrollActivity extends Activity {            TextView tv;            String L = "TextScrollActivity";            List<String> welcomeWords = new ArrayList<String>();            int curIndex = 0;                    @Override            protected void onCreate(Bundle savedInstanceState) {                super.onCreate(savedInstanceState);                        welcomeWords                        .add("        您的下榻使我们倍感荣幸。我谨代表北京饭店全体员工向您表示诚挚的欢迎。始建于1900年的北京饭店是一座历史悠久的豪华饭店。拥有豪华典雅的客房;口味独特的佳肴;会议会展设施及娱乐健身设施。我们将竭诚为您提供满意而舒适的服务。希望您在北京饭店下榻愉快。\n 您的下榻使我们倍感荣幸。我谨代表北京饭店全体员工向您表示诚挚的欢迎。始建于1900年的北京饭店是一座历史悠久的豪华饭店。拥有豪华典雅的客房;口味独特的佳肴;会议会展设施及娱乐健身设施。我们将竭诚为您提供满意而舒适的服务。希望您在北京饭店下榻愉快。");                welcomeWords                        .add("  It is an honor for you to stay at the Beijing Hotel. On behalf of the staff at the Beijing Hotel, I sincerely welcome you.Built in 1900, Beijing Hotel is a luxury hotel with a long history. We have elegant guestrooms, exquisite cuisine, convenient facilities and entertainment facilities. It is our pleasure to offer you the best services.Have a nice stay!");                        setContentView(R.layout.textscroll);                        tv = (TextView) findViewById(R.id.tScroll);                                /**                * 动态设置坐标及宽和高,也可以忽略,在配置文件中设置                */                AbsoluteLayout.LayoutParams lp = (AbsoluteLayout.LayoutParams) tv                        .getLayoutParams();                lp.x = 300;                lp.y = 300;                lp.width = 500;                lp.height = 170;                                tv.setTextSize(16);                tv.setTextColor(Color.WHITE);                tv.setGravity(Gravity.LEFT);                        tv.setText(welcomeWords.get(curIndex));                        h.postDelayed(r, 3000);            }                    Handler h = new Handler();             int i = 0;            Runnable r = new Runnable() {                        @Override                public void run() {                    int height = tv.getHeight();                    int scrollY = tv.getScrollY();                    int lineHeight = tv.getLineHeight();                    int lineCount = tv.getLineCount();//总行数                    /**                    * textView不可见内容的高度,可以理解为偏移位移                    */                    int maxY = (tv.getLineCount() * tv.getLineHeight()                            + tv.getPaddingTop() + tv.getPaddingBottom())                            - tv.getHeight();                                        Log.e("=maxY=", maxY+"");                    Log.e("=height=", height+"");                    Log.e("=lineCount=", tv.getLineCount()+"");                                        double viewCount = Math.floor(height / lineHeight);//可见区域最大显示多少行                    if (lineCount > viewCount) {//总行数大于可见区域显示的行数时则滚动                                if (scrollY >= maxY) {                            tv.scrollBy(0, -maxY);                        } else {                            tv.scrollBy(0, lineHeight);                        }                        h.postDelayed(this, 3000);                    }                        }                    };                    public boolean onKeyDown(int keyCode, KeyEvent event) {                        switch (keyCode) {                case KeyEvent.KEYCODE_DPAD_UP:                                        break;                case KeyEvent.KEYCODE_DPAD_DOWN:                                        break;                case KeyEvent.KEYCODE_DPAD_RIGHT:                                        handle();                    break;                case KeyEvent.KEYCODE_DPAD_LEFT:                                        handle();                    break;                case KeyEvent.KEYCODE_DPAD_CENTER:                    handle();                    break;                case KeyEvent.KEYCODE_ENTER:                    handle();                    break;                case KeyEvent.KEYCODE_BACK:                    finish();                                        break;                default:                                    }                return super.onKeyDown(keyCode, event);            }                    public void handle() {                        h.removeCallbacks(r);                                            curIndex = (curIndex + 1) % 2;                        tv.setText(welcomeWords.get(curIndex));                        h.postDelayed(r, 3000);                    }                    @Override            public void onDestroy() {                super.onDestroy();                h.removeCallbacks(r);            }                }