一个简单的跑马灯APP

一个简单的跑马灯APP

最近突然想写点东西,因为前段时间课程实验要求,写了挺多的安卓的App,所以想试试看。都是些很基础的东西,希望能帮助到有缘人。写的不好请谅解。

基本原理

android中可以直接设置显示跑马灯,如下所示。但是使用这种方法有个致命缺陷,就是文字过长时无法实现跑马灯效果。
//设置为跑马灯显示

       android:ellipsize="marquee"

因此我自定义TextView,每次run偏移时textview控件的位置,并创建线程每几毫秒便执行一次,以达到跑马灯的效果。

APP的布局

 <?xml version="1.0" encoding="utf-8"?>
<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">

<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="150px"
    android:orientation="horizontal">
<Button
    android:id="@+id/start"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:onClick="start"
    android:text="开始" />

<Button
    android:id="@+id/jiasu"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:onClick="jiasu"
    android:text="加速" />

    <Button
        android:id="@+id/jianSu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="jianSu"
        android:text="减速" />
</LinearLayout>
<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="150px"
    android:orientation="horizontal">

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



<Button
    android:id="@+id/fx"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="fx"
    android:layout_weight="1"
    android:text="改变方向" />

<Button
    android:id="@+id/startfor0"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="startFromHead"
    android:layout_weight="1"
    android:text="重置" />//这六个按钮是操作用的按钮

</LinearLayout>
<com.example.a.myapplication.MarqueeText
    android:id="@+id/test"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#A4D3EE"
    android:ellipsize="marquee"
    android:singleLine="true"
    android:text="加油,努力"
    android:textColor="#000000"
    android:textSize="20dp" />

<com.example.a.myapplication.MarqueeText
    android:id="@+id/test2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#B4EEB4"
    android:ellipsize="marquee"
    android:singleLine="true"
    android:text="努力,奋斗,拼搏"
    android:textColor="#FF0000"
    android:textSize="20dp" />//这两个是跑马灯中的文字

自定义TextView

因为我的代码注释的挺详细的了,所以不再赘述,看代码注释就ok了。

public class MarqueeText extends android.support.v7.widget.AppCompatTextView implements Runnable {
private int currentScrollX; // 当前滚动的位置
private boolean isStop = false;//是否已停止
private int textWidth;
private boolean isMeasure = false;//获取文字宽度
private float speed=2;//速度
private int fx=0;//运行的方向

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() {
    if(fx==0) {
        currentScrollX -= speed;// 滚动速度.+号表示往左边-
        scrollTo(currentScrollX, 0);
        if (isStop) {
            return;
        }
        if (getScrollX() <= - (this.getWidth())) {
            currentScrollX = (this.getWidth());// 当前出现的位置
        }
        postDelayed(this, 1);//创建线程每jimiao跑一次
    }
    else
    {
        currentScrollX += speed;// 滚动速度.+号表示往左边-
        scrollTo(currentScrollX, 0);
        if (isStop) {
            return;
        }
        if (getScrollX() >= (this.getWidth())) {
            currentScrollX = -(this.getWidth());// 当前出现的位置
        }
        postDelayed(this, 1);//创建线程每一秒运行一次
    }
}

/*
   public void run() {
    currentScrollX += 1;// 滚动速度.+号表示往左边-
    scrollTo(currentScrollX, 0);
    if (isStop) {
        return;
    }
    if (getScrollX() >= (textWidth)) {
        currentScrollX = -(this.getWidth());// 当前出现的位置
    }
    postDelayed(this, 1);//创建线程每jimiao
}
 */
/*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 jiasu(){
    speed+=0.2;
}
//减速
public void jianSu(){
    if(speed>1){
        speed-=0.1;
    }
    else{
        speed=1;
    }
}

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

//改变方向
public void fx(){
    if(fx==0){
        fx=1;
    }
    else{
        fx=0;
    }
}
// 从头开始滚动
public void startFromHead() {
    currentScrollX = 0;
    speed=1;
    startScroll();
}
}

在MainActivity调用

    public class MainActivity extends Activity {
private MarqueeText test;
private MarqueeText test2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    test=(MarqueeText) findViewById(R.id.test);
    test2=(MarqueeText) findViewById(R.id.test2);
}
//加速
public void jiasu(View v){
    test.jiasu();
    test2.jiasu();
}
//改变方向
public void fx(View v){
    test.fx();
    test2.fx();
}
//减速
public void jianSu(View v){
    test.jianSu();
    test2.jianSu();
}
//开始
public void start(View v){
    test.startScroll();
    test2.startScroll();
}

//停止
public void stop(View v){
    test.stopScroll();
    test2.stopScroll();
}
//重置
public void startFromHead(View v){
    test.startFromHead();
    test2.startFromHead();
}
}

截图

在这里插入图片描述

希望以上能给你带来帮助,我知道第一次写肯定不好,所以请见谅。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值