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



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

这是我MainActivity的代码

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;
}
}

以及我的activity_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>

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

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)
}
}
}
}

创建一个任务,如果它处于活动状态,它将完成工作,并重新调用它自己。

在按钮处理程序中。设置按钮的状态,如果激活,则调用任务。如果颜色过快,可能会增加延迟。

相关内容

最新更新