android TextView set neon effect

TextView set neon effect

TextView is one of the most commonly used controls in android, and it is also a very simple control. Here, a simple demo is made by using TextView to familiarize yourself with TextView, which is suitable for android novice partners to have a look.
The whole process is to use threads to send messages every other time, change the background color of TextView, and finally achieve the effect of neon lights. I'm also a novice. I don't understand thread very well. Welcome to ask questions. Let's discuss. Here is the code for the demo.
activity_main.xm
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="f3.nsu.com.neon.MainActivity">

    <TextView
        android:id="@+id/text01"
        android:layout_width="240dp"
        android:layout_height="240dp"
        android:layout_gravity="center"/>
    <TextView
        android:id="@+id/text02"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_gravity="center"/>
    <TextView
        android:id="@+id/text03"
        android:layout_width="160dp"
        android:layout_height="160dp"
        android:layout_gravity="center"/>
    <TextView
        android:id="@+id/text04"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_gravity="center"/>
    <TextView
        android:id="@+id/text05"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_gravity="center"/>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
        android:layout_gravity="center"
        android:contentDescription="@string/app_name"/>

</FrameLayout>

MainActivity.java
package f3.nsu.com.neon;

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

import java.util.Timer;
import java.util.TimerTask;

/**
 * TextView The neon lights
 */
public class MainActivity extends AppCompatActivity {

    //Save TextView ID
    private int[] textIds = new int[]{R.id.text01,R.id.text02,R.id.text03,R.id.text04,R.id.text05};
    //Save color
    private int[] colors = new int[]{Color.BLACK,Color.BLUE,Color.CYAN,Color.DKGRAY,Color.GRAY};
    //TextView array used to bind control
    private TextView[] views = new TextView[textIds.length];
    private Handler mHandler;
    private int currentColor = 0;   //Which color should I start with

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Loop through ID array binding control
        for (int i = 0;i<textIds.length;i++){
            views[i] = (TextView) findViewById(textIds[i]);
        }
        //Create a Handler object to receive and process messages
        mHandler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                //Determine whether the message is the specified message
                if (msg.what == 0x11){
                    //Cycle the background color of TextView
                    for (int i = 0;i<views.length;i++){
                        views[i].setBackgroundColor(colors[(i+currentColor)%colors.length]);
                    }
                    currentColor = (currentColor+1)%colors.length;
                }
            }
        };
        Timer timer = new Timer();      //Create timer object
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                mHandler.sendEmptyMessage(0x11);
            }
        },0,3000);      //Send message every 3000ms
    }
}

The above is all the code. If you find any problems, please correct them.

Tags: Android Java xml encoding

Posted on Mon, 04 May 2020 08:01:18 -0700 by cscheuble