最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android幀式布局實(shí)現(xiàn)自動(dòng)切換顏色

 更新時(shí)間:2022年04月24日 08:21:00   作者:doubleview  
這篇文章主要為大家詳細(xì)介紹了Android幀式布局實(shí)現(xiàn)自動(dòng)切換顏色,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android幀式布局實(shí)現(xiàn)自動(dòng)切換顏色的具體代碼,供大家參考,具體內(nèi)容如下

效果:

實(shí)現(xiàn):

activity_main.xml

<?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="match_parent"
? ? android:layout_height="match_parent"
? ? android:gravity="center"
? ? android:orientation="vertical"
? ? tools:context=".MainActivity">

? ? <FrameLayout
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content">

? ? ? ? <TextView
? ? ? ? ? ? android:id="@+id/tvBottom"
? ? ? ? ? ? android:layout_width="300dp"
? ? ? ? ? ? android:layout_height="300dp"
? ? ? ? ? ? android:layout_gravity="center"
? ? ? ? ? ? android:background="#ff0000"
? ? ? ? ? ? android:text="@string/bottom"
? ? ? ? ? ? android:textColor="#ffff00"
? ? ? ? ? ? android:textSize="30sp" />

? ? ? ? <TextView
? ? ? ? ? ? android:id="@+id/tvMiddle"
? ? ? ? ? ? android:layout_width="200dp"
? ? ? ? ? ? android:layout_height="200dp"
? ? ? ? ? ? android:layout_gravity="center"
? ? ? ? ? ? android:background="#0000ff"
? ? ? ? ? ? android:text="@string/middle"
? ? ? ? ? ? android:textColor="#ffff00"
? ? ? ? ? ? android:textSize="30sp" />

? ? ? ? <TextView
? ? ? ? ? ? android:id="@+id/tvTop"
? ? ? ? ? ? android:layout_width="100dp"
? ? ? ? ? ? android:layout_height="100dp"
? ? ? ? ? ? android:layout_gravity="center"
? ? ? ? ? ? android:background="#00ff00"
? ? ? ? ? ? android:text="@string/top"
? ? ? ? ? ? android:textColor="#ffff00"
? ? ? ? ? ? android:textSize="30sp" />


? ? </FrameLayout>

? ? <LinearLayout
? ? ? ? android:layout_marginTop="20dp"
? ? ? ? android:layout_width="300dp"
? ? ? ? android:layout_height="50dp"
? ? ? ? android:gravity="center">
? ? ? ? <Button
? ? ? ? ? ? android:layout_width="100dp"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:text="@string/start"
? ? ? ? ? ? android:textSize="20sp"
? ? ? ? ? ? android:onClick="doStart"
? ? ? ? ? ? android:layout_marginRight="50dp"
? ? ? ? ? ? android:background="#04b102"/>

? ? ? ? <Button
? ? ? ? ? ? android:layout_width="100dp"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:text="@string/stop"
? ? ? ? ? ? android:textSize="20sp"
? ? ? ? ? ? android:onClick="doStop"
? ? ? ? ? ? android:background="#04b102"/>
? ? </LinearLayout>
</LinearLayout>

ActivityMain.java

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
? ? private TextView tvBottom;
? ? private TextView tvMiddle;
? ? private TextView tvTop;
? ? private int[] colors;
? ? private Handler handler;
? ? private Thread thread;
? ? private boolean isRunning;

? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? //利用布局資源設(shè)置用戶(hù)界面
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? //通過(guò)資源標(biāo)識(shí)符獲取控件實(shí)例
? ? ? ? tvBottom = findViewById(R.id.tvBottom);
? ? ? ? tvMiddle = findViewById(R.id.tvMiddle);
? ? ? ? tvTop = findViewById(R.id.tvTop);
? ? ? ? //初始化顏色數(shù)組
? ? ? ? colors = new int[]{Color.RED, Color.BLUE, Color.GREEN};

? ? ? ? handler = new Handler() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void handleMessage(@NonNull Message msg) {
? ? ? ? ? ? ? ? super.handleMessage(msg);
? ? ? ? ? ? ? ? if (msg.what == 0x0001) {
? ? ? ? ? ? ? ? ? ? //切換顏色
? ? ? ? ? ? ? ? ? ? int temp = colors[0];
? ? ? ? ? ? ? ? ? ? for (int i = 0; i < colors.length - 1; i++) {
? ? ? ? ? ? ? ? ? ? ? ? colors[i] = colors[i + 1];
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? colors[colors.length - 1] = temp;
? ? ? ? ? ? ? ? ? ? // 根據(jù)切換后的顏色數(shù)組來(lái)設(shè)置三層標(biāo)簽的背景色
? ? ? ? ? ? ? ? ? ? tvBottom.setBackgroundColor(colors[0]);
? ? ? ? ? ? ? ? ? ? tvMiddle.setBackgroundColor(colors[1]);
? ? ? ? ? ? ? ? ? ? tvTop.setBackgroundColor(colors[2]);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? };

? ? }

? ? /**
? ? ?* 【開(kāi)始】按鈕單擊事件處理方法
? ? ?*/
? ? public void doStart(View view) {
? ? ? ? // 設(shè)置線程運(yùn)行控制變量
? ? ? ? isRunning = true;
? ? ? ? // 創(chuàng)建子線程,定時(shí)發(fā)送消息
? ? ? ? thread = new Thread(new Runnable() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void run() {
? ? ? ? ? ? ? ? while (isRunning) {
? ? ? ? ? ? ? ? ? ? // 向主線程發(fā)送消息
? ? ? ? ? ? ? ? ? ? handler.sendEmptyMessage(0x0001);
? ? ? ? ? ? ? ? ? ? // 讓線程睡眠500毫秒
? ? ? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? ? ? Thread.sleep(500);
? ? ? ? ? ? ? ? ? ? } catch (InterruptedException e) {
? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? // 啟動(dòng)線程
? ? ? ? thread.start();
? ? }

? ? /**
? ? ?* 【停止】按鈕單擊事件處理方法
? ? ?*/
? ? public void doStop(View view) {
? ? ? ? // 設(shè)置線程運(yùn)行控制變量
? ? ? ? isRunning = false;
? ? ? ? // 銷(xiāo)毀子線程
? ? ? ? thread = null;
? ? }
}

string.xml

<resources>
? ? <string name="app_name">幀式布局:顏色切換</string>
? ? <string name="bottom">底層</string>
? ? <string name="middle">中層</string>
? ? <string name="top">頂層</string>
? ? <string name="start">開(kāi)始</string>
? ? <string name="stop">結(jié)束</string>
</resources>

原本想用Timer定時(shí)器實(shí)現(xiàn),但是不知怎么的總是報(bào)錯(cuò),所有就使用了這個(gè)舊方法。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android編程之退出整個(gè)應(yīng)用程序的方法

    Android編程之退出整個(gè)應(yīng)用程序的方法

    這篇文章主要介紹了Android編程之退出整個(gè)應(yīng)用程序的方法,實(shí)例分析了Android直接關(guān)閉所有的Acitivity并退出應(yīng)用程序的實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2015-12-12
  • android實(shí)現(xiàn)清理緩存功能

    android實(shí)現(xiàn)清理緩存功能

    這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)清理緩存,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • Android自定義實(shí)現(xiàn)轉(zhuǎn)盤(pán)菜單

    Android自定義實(shí)現(xiàn)轉(zhuǎn)盤(pán)菜單

    旋轉(zhuǎn)菜單是一種占用空間較大,實(shí)用性稍弱的UI,本文主要為大家詳細(xì)介紹了Android如何自定義實(shí)現(xiàn)轉(zhuǎn)盤(pán)菜單,文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考下
    2023-12-12
  • Android直播系統(tǒng)平臺(tái)搭建之圖片實(shí)現(xiàn)陰影效果的方法小結(jié)

    Android直播系統(tǒng)平臺(tái)搭建之圖片實(shí)現(xiàn)陰影效果的方法小結(jié)

    這篇文章主要介紹了Android直播系統(tǒng)平臺(tái)搭建, 圖片實(shí)現(xiàn)陰影效果的若干種方法,本文給大家?guī)?lái)三種方法,每種方法通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2021-08-08
  • Android字符串轉(zhuǎn)Ascii碼實(shí)例代碼

    Android字符串轉(zhuǎn)Ascii碼實(shí)例代碼

    這篇文章主要介紹了Android字符串轉(zhuǎn)Ascii碼的方法,大家參考使用
    2013-11-11
  • 最新評(píng)論

    砚山县| 阿克| 华安县| 峨山| 三江| 图木舒克市| 准格尔旗| 雷州市| 曲阜市| 舞钢市| 建昌县| 乌鲁木齐市| 香格里拉县| 余干县| 昂仁县| 瑞丽市| 无为县| 吉隆县| 包头市| 揭西县| 苍溪县| 岳西县| 汉源县| 祥云县| 洛浦县| 崇礼县| 通化县| 常熟市| 高密市| 合川市| 廉江市| 东源县| 金寨县| 抚远县| 清水河县| 达尔| 肇源县| 海兴县| 石渠县| 罗田县| 本溪市|