android开发分享Android帧式布局实现自动切换颜色

本文实例为大家分享了android帧式布局实现自动切换颜色的具体代码,供大家参考,具体内容如下效果:实现:activity_main.xml<?xml version=”1.0″ encodin

android开发分享Android帧式布局实现自动切换颜色实例为大家分享了android帧式布局实现自动切换颜色的具体代码,供大家参考,具体内容如下

上述就是android开发分享Android帧式布局实现自动切换颜色的全部内容,如果对大家有所用处且需要了解更多关于Android学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

效果:

Android帧式布局实现自动切换颜色

实现:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>  <linearlayout xmlns:android="https://schemas.android.com/apk/res/android"      xmlns:tools="https://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);          //利用布局资源设置用户界面          setcontentview(r.layout.activity_main);          //通过资源标识符获取控件实例          tvbottom = findviewbyid(r.id.tvbottom);          tvmiddle = findviewbyid(r.id.tvmiddle);          tvtop = findviewbyid(r.id.tvtop);          //初始化颜色数组          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;                      // 根据切换后的颜色数组来设置三层标签的背景色                      tvbottom.setbackgroundcolor(colors[0]);                      tvmiddle.setbackgroundcolor(colors[1]);                      tvtop.setbackgroundcolor(colors[2]);                  }              }          };        }        /**       * 【开始】按钮单击事件处理方法       */      public void dostart(view view) {          // 设置线程运行控制变量          isrunning = true;          // 创建子线程,定时发送消息          thread = new thread(new runnable() {              @override              public void run() {                  while (isrunning) {                      // 向主线程发送消息                      handler.sendemptymessage(0x0001);                      // 让线程睡眠500毫秒                      try {                          thread.sleep(500);                      } catch (interruptedexception e) {                          e.printstacktrace();                      }                  }              }          });          // 启动线程          thread.start();      }        /**       * 【停止】按钮单击事件处理方法       */      public void dostop(view view) {          // 设置线程运行控制变量          isrunning = false;          // 销毁子线程          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">开始</string>      <string name="stop">结束</string>  </resources>

原本想用timer定时器实现,但是不知怎么的总是报错,所有就使用了这个旧方法。

以上就是android开发分享Android帧式布局实现自动切换颜色的全部内容,希望对大家的学习有所帮助,也希望大家多多支持<计算机技术网(www.ctvol.com)!!>。

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/addevelopment/1081550.html

(0)
上一篇 2022年4月24日
下一篇 2022年4月24日

精彩推荐