TextView跑马灯效果

来源:互联网 发布:淘宝网违规提醒 编辑:程序博客网 时间:2024/06/05 18:15

(1)

只要简单设置TextView的属性即可实现跑马灯效果,用FrameLayout布局可以实现跑马灯在其他View如VideoView上显示:

<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:gravity="center_vertical"
        android:focusable="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:focusableInTouchMode="true"
        android:scrollHorizontally="true"
        android:singleLine="true"

        android:textSize="20sp"
        android:textStyle="bold"
        android:text="文字要足够长,才能显示出跑马灯的效果,所以请写长一点" />


(2)

转:http://blog.csdn.net/chixinwuxue/article/details/41867407

Android自带的跑马灯效果不太好控制,还必须要满足条件才能有效果,而且速度不受控制。前面我的博客中有一篇就是用Android自带的跑马灯效果的,但是基于不同的使用效果,这里在网上找到了一个更好的方法。沿用了作者的一些方法,但是添加了更好的扩展功能,和大家一起分享。这里面有控制往左往右两个方向的实现。

1、首先是简单的布局main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <Button
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="start"
        android:text="开始" />


    <Button
        android:id="@+id/stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="stop"
        android:text="停止" />


    <Button
        android:id="@+id/startfor0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="startFromHead"
        android:text="重置" />


    <com.xuhui.customrolllight.MarqueeText
        android:id="@+id/test"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#339320"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:text="滚动效果,不管多少字"

       android:ellipsize = "marquee" // 跑马灯效果,字数不超过就不动,超过就滚动
        android:textColor="#000000"
        android:textSize="20dp" >
    </com.xuhui.customrolllight.MarqueeText>


</LinearLayout>

2、自定义滚动方法MarqueeText.java

package com.xuhui.customrolllight;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView;


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


public MarqueeText(Context context, AttributeSet attrs) {
super(context, attrs);
}


public MarqueeText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}


@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
currentScrollX = this.getWidth();
}


protected void onDraw(Canvas canvas) {
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() { currentScrollX-=2;//滚动速度.+号表示往左边-
* scrollTo(currentScrollX,0); if(isStop){ return; }
* if(getScrollX()<=-(this.getWidth())){ scrollTo(textWidth,0);
* currentScrollX=textWidth; } postDelayed(this, 5); }
*/
public void run() {
currentScrollX += 2;// 滚动速度.+号表示往左边-
scrollTo(currentScrollX, 0);
if (isStop) {
return;
}
if (getScrollX() >= (textWidth)) {
currentScrollX = -(this.getWidth());// 当前出现的位置
}
postDelayed(this, 1);
}
/*( public void run() {


// currentScrollX += 3;// 滚动速度.+号表示往左边-
// scrollTo(currentScrollX, 0);


if (textWidth>this.getWidth()) {
currentScrollX += 3;// 滚动速度.+号表示往左边-
scrollTo(currentScrollX, 0);
}
if (getScrollX() >= (textWidth)) {
// scrollTo(this.getWidth(),0);
currentScrollX = -(this.getWidth());// 当前出现的位置
}
postDelayed(this, 5);
})这里面实现的是没有省略号的效果。文字没有超出框的长度就不滚,超出就滚*/

// 开始滚动
public void startScroll() {
isStop = false;
this.removeCallbacks(this);
post(this);
}


// 停止滚动
public void stopScroll() {
isStop = true;
}


// 从头开始滚动
public void startFromHead() {
currentScrollX = 0;
startScroll();
}
}

上面注释掉的代码是实现文字往右边跑

3、下面是主程序MainActivity.java

package com.xuhui.customrolllight;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;


public class MainActivity extends Activity {


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

}
public void start(View v){
test.startScroll();
}
public void stop(View v){
test.stopScroll();
}
public void startFromHead(View v){
test.startFromHead();
}
}

源码下载:http://download.csdn.net/download/chixinwuxue/8249689


(3)

自定义MenuTextView.java:

package com.gci.station.view;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;


public class MenuTextView extends TextView implements Runnable {
/** 当前滚动的位置 */
private int currentScrollX;
private boolean isStop = false;
private int textWidth;
private boolean isMeasure = false;


private TranslateAnimation mShowAnimation;
private TranslateAnimation mHideAnimation;


public MenuTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}


public MenuTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}


public MenuTextView(Context context) {
super(context);
this.setVisibility(View.GONE);
}


@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
currentScrollX = this.getWidth();
}


@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (!isMeasure) {
getTextWidth();
isMeasure = true;
}
}


private void getTextWidth() {
Paint paint = this.getPaint();
String string = this.getText().toString();
textWidth = (int) paint.measureText(string);
}


public void run() {
if (textWidth > this.getWidth()) {
currentScrollX += 1;// 滚动速度.+号表示往左边-
scrollTo(currentScrollX, 0);
}
if (getScrollX() >= (textWidth)) {
currentScrollX = -(this.getWidth());// 当前出现的位置
}
postDelayed(this, 3);
}


/** 开始滚动 */
public void startScroll() {
isStop = false;
this.removeCallbacks(this);
post(this);
}


/** 停止滚动 */
public void stopScroll() {
isStop = true;
}


/** 从头开始滚动 */
public void startFromHead() {
currentScrollX = 0;
startScroll();
}


public void showMenu() {
// 从自已-1倍的位置移到自己原来的位置
mShowAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
mShowAnimation.setDuration(1000);
this.setVisibility(View.VISIBLE);
this.startAnimation(mShowAnimation);
}


public void hideMenu() {
mHideAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -1.0f);
mHideAnimation.setDuration(500);
this.startAnimation(mHideAnimation);
this.setVisibility(View.GONE);
}
}


然后在MainActivity.java:

public class MainActivity extends Activity {
/** 锁屏上方的通知 */
private MenuTextView tv_inform;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

                tv_inform.showMenu();
}

}

0 0
原创粉丝点击