簡體   English   中英

一組顏色每4秒隨機更改顏色-Android應用

[英]Random Color change every 4 seconds from a set of colors - Android App

我正在嘗試制作一個使用Eclipse在Java中每4秒從4種顏色中選擇一種隨機顏色的應用程序。 我唯一真正的編程經驗是在Flash中使用動作腳本,但在獲得所需的結果時遇到了一些麻煩。 我遵循了一些基礎知識的教程,並且對編碼有足夠的了解,可以對其進行足夠的更改以使其接近我想要的內容,但目前,我只能使它以到目前為止的方式來進行循環。

MainActivity.java

package com.example.androidcountdowntimer;

import com.example.androidcountdowntimer.SpotView.SpotCallBack;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity 
  implements SpotCallBack{

 TextView myState, myCounter;
 Button btnStart;

 SpotView mySpotView;

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

  mySpotView = (SpotView)findViewById(R.id.myspotview);
  mySpotView.setCallback(this);

  myState = (TextView) findViewById(R.id.mystate);

  myCounter = (TextView) findViewById(R.id.mycounter);
  btnStart = (Button) findViewById(R.id.start);
  btnStart.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    mySpotView.startRun();
   }
  });

 }

 @Override
 public void cb_onTick(String msg) {
  myCounter.setText("Milliseconds: " + msg);
 }

 @Override
 public void cb_onFinish(String msg) {
  myState.setText(msg);
 }

}

SpotView.java

package com.example.androidcountdowntimer;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.CountDownTimer;
import android.util.AttributeSet;
import android.view.View;

public class SpotView extends View{

 //used to pass back something to MainActivity
 interface SpotCallBack {
  void cb_onTick(String msg);
  void cb_onFinish(String msg); 
 }

 SpotCallBack spotCallback;

 CountDownTimer countDownTimer;

 int state;
 final static int STATE_IDLE = 0;
 final static int STATE_RED = 1;
 final static int STATE_GREEN = 2;
 final static int STATE_BLUE = 3;
 final static int STATE_YELLOW = 4;

 Paint paintRed, paintGreen, paintBlue, paintYellow;

 public SpotView(Context context) {
  super(context);
  init();
 }

 public SpotView(Context context, AttributeSet attrs) {
  super(context, attrs);
  init();
 }

 public SpotView(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  init();
 }

 private void init(){
  state = STATE_IDLE;

  paintRed = new Paint();
  paintRed.setColor(Color.RED);
  paintRed.setStrokeWidth(1);
  paintRed.setStyle(Paint.Style.FILL);

  paintGreen = new Paint();
  paintGreen.setColor(Color.GREEN);
  paintGreen.setStrokeWidth(1);
  paintGreen.setStyle(Paint.Style.FILL);

  paintBlue = new Paint();
  paintBlue.setColor(Color.BLUE);
  paintBlue.setStrokeWidth(1);
  paintBlue.setStyle(Paint.Style.FILL);

  paintYellow = new Paint();
  paintYellow.setColor(Color.YELLOW);
  paintYellow.setStrokeWidth(1);
  paintYellow.setStyle(Paint.Style.FILL);
 }

 public void setCallback(SpotCallBack cb){
  spotCallback = cb;
 }

 public void startRun(){
  state = STATE_RED;

  if(countDownTimer!=null){
   countDownTimer.cancel();
  }

  countDownTimer = new CountDownTimer(4000, 500) {

   @Override
   public void onTick(long millisUntilFinished) {
    spotCallback.cb_onTick(String.valueOf(millisUntilFinished));
   }

   @Override
   public void onFinish() {
    if(state == STATE_RED){
     state = STATE_GREEN;
     spotCallback.cb_onFinish("GREEN");
    }else if(state == STATE_GREEN){
     state = STATE_BLUE;
     spotCallback.cb_onFinish("BLUE");
    }else if(state == STATE_BLUE){
     state = STATE_YELLOW;
     spotCallback.cb_onFinish("YELLOW");
    }else if(state == STATE_YELLOW){
        state = STATE_RED;
        spotCallback.cb_onFinish("RED");
    }
    countDownTimer.start();
    invalidate();
   }
  };

  countDownTimer.start();
  spotCallback.cb_onFinish("RED");
  invalidate();
 }

 @Override
 protected void onDraw(Canvas canvas) {

  float w = getWidth();
  float h = getHeight();

  if(state == STATE_RED){
   canvas.drawCircle(w/2, h/2, 400, paintRed);
  }else if(state == STATE_GREEN){
   canvas.drawCircle(w/2, h/2, 400, paintGreen);
  }else if(state == STATE_BLUE){
   canvas.drawCircle(w/2, h/2, 400, paintBlue);
  }else if(state == STATE_YELLOW){
      canvas.drawCircle(w/2, h/2, 400, paintYellow);
  }
 }

}

activity_main.xml

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.androidcountdowntimer.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="Tynes Timer Thing"
        android:textStyle="bold"
        android:textSize="24sp" />

    <Button
        android:id="@+id/start"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Start CountDownTimer" />

    <TextView
        android:id="@+id/mystate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="24sp" />

    <TextView
        android:id="@+id/mycounter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="24sp" />

    <com.example.androidcountdowntimer.SpotView 
        android:id="@+id/myspotview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

因此,他們嘗試隨機改變顏色的方法是將用於狀態的顏色存儲為變量,然后嘗試使用該變量使當前僅通過一組狀態運行的部分代碼動態化喜歡:

String[] colours = {"STATE_RED", "STATE_GREEN", "STATE_BLUE", "STATE_YELLOW"};
int idx = new Random().nextInt(colours.length);
String random = (colors[idx]);

並以此嘗試在elseif中設置狀態

state = random;

但是我只是不斷在代碼中得到紅色的X和錯誤,無法弄清楚如何使狀態在Java中隨機變化。

任何幫助和/或建議將不勝感激。

謝謝,利亞姆

您正在嘗試將String值分配給int變量:

state = random;

而是獲取隨機的int值,並在您的代碼中使用此值:

int random = new Random().nextInt(STATE_YELLOW) + 1;
state = random;

對於更清潔的代碼,請定義顏色的最小值和最大值常量,並使用它們:

private static final int MIN_STATE_COLOR = STATE_RED;
private static final int MAX_STATE_COLOR = STATE_YELLOW;

int random = new Random().nextInt(MAX_STATE_COLOR - MIN_STATE_COLOR + 1) + MIN_STATE_COLOR;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM