Generatore di colore casuale sul telefono shake

voti
0

Ho fatto un app che mostra colore casuale sulla scossa. Ora ho cercato di renderlo un po 'più di un gioco. Ma ora sono in grado di gestire i pulsanti. La mia idea principale è, in primo luogo la necessità di scegliere il pulsante poi dopo scossa se la corrispondenza dei colori poi c'è vincere altrimenti, sciolto Qui potete trovare il mio codice. Qualcuno mi può aiutare con questo? Ecco come il mio aspetto app

Ecco la mia .XML di seguito:

<?xml version=1.0 encoding=utf-8?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=http://schemas.android.com/apk/res/android
    xmlns:app=http://schemas.android.com/apk/res-auto
    xmlns:tools=http://schemas.android.com/tools
    android:layout_width=match_parent
    android:layout_height=match_parent>

    <Button
        android:id=@+id/t21
        android:layout_width=0dp
        android:layout_height=84dp
        android:layout_marginStart=32dp
        android:layout_marginLeft=32dp
        android:layout_marginEnd=32dp
        android:layout_marginRight=32dp
        android:layout_marginBottom=16dp
        android:background=#59C4C5
        android:text=Win or Loose
        app:layout_constraintBottom_toTopOf=@+id/tableLayout
        app:layout_constraintEnd_toEndOf=parent
        app:layout_constraintHorizontal_bias=1.0
        app:layout_constraintStart_toStartOf=parent
        app:layout_constraintTop_toTopOf=parent
        app:layout_constraintVertical_bias=1.0 />

    <TableLayout
        android:id=@+id/tableLayout
        android:layout_width=354dp
        android:layout_height=237dp
        android:layout_marginStart=32dp
        android:layout_marginLeft=32dp
        android:layout_marginEnd=32dp
        android:layout_marginRight=32dp
        android:layout_marginBottom=16dp
        app:layout_constraintBottom_toTopOf=@+id/t
        app:layout_constraintEnd_toEndOf=parent
        app:layout_constraintStart_toStartOf=parent>

        <TableRow
            android:layout_width=match_parent
            android:layout_height=match_parent>

            <Button
                android:id=@+id/t1
                android:layout_width=wrap_content
                android:layout_height=wrap_content
                android:background=#fed766
                android:minWidth=88dp
                android:text= />

            <Button
                android:id=@+id/t2
                android:layout_width=wrap_content
                android:layout_height=wrap_content
                android:minWidth=88dp
                android:text=
                android:background=#eec9d2

                />

            <Button
                android:id=@+id/t3
                android:layout_width=wrap_content
                android:layout_height=wrap_content
                android:minWidth=88dp
                android:text=
                android:background=#f07d38

                />

            <Button
                android:id=@+id/t4
                android:layout_width=wrap_content
                android:layout_height=wrap_content
                android:minWidth=88dp
                android:text=
                android:background=#c7243a
               />
        </TableRow>

        <TableRow
            android:layout_width=match_parent
            android:layout_height=match_parent>

            <Button
                android:id=@+id/t5
                android:layout_width=wrap_content
                android:layout_height=wrap_content
                android:text=
                android:background=#f485c7
               />

            <Button
                android:id=@+id/t6
                android:layout_width=wrap_content
                android:layout_height=wrap_content
                android:background=#ff1f5d4f

                android:text= />

            <Button
                android:id=@+id/t7
                android:layout_width=wrap_content
                android:layout_height=wrap_content
                android:background=#c6cc56a9
                android:text= />

            <Button
                android:id=@+id/t8
                android:layout_width=wrap_content
                android:layout_height=wrap_content
                android:background=#f3227588
                android:text= />
        </TableRow>

        <TableRow
            android:layout_width=match_parent
            android:layout_height=match_parent>

            <Button
                android:id=@+id/t9
                android:layout_width=wrap_content
                android:layout_height=wrap_content
                android:background=#f2684ab4
                android:text= />

            <Button
                android:id=@+id/t10
                android:layout_width=wrap_content
                android:layout_height=wrap_content
                android:background=#d2e7ff
                android:text= />

            <Button
                android:id=@+id/t11
                android:layout_width=wrap_content
                android:layout_height=wrap_content
                android:background=#ff6f69
                android:text= />

            <Button
                android:id=@+id/t12
                android:layout_width=wrap_content
                android:layout_height=wrap_content
                android:background=#bed905
                android:text= />
        </TableRow>

        <TableRow
            android:layout_width=match_parent
            android:layout_height=match_parent>

            <Button
                android:id=@+id/t13
                android:layout_width=wrap_content
                android:layout_height=wrap_content
                android:background=#e43afee1
                android:text= />

            <Button
                android:id=@+id/t14
                android:layout_width=wrap_content
                android:layout_height=wrap_content
                android:background=#bf30b02f
                android:text= />

            <Button
                android:id=@+id/t15
                android:layout_width=wrap_content
                android:layout_height=wrap_content
                android:background=#a8e6cf
                android:text= />

            <Button
                android:id=@+id/t16
                android:layout_width=wrap_content
                android:layout_height=wrap_content
                android:background=#d2d4dc
                android:text= />

        </TableRow>

        <TableRow
            android:layout_width=match_parent
            android:layout_height=match_parent>

            <Button
                android:id=@+id/t17
                android:layout_width=wrap_content
                android:layout_height=wrap_content
                android:background=#ea9ed0a6
                android:text= />

            <Button
                android:id=@+id/t18
                android:layout_width=wrap_content
                android:layout_height=wrap_content
                android:background=#d9ad0495
                android:text= />

            <Button
                android:id=@+id/t19
                android:layout_width=wrap_content
                android:layout_height=wrap_content
                android:background=#fff4e6
                android:text= />

            <Button
                android:id=@+id/t20
                android:layout_width=wrap_content
                android:layout_height=wrap_content
                android:background=#be9b7b
                android:text= />
        </TableRow>

    </TableLayout>

    <Button
        android:id=@+id/t
        android:layout_width=0dp
        android:layout_height=84dp
        android:layout_marginStart=32dp
        android:layout_marginLeft=32dp
        android:layout_marginEnd=32dp
        android:layout_marginRight=32dp
        android:text=Choose one of the above color
        android:background=#8b9dc3
        app:layout_constraintBottom_toBottomOf=parent
        app:layout_constraintEnd_toEndOf=parent
        app:layout_constraintStart_toStartOf=parent />

</androidx.constraintlayout.widget.ConstraintLayout>

Ecco il mio java di seguito:

package com.example.android.myapplication;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.squareup.seismic.ShakeDetector;
import java.util.Random;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity implements ShakeDetector.Listener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        ShakeDetector shakeDetector = new ShakeDetector(this);
        shakeDetector.start(sensorManager);


    }

    @Override
    public void hearShake() {
        Random rnd = new Random();
        final TextView textView = findViewById(R.id.t);

        int color = Color.argb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
        textView.setText(Random color value is +Integer.toHexString(color)+\n and you choose +color);
        findViewById(android.R.id.content).setBackgroundColor(color);

    }
}

E qui è un'implementazione che ho make in Gradle Script -> Build.Gradle (Module.app) ->

dependencies { 
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.squareup:seismic:1.0.2'  ///only this line is added. 
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

È pubblicato 03/12/2019 alle 00:05
fonte dall'utente
In altre lingue...                            


1 risposte

voti
0

Si dovrebbe avere un insieme fisso di colori tra cui scegliere in modo casuale da, altrimenti sarebbe quasi impossibile indovinare il colore giusto. Così, per esempio, riempire una matrice con i colori come fatto nel post qui

Quindi creare un pulsante per ogni colore dell'array. negozio anche il colore scelto in una variabile in modo da poter verificare se una persona indovinato il colore giusto dopo la scossa.

Risposto il 03/12/2019 a 00:15
fonte dall'utente

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more