1. 程式人生 > >android 文字迴圈跑馬燈

android 文字迴圈跑馬燈

程式碼

public class MainActivity extends AppCompatActivity {
    private MarqueeText text;
    private LinearLayout layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = (MarqueeText) findViewById(R.id.test);
        layout = (LinearLayout) findViewById(R.id.my_layout);
    }


    public
void stop(View v) { text.stopScroll(); layout.setVisibility(View.GONE); } }
    public class MarqueeText extends TextView implements Runnable {

    private int currentScrollX;//當前滾動的位置
    private boolean isStop = false;
    private int textWidth;
    private boolean isMeasure = false
; public MarqueeText(Context context) { super(context); // TODO Auto-generated constructor stub } public MarqueeText(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } public MarqueeText(Context context, AttributeSet attrs, int
defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); if (!isMeasure) {//文字寬度只需要獲取一次就可以了 getTextWidth(); isMeasure = true; } } /** * 獲取文字寬度 */ private void getTextWidth() { Paint paint = this.getPaint(); String str = this.getText().toString(); textWidth = (int) paint.measureText(str); } @Override public void run() { // TODO Auto-generated method stub currentScrollX -= 2; // Rolling speed scrollTo(currentScrollX, 0); if (isStop) { return; } if (getScaleX() <= -(this.getWidth())) { scrollTo(textWidth, 0); currentScrollX = textWidth; //return; } postDelayed(this, 5); } // start public void startScroll() { isStop = false; this.removeCallbacks(this); post(this); } // stop public void stopScroll() { isStop = true; } // Start from scratch public void startFor0() { currentScrollX = 0; startScroll(); } }

佈局檔案

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/activity_main"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    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="example.wy.MainActivity">

    <LinearLayout
        android:id="@+id/my_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingLeft="5dp"
        android:background="#e5e2e2"
        android:paddingRight="5dp">

        <example.wy.MarqueeText
            android:id="@+id/test"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:background="#e5e2e2"
            android:ellipsize="marquee"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:marqueeRepeatLimit="marquee_forever"

            android:singleLine="true"
            android:text="跑馬燈,套馬杆的漢子你威武雄壯"
            android:textColor="#000000"
            android:textSize="16sp" >
        </example.wy.MarqueeText>

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="20dp"
            android:background="#e5e2e2"
            android:onClick="stop"
            android:text="停止"
            android:layout_marginLeft="5dp"
            android:textSize="16sp" />
    </LinearLayout>
</LinearLayout>