博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
textview(跑马灯效果)文字长短不限循环播放
阅读量:5821 次
发布时间:2019-06-18

本文共 4289 字,大约阅读时间需要 14 分钟。

textview显示跑马灯效果,使用的是继承的方法onDraw不停地绘制 

优点: 
1.文字长短不限哦 
2.不用非得获取焦点哦  

 

package feng.f7_27.activity;import android.content.Context;import android.graphics.Canvas;import android.graphics.Paint;import android.os.Parcel;import android.os.Parcelable;import android.util.AttributeSet;import android.view.Display;import android.view.View;import android.view.View.OnClickListener;import android.view.WindowManager;import android.widget.TextView;public class CustomTextView extends TextView implements OnClickListener{    public final static String TAG = CustomTextView.class.getSimpleName();    private float textLength = 0f;//�ı�����    private float viewWidth = 0f;    private float step = 0f;//���ֵĺ����    private float y = 0f;//���ֵ������    private float temp_view_plus_text_length = 0.0f;//���ڼ������ʱ����    private float temp_view_plus_two_text_length = 0.0f;//���ڼ������ʱ����    public boolean isStarting = false;//�Ƿ�ʼ����    private Paint paint = null;//��ͼ��ʽ    private String text = "";//�ı�����    public CustomTextView(Context context) {    super(context);    initView();    }    public CustomTextView(Context context, AttributeSet attrs) {    super(context, attrs);    initView();    }    public CustomTextView(Context context, AttributeSet attrs, int defStyle) {    super(context, attrs, defStyle);    initView();    }    /** *//**    * ��ʼ���ؼ�    */    private void initView()    {    setOnClickListener(this);    }    /** *//**    * �ı���ʼ����ÿ�θ���ı����ݻ����ı�Ч���֮����Ҫ���³�ʼ��һ��    */    public void init(WindowManager windowManager)    {    paint = getPaint();    text = getText().toString();    textLength = paint.measureText(text);    viewWidth = getWidth();    if(viewWidth == 0)    {    if(windowManager != null)    {    Display display = windowManager.getDefaultDisplay();    viewWidth = display.getWidth();    }    }    step = textLength;    temp_view_plus_text_length = viewWidth + textLength;    temp_view_plus_two_text_length = viewWidth + textLength * 2;    y = getTextSize() + getPaddingTop()+15;    }    @Override    public Parcelable onSaveInstanceState()    {    Parcelable superState = super.onSaveInstanceState();    SavedState ss = new SavedState(superState);    ss.step = step;    ss.isStarting = isStarting;    return ss;    }                            @Override    public void onRestoreInstanceState(Parcelable state)    {    if (!(state instanceof SavedState)) {    super.onRestoreInstanceState(state);    return;    }    SavedState ss = (SavedState)state;    super.onRestoreInstanceState(ss.getSuperState());    step = ss.step;    isStarting = ss.isStarting;    }    public static class SavedState extends BaseSavedState {    public boolean isStarting = false;    public float step = 0.0f;    SavedState(Parcelable superState) {    super(superState);    }    @Override    public void writeToParcel(Parcel out, int flags) {    super.writeToParcel(out, flags);    out.writeBooleanArray(new boolean[]{isStarting});    out.writeFloat(step);    }    public static final Parcelable.Creator
CREATOR = new Parcelable.Creator
() { public SavedState[] newArray(int size) { return new SavedState[size]; } @Override public SavedState createFromParcel(Parcel in) { return new SavedState(in); } }; private SavedState(Parcel in) { super(in); boolean[] b = null; in.readBooleanArray(b); if(b != null && b.length > 0) isStarting = b[0]; step = in.readFloat(); } } /** *//** * ��ʼ���� */ public void startScroll() { isStarting = true; invalidate(); } /** *//** * ֹͣ���� */ public void stopScroll() { isStarting = false; invalidate(); } @Override public void onDraw(Canvas canvas) { canvas.drawText(text, temp_view_plus_text_length - step, y, paint); if(!isStarting) { return; } step += 0.5; if(step > temp_view_plus_two_text_length) step = textLength; invalidate(); } @Override public void onClick(View v) { if(isStarting) stopScroll(); else startScroll(); }}
tv.setText("aaaaaavvv");        tv.init(getWindowManager());        tv.startScroll();

 

转载于:https://www.cnblogs.com/greywolf/archive/2013/02/01/2888888.html

你可能感兴趣的文章
让你的WPF程序在Win7下呈现Win8风格主题
查看>>
JDBC二查询(web基础学习笔记八)
查看>>
监听器(web基础学习笔记二十二)
查看>>
802.11 学习笔记
查看>>
Leetcode-Database-176-Second Highest Salary-Easy(转)
查看>>
构建Docker Compose服务堆栈
查看>>
最小角回归 LARS算法包的用法以及模型参数的选择(R语言 )
查看>>
Hadoop生态圈-Kafka常用命令总结
查看>>
如何基于Redis Replication设计并实现Redis-replicator?
查看>>
Linux 环境下 PHP 扩展的编译与安装 以 mysqli 为例
查看>>
浮点数内存如何存储的
查看>>
贪吃蛇
查看>>
EventSystem
查看>>
用WINSOCK API实现同步非阻塞方式的网络通讯
查看>>
玩一玩博客,嘿嘿
查看>>
P1352 没有上司的舞会
查看>>
ios11文件夹
查看>>
【HLOJ 559】好朋友的题
查看>>
Electric Fence(皮克定理)
查看>>
nvl 在mysql中如何处理
查看>>