码迷,mamicode.com
首页 > 其他好文 > 详细

会游走的TextView

时间:2016-05-16 17:30:56      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

//自定义的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 = "促进青年教师全面发展,\n引导广大高校青年教师为实现中华民族伟大复兴的中国梦贡献力" + "\n"
            + "促进青年教师全面发展,\n引导广大高校青年教师为实现中华民族伟大复兴的中国梦贡献力" + "\n"
            + "  djsdnh   kshdfjks     \n\r\t ";

    @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();
    }

}

//布局

<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

标签:

原文地址:http://www.cnblogs.com/1995yu/p/5498465.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!