實現android之文字左右滾動

jopen 10年前發布 | 56K 次閱讀 Android Android開發 移動開發

下面貼一下代碼:

<com.example.masong_yuanchengdianbo.AutoScrollTextView  
       android:layout_width="fill_parent"  
       android:id="@+id/textView"  
       android:textSize="15dip"  
       android:layout_height="wrap_content"  
       /> 


com.example.masong_yuanchengdianbo下代碼:

    public class AutoScrollTextView extends TextView {  
        /** 文字長度 */  
        private float textLength = 0f;  
        /** 滾動條長度 */  
        private float viewWidth = 0f;  
        /** 文本x軸 的坐標 */  
        private float tx = 0f;  
        /** 文本Y軸的坐標 */  
        private float ty = 0f;  
        /** 文本當前長度 */  
        private float temp_tx1 = 0.0f;  
        /** 文本當前變換的長度 */  
        private float temp_tx2 = 0x0f;  
        /** 文本滾動開關 */  
        private boolean isStarting = false;  
        /** 畫筆對象 */  
        private Paint paint = null;  
        /** 顯示的文字 */  
        private String text = "";  

        public AutoScrollTextView(Context context, AttributeSet attrs) {  
            super(context, attrs);  
        }  

        /**  
         * 初始化自動滾動條,每次改變文字內容時,都需要重新初始化一次  
         *   
         * @param windowManager  
         *            獲取屏幕  
         * @param text  
         *            顯示的內容  
         */  
        public void initScrollTextView(WindowManager windowManager, String text) {  
            // 得到畫筆,獲取父類的textPaint  
            paint = this.getPaint();  
            // 得到文字  
            this.text = text;  

            textLength = paint.measureText(text);// 獲得當前文本字符串長度  
            viewWidth = this.getWidth();// 獲取寬度return mRight - mLeft;  
            if (viewWidth == 0) {  
                if (windowManager != null) {  
                    // 獲取當前屏幕的屬性  
                    Display display = windowManager.getDefaultDisplay();  
                    viewWidth = display.getWidth();// 獲取屏幕寬度  

                }  
            }  
            tx = textLength;  
            temp_tx1 = viewWidth + textLength;  
            temp_tx2 = viewWidth + textLength * 2;// 自己定義,文字變化多少  
            // 文字的大小+距頂部的距離  
            ty = this.getTextSize() + this.getPaddingTop();  
        }  

        /**  
         * 開始滾動  
         */  
        public void starScroll() {  
            // 開始滾動  
            isStarting = true;  
            this.invalidate();// 刷新屏幕  
        }  

        /**  
         * 停止方法,停止滾動  
         */  
        public void stopScroll() {  
            // 停止滾動  
            isStarting = false;  
            this.invalidate();// 刷新屏幕  
        }  

        /** 重寫onDraw方法 */  
        @Override  
        protected void onDraw(Canvas canvas) {  
            if (isStarting) {  
                // A-Alpha透明度/R-Read紅色/g-Green綠色/b-Blue藍色  
                paint.setARGB(255, 200, 200, 200);  
                canvas.drawText(text, temp_tx1 - tx, ty, paint);  
                tx += 0.4;  
                // 當文字滾動到屏幕的最左邊  
                if (tx >= temp_tx2) {  
                    // 把文字設置到最右邊開始  
                    tx = temp_tx1 - viewWidth;  
                }  
                this.invalidate();// 刷新屏幕  
            }  
            super.onDraw(canvas);  
        }  
    }  


主activity代碼:

autoScrollTextView = (AutoScrollTextView) findViewById(R.id.textView);  

    String path = this.getIntent().getStringExtra("path");  
    System.out.println("path = "+path);  
    if(path!=null){  
        autoScrollTextView.initScrollTextView(this.getWindowManager(),  
                path);  
        autoScrollTextView.starScroll();  
    }else{  
        autoScrollTextView.initScrollTextView(this.getWindowManager(),  
                "歡迎使用遠程點播");  
        autoScrollTextView.starScroll();  

    }  
 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!