• 跑马灯


    源码:http://download.csdn.net/detail/lm_zp/9517135

    xml

    [html]

    <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"  

    tools:context=".MainActivity" >  

    <com.bwie.androidtest.AutoText     

    android:id="@+id/autoTxt"      

    android:layout_width="match_parent"      

    android:layout_height="wrap_content"      

    android:background="#ffffff"      

    android:textColor="#00ff00"      

            android:textSize="35sp" />    

    </RelativeLayout>  

    自定义textview

           package com.bwie.androidtest;  

    import android.content.Context;  

    import android.graphics.Canvas;  

    import android.graphics.Color;  

    import android.graphics.Paint;  

    import android.graphics.Paint.FontMetrics;  

    import android.graphics.Typeface;  

    import android.util.AttributeSet;  

    import android.util.DisplayMetrics;  

    import android.util.Log;  

    import android.view.WindowManager;  

    import android.widget.TextView;  

    public class AutoText extends TextView {  

        private int width, height;  

        private Paint paintText;  

        private float posx, posy;  

        private float speed = 0.0f;  

        private String text = "hello haha";  

        private float textWidth = 0;  

        private float moveDistance = 0.0f;  

        private boolean isStarting = false;  

       public AutoText(Context context) {  

           super(context);  

        }  

        public AutoText(Context context, AttributeSet attrs) {  

          super(context, attrs);  

        }  

        private void initView() {  

             paintText = new Paint();  

            //字体大小  

            paintText.setTextSize(50.0f);  

            //字体颜色  

            paintText.setColor(Color.BLACK);  

         paintText.setTypeface(Typeface.DEFAULT_BOLD);  

            paintText.setAntiAlias(true);  

    text = getText().toString();  

    textWidth = paintText.measureText(text);  

            Log.e("msg", "textWidth= " + textWidth);  

    this.speed = textWidth;  

    moveDistance = textWidth * 2 + width;  

        }  

       public void initDisplayMetrics(WindowManager windowManager) {  

          /* 取得屏幕分辨率大小 */  

            DisplayMetrics dm = new DisplayMetrics();  

            windowManager.getDefaultDisplay().getMetrics(dm);  

    this.width = dm.widthPixels;  

    this.height = dm.heightPixels;  

            initView();  

    this.posx = width + textWidth;  

            FontMetrics fm = paintText.getFontMetrics();  

            float baseline = fm.descent - fm.ascent;  

            //字体显示高度  

    this.posy = height / 2 - baseline;  

        }  

        public void startScroll() {  

    isStarting = true;  

            invalidate();  

        }  

        public void stopScroll() {  

    isStarting = false;  

            invalidate();  

        }  

        @Override  

        protected void onDraw(Canvas canvas) {  

            // super.onDraw(canvas);  

            canvas.drawText(text, posx - speed, posy, paintText);  

            if (!isStarting) {  

                return;  

            }  

            speed += 2.0f;  

            if (speed > moveDistance)  

    speed = textWidth;  

            invalidate();  

        }  

    }  

     

    package com.bwie.androidtest;  

    import android.os.Bundle;  

    import android.app.Activity;  

    import android.text.TextUtils.TruncateAt;  

    import android.view.Menu;  

    import android.widget.TextView;  

    public class MainActivity extends Activity {  

        String str11 = "促进青年教师全面发展, 引导广大高校青年教师为实现中华民族伟大复兴的中国梦贡献力" + " "  

                + "促进青年教师全面发展, 引导广大高校青年教师为实现中华民族伟大复兴的中国梦贡献力" + " "  

                + "  djsdnh   kshdfjks       ";  

        @Override  

        protected void onCreate(Bundle savedInstanceState) {  

            super.onCreate(savedInstanceState);  

            setContentView(R.layout.activity_main);  

            scroll3();  

        }  

        void scroll3() {  

            AutoText auto = (AutoText) findViewById(R.id.autoTxt);  

            auto.setText(str11);  

            //传递屏幕宽度  

            auto.initDisplayMetrics(getWindowManager());  

            //开始  

            auto.startScroll();  

        }  

    }  

     

  • 相关阅读:
    HDU1998奇数阶魔方 一个好玩的程序 按规则写蛮简单的
    ASCII码表
    排序之快速排序算法
    HDU1002 A + B Problem II
    SDUT2136数据结构实验之二叉树的建立与遍历
    (转)计算组合数整数拆分
    IE CSS Bug及解决方案参考手册
    包含空格的项目的文件/路径部分需要用括号括起来
    Ext学习笔记07 Window及Window中的布局
    Google Map 2.0 获取当前倍率和当前点击所在地理坐标
  • 原文地址:https://www.cnblogs.com/hnpy/p/5488312.html
Copyright © 2020-2023  润新知