【Android】使用FrameLayout布局實現霓虹燈效果

來源:互聯網
上載者:User


特性實現一個簡單的霓虹燈效果。

Activity代碼

package com.app.test01;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.view.View;public class FrameLayoutActivity extends Activity implements Runnable{//定義5個顏色值private int[] colors = new int[]{0xFFFF0000,0xFF0000FF,0xFF00FFFF,0xFFFF00FF,0xFF00FF00};//每一個顏色值的索引private int[] nextColorPoints = new int[]{1,2,3,4,0};//當前值的索引private int currentColorPoint = 0;private View[] views;private Handler handler;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_frame);views = new View[]{findViewById(R.id.textView5),findViewById(R.id.textView4),findViewById(R.id.textView3),findViewById(R.id.textView2),findViewById(R.id.textView1)};handler = new Handler();handler.postDelayed(this, 300);}@Overridepublic void run() {// TODO Auto-generated method stubint nextColorPoint = currentColorPoint;for (int i = views.length-1; i>=0; i--) {views[i].setBackgroundColor(colors[nextColorPoint]);nextColorPoint = nextColorPoints[nextColorPoint];}currentColorPoint++;if (currentColorPoint == 5) {currentColorPoint = 0;}handler.postDelayed(this, 300);}}

布局檔案

<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <TextView        android:id="@+id/textView1"        android:layout_width="300dp"        android:layout_height="300dp"        android:text="TextView"         android:layout_gravity="center"/>    <TextView        android:id="@+id/textView2"        android:layout_width="240dp"        android:layout_height="240dp"        android:text="TextView"         android:layout_gravity="center"/>        <TextView        android:id="@+id/textView3"        android:layout_width="180dp"        android:layout_height="180dp"        android:text="TextView"         android:layout_gravity="center"/>        <TextView        android:id="@+id/textView4"        android:layout_width="120dp"        android:layout_height="120dp"        android:text="TextView"         android:layout_gravity="center"/>        <TextView        android:id="@+id/textView5"        android:layout_width="60dp"        android:layout_height="60dp"        android:text="TextView"         android:layout_gravity="center"/>    </FrameLayout>


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.