TextView文字滚动、变色

本文介绍了一种基于Android平台的滚动马赛克效果TextView实现方法。通过TextView组件结合Handler定时任务,实现了文本的水平滚动及文字颜色的随机变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

布局文件中:

<TextView
    android:id="@+id/myTextView"
    android:layout_gravity="center_horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:ellipsize="marquee"
    android:focusable="true"
    android:marqueeRepeatLimit="marquee_forever"
    android:singleLine="true"
    android:textSize="24sp"
    android:text="北京八维研修学院是经北京市教委批准的以IT工程专业为主,集职业性高等教育、职业技能认证等教育为一体的全日制现代职业教育高等院校,民办非学历高等教育机构。"
    android:textColor="@android:color/black" >
</TextView>
代码:
package com.example.a05_horse_race_lamp;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what == 0){
                Random random = new Random();
                int red = random.nextInt(255);
                int green = random.nextInt(255);
                int blue = random.nextInt(255);
                textView.setTextColor(Color.rgb(red, green, blue));
                handler.sendEmptyMessageDelayed(0, 200);
            }
        }
    };
    private TextView textView;

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

        textView = findViewById(R.id.myTextView);
        textView.setSelected(true);
        textView.setTextColor(Color.rgb(100, 100, 100));
        handler.sendEmptyMessageDelayed(0, 200);
        
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值