The Dreamer
The Walker
——————————
*这里的文字都是分镜手的点子笔记,文手勿喷。
送给30后的我的礼物。
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.Display;
import android.view.WindowManager;
import android.widget.TextView;
public class AutoScrollTextView extends TextView
{
// 支持滚动的走马灯
private Paint paint = null; // 画笔对象
private float viewWidth = 0f; // 滚动长度
private float textLength = 0f; // 文字长度
private float yPoint = 0f; // 文本Y轴的坐标
private float xPoint = 0f; // 文本x轴 的坐标
private String displayText = ""; // 显示的文字
private float temp_tx1 = 0.0f; // 文本当前长度
private boolean isStarting = false; // 文本滚动开关
private float temp_tx2 = 0x0f; // 文本当前变换的长度
public AutoScrollTextView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
// 初始化自动滚动条,每次改变文字内容时,都需要重新初始化一次
@SuppressWarnings("deprecation")
public void initScrollTextView(WindowManager windowManager, String text)
{
paint = getPaint(); // 得到画笔,获取父类的textPaint
displayText = text; // 得到文字
textLength = paint.measureText(text);// 获得当前文本字符串长度
viewWidth = getWidth();// 获取宽度return mRight - mLeft;
if (viewWidth == 0)
{
if (windowManager != null)
{
Display display = windowManager.getDefaultDisplay(); // 获取当前屏幕的属性
viewWidth = display.getWidth();// 获取屏幕宽度
}
}
xPoint = textLength;
temp_tx1 = viewWidth + textLength;
temp_tx2 = viewWidth + textLength * 2; // 自己定义,文字变化多少
yPoint = 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)
{
canvas.drawText(displayText, temp_tx1 - xPoint, yPoint, paint);
xPoint += 2.6;
if (xPoint >= temp_tx2) // 当文字滚动到屏幕的最左边
xPoint = temp_tx1 - viewWidth; // 把文字设置到最右边开始
this.invalidate();// 刷新屏幕
}
super.onDraw(canvas);
}
}
---------------------------------------------------------------------------------------------------
<com.view.AutoScrollTextView
android:id="@+id/base_activity_atv_news"
style="@style/shadow"
android:layout_width="match_parent"
android:layout_height="36dp"
android:background="@drawable/pic_bg_marquee_news"
android:gravity="center_vertical"
android:paddingTop="4dp"
android:singleLine="true"
android:textColor="@color/white"
android:textSize="@dimen/size_16sp" />
---------------------------------------------------------------------------------------------------
tvNews = (AutoScrollTextView) findViewById(R.id.base_activity_atv_news);
---------------------------------------------------------------------------------------------------
tvNews.initScrollTextView(getWindowManager(), strNews);
tvNews.starScroll();