Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 制作一个循环使用所有可能的RGBA颜色组合的应用程序_Java_Android_Colors_Rgba - Fatal编程技术网

Java 制作一个循环使用所有可能的RGBA颜色组合的应用程序

Java 制作一个循环使用所有可能的RGBA颜色组合的应用程序,java,android,colors,rgba,Java,Android,Colors,Rgba,我想知道如何使它,当你点击一个按钮时,它会启动一个循环,使用一个随机数生成器循环所有可能的RGBA颜色组合。我试图通过创建一个对象类来实现这一点,该对象类创建所有可能的组合,并将它们放入ArrayList,然后ArrayList随机选择一个可能的RGBA组合 这是我的main活动的代码 package com.example.safteyprecautions; import androidx.appcompat.app.AppCompatActivity; import android.g

我想知道如何使它,当你点击一个按钮时,它会启动一个循环,使用一个随机数生成器循环所有可能的RGBA颜色组合。我试图通过创建一个对象类来实现这一点,该对象类创建所有可能的组合,并将它们放入ArrayList,然后ArrayList随机选择一个可能的RGBA组合

这是我的main活动的代码

package com.example.safteyprecautions;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import java.util.Random;

public class MainActivity extends AppCompatActivity
{
    private Random rand1;
    private Random rand2;
    private Random rand3;
    private Random rand4;
    private int r;
    private int g;
    private int b;
    private int a;
    private RGBASelector rgba;

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

        Button pressMe = findViewById(R.id.pressMe);
        final LinearLayout layout = findViewById(R.id.LinearLayout);
//        rand1 = new Random(255);
//        rand2 = new Random(255);
//        rand3 = new Random(255);
//        rand4 = new Random(100);
        rgba = new RGBASelector(255, 255, 255, 100);
        pressMe.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {

                r = rgba.getR();
//                g = rgba.getG();
//                b = rgba.getB();
//                a = rgba.getA();
                layout.setBackgroundColor(Color.argb(100, r, 0, 0));
                Toast toast = Toast.makeText(getApplicationContext(), "Let the fun begin!", Toast.LENGTH_SHORT);
                toast.show();
            }
        });
    }
}
我的对象类

package com.example.safteyprecautions;

import java.util.ArrayList;
import java.util.Random;

public class RGBASelector
{
    private int r;
    private int g;
    private int b;
    private int a;
    private int[] RGBAValues;
    private ArrayList <Integer> RGBA;
    private Random rand;

    public RGBASelector(int rRange, int gRange, int bRange, int aRange)
    {
        r = rRange;
        g = gRange;
        b = bRange;
        a = aRange;
        rand = new Random(255);
        RGBAValues = new int[rRange];
        RGBA = new ArrayList <>();
        for (int i = 0; i < r; i++)
        {
            RGBAValues[i] = i;
            RGBA.add(RGBAValues[i]);
        }
    }

//    public void createRGBAValues()
//    {
//        for (int i = 0; i < r; i++)
//        {
//            RGBAValues[i] = i;
//        }
//    }

    public int getR()
    {
        r = rand.nextInt();
        return RGBA.get(r);
    }

    public int getG()
    {
        return g;
    }

    public int getB()
    {
        return b;
    }

    public int getA()
    {
        return a;
    }
}
package com.example.safteyprecautions;
导入java.util.ArrayList;
导入java.util.Random;
公共类RGBASelector
{
私人INTR;
私人INTG;
私人INTB;
私人INTA;
私有int[]rgbavalue;
私人ArrayList RGBA;
私有随机兰德;
公共RGBASelector(int RANGE、int gRange、int bRange、int aRange)
{
r=安排;
g=田庄;
b=布兰奇;
a=阿兰奇;
兰德=新随机数(255);
RGBAValues=新整数[rRange];
RGBA=newarraylist();
对于(int i=0;i
和我的活动_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/welcome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome to "
        android:fontFamily="cursive" />

    <Button
        android:id="@+id/pressMe"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="PRESS ME!!!"
        android:fontFamily="cursive" />

</LinearLayout>

您可以使用处理程序创建事件循环,在其中重新调用方法。您可以检查按钮的状态,然后选择是否更新。我的在科特林,但离这里很近。创建了概念的快速证明。对于我的布局,它只是一个带有普通视图的约束布局,占据了所有内容和按钮

class MainActivity : AppCompatActivity() {
var pressed = false
val handler = Handler()
lateinit var task: Runnable

val colors = arrayOf(Color.RED, Color.BLACK,Color.BLUE)

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    val bg: View = findViewById(R.id.background)
    val button: Button = findViewById(R.id.doThings)

    button.setOnClickListener {
        pressed = !pressed
        if (pressed)
            handler.post(task)
    }

        task= Runnable {
            if (pressed){
                bg.setBackgroundColor(colors[Random.nextInt(colors.size)])

                handler.post(task)
            }
        }
    }
}
创建一个任务,如果激活,它将执行工作,并重新调用它自己

在按钮处理程序中。设置按钮的状态,如果处于活动状态,则调用任务。
如果颜色太快,可以添加延迟

您希望将颜色存储在数组中,而不是动态生成颜色,这有什么原因吗?有几种方法可以在没有43亿个值的数组的情况下生成随机颜色。或者在有限的alpha范围内使用有限的17亿,即6.7GB大小!