How can I compare the drawables?

0

I like to compare the drawable of an ImageView and a Button, I'm doing a kind of lottery but I do not know how to send the message that I complete the table, the imageview places them in a viewfliper, I hope you can help me: D

Code xml:

<ViewFlipper
    android:id="@+id/vfFotos"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <ImageView

        android:id="@+id/apple"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/apple"
         />
    <ImageView
        android:id="@+id/banna"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/banna"
        />
    <ImageView
        android:id="@+id/bird"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/bird"
        />
    <ImageView
        android:id="@+id/black"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/black"
        />
    <ImageView
        android:id="@+id/blue"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/blue"
        />
    <ImageView
        android:id="@+id/brown"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/brown"
        />
    <ImageView
        android:id="@+id/cat"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/cat"
        />
    <ImageView
        android:id="@+id/chicken"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/chicken"
        />
    <ImageView
        android:id="@+id/cow"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/cow"
        />
    <ImageView
        android:id="@+id/dog"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/dog"
        />
    <ImageView
        android:id="@+id/eight"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/eight"
        />
    <ImageView
        android:id="@+id/five"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/five"
        />
    <ImageView
        android:id="@+id/four"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/four"
        />
    <ImageView
        android:id="@+id/giraffe"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/giraffe"
        />
    <ImageView
        android:id="@+id/gray"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/gray"
        />
    <ImageView
        android:id="@+id/green"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/green"
        />
    <ImageView
        android:id="@+id/horse"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/horse"
        />
    <ImageView
        android:id="@+id/lion"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/lion"
        />
    <ImageView
        android:id="@+id/mango"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/mango"
        />
    <ImageView
        android:id="@+id/melon"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/melon"
        />
    <ImageView
        android:id="@+id/nine"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/nine"
        />
    <ImageView
        android:id="@+id/one"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/one"
        />
    <ImageView
        android:id="@+id/orange"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/orange"
        />
    <ImageView
        android:id="@+id/orangecolor"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/orangecolor"
        />
    <ImageView
        android:id="@+id/pawpaw"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/pawpaw"
        />
    <ImageView
        android:id="@+id/pear"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/pear"
        />
    <ImageView
        android:id="@+id/pig"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/pig"
        />
    <ImageView
        android:id="@+id/pineapple"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/pineapple"
        />
    <ImageView
        android:id="@+id/pink"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/pink"
        />
    <ImageView
        android:id="@+id/purple"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/purple"
        />
    <ImageView
        android:id="@+id/rabbit"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/rabbit"
        />
    <ImageView
        android:id="@+id/red"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/red"
        />
    <ImageView
        android:id="@+id/seven"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/seven"
        />
    <ImageView
        android:id="@+id/six"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/six"
        />
    <ImageView
        android:id="@+id/strawberry"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/strawberry"
        />
    <ImageView
        android:id="@+id/three"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/three"
        />
    <ImageView
        android:id="@+id/two"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/two"
        />

    <ImageView
        android:id="@+id/watermelon"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/watermelon"
        />
    <ImageView
        android:id="@+id/yellow"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/yellow"
        />
    <ImageView
        android:id="@+id/zero"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/zero"
        />

</ViewFlipper>

    <Button
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:id="@+id/Btnpear"
        app:layout_constraintTop_toBottomOf="@id/vfFotos"
        android:background="@drawable/pear"
        />
<Button
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:id="@+id/Btnpink"
    app:layout_constraintTop_toBottomOf="@id/vfFotos"
    app:layout_constraintLeft_toRightOf="@id/Btnpear"
    android:background="@drawable/pink"
    />
<Button
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:id="@+id/Btnbanna"
    app:layout_constraintTop_toBottomOf="@id/vfFotos"
    app:layout_constraintLeft_toRightOf="@id/Btnpink"
    android:background="@drawable/banna"
    />
<Button
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:id="@+id/Btnmango"
    app:layout_constraintTop_toBottomOf="@id/Btnpear"
    android:background="@drawable/mango"
    />
<Button
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:id="@+id/Btnsix"
    app:layout_constraintTop_toBottomOf="@id/Btnpink"
    app:layout_constraintLeft_toRightOf="@id/Btnmango"
    android:background="@drawable/six"
    />
<Button
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:id="@+id/Btnhorse"
    app:layout_constraintTop_toBottomOf="@id/Btnbanna"
    app:layout_constraintLeft_toRightOf="@id/Btnsix"
    android:background="@drawable/horse"
    />
<Button
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:id="@+id/Btnone"
    app:layout_constraintTop_toBottomOf="@id/Btnmango"
    android:background="@drawable/one"
    />
<Button
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:id="@+id/Btnrabbit"
    app:layout_constraintTop_toBottomOf="@id/Btnsix"
    app:layout_constraintLeft_toRightOf="@id/Btnone"
    android:background="@drawable/rabbit"
    />
<Button
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:id="@+id/Btnfive"
    app:layout_constraintTop_toBottomOf="@id/Btnhorse"
    app:layout_constraintLeft_toRightOf="@id/Btnrabbit"
    android:background="@drawable/five"
    />
<Button
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:id="@+id/Btnseven"
    app:layout_constraintTop_toBottomOf="@id/vfFotos"
    app:layout_constraintLeft_toRightOf="@id/Btnbanna"
    android:background="@drawable/seven"
    />
<Button
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:id="@+id/Btndog"
    app:layout_constraintTop_toBottomOf="@id/Btnseven"
    app:layout_constraintLeft_toRightOf="@id/Btnhorse"
    android:background="@drawable/dog"
    />
<Button
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:id="@+id/Btnpawpaw"
    app:layout_constraintTop_toBottomOf="@id/Btndog"
    app:layout_constraintLeft_toRightOf="@id/Btnfive"
    android:background="@drawable/pawpaw"
    />
<Button
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:id="@+id/Btnpurple"
    app:layout_constraintTop_toBottomOf="@id/vfFotos"
    app:layout_constraintLeft_toRightOf="@id/Btnseven"
    android:background="@drawable/purple"
    />
<Button
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:id="@+id/Btnzero"
    app:layout_constraintTop_toBottomOf="@id/Btnpurple"
    app:layout_constraintLeft_toRightOf="@id/Btndog"
    android:background="@drawable/zero"
    />
<Button
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:id="@+id/Btnwatermelon"
    app:layout_constraintTop_toBottomOf="@id/Btnzero"
    app:layout_constraintLeft_toRightOf="@id/Btnpawpaw"
    android:background="@drawable/watermelon"
    />

<Button
    android:id="@+id/jugar"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:text="Jugar"
    app:layout_constraintTop_toBottomOf="@id/Btnone"
    />
<Button
    android:id="@+id/salir"
    android:layout_width="190dp"
    android:layout_height="50dp"
    android:text="Salir"
    app:layout_constraintTop_toBottomOf="@id/Btnpawpaw"
    app:layout_constraintLeft_toRightOf="@id/jugar"

    />
<TextView
    android:id="@+id/IdMensage"
    android:layout_width="100dp"
    android:layout_height="100dp"
    app:layout_constraintTop_toBottomOf="@id/jugar"
    />

Java code:

package com.example.dante.quetzal;

        import android.graphics.Bitmap;
        import android.graphics.drawable.BitmapDrawable;
        import android.graphics.drawable.Drawable;
        import android.os.CountDownTimer;
        import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;
        import android.view.View;
        import android.widget.Button;
        import android.widget.ImageView;
        import android.widget.TextView;
        import android.widget.ViewFlipper;

        import java.util.Random;

public class tabla1 extends AppCompatActivity implements View.OnClickListener{

    ImageView pear,pink,banna,mango,six,horse,one,rabbit,five,seven,dog,pawpaw,purple,zero,watermelon;
    Button jugar,salir,btnpear,btnpink,btnbanna,btnmango,btnsix,btnhorse,btnone,btnrabbit,btnfive,btnseven,btndog,btnpawpaw,btnpurple,btnzero,btnwatermelon;
    TextView idMensaje;


    ViewFlipper vfFotos;

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

        pear = (ImageView)findViewById(R.id.pear);
        pink = (ImageView)findViewById(R.id.pink);
        banna = (ImageView)findViewById(R.id.banna);
        mango = (ImageView)findViewById(R.id.mango);
        six = (ImageView)findViewById(R.id.six);
        horse = (ImageView)findViewById(R.id.horse);
        one = (ImageView)findViewById(R.id.one);
        rabbit = (ImageView)findViewById(R.id.rabbit);
        five = (ImageView)findViewById(R.id.five);
        seven = (ImageView)findViewById(R.id.seven);
        dog = (ImageView)findViewById(R.id.dog);
        pawpaw = (ImageView)findViewById(R.id.pawpaw);
        purple = (ImageView)findViewById(R.id.purple);
        zero = (ImageView)findViewById(R.id.zero);
        watermelon = (ImageView)findViewById(R.id.watermelon);

        btnpear = (Button)findViewById(R.id.Btnpear);
        btnpink = (Button)findViewById(R.id.Btnpink);
        btnbanna = (Button)findViewById(R.id.Btnbanna);
        btnmango = (Button)findViewById(R.id.Btnmango);
        btnsix = (Button)findViewById(R.id.Btnsix);
        btnhorse = (Button)findViewById(R.id.Btnhorse);
        btnone = (Button)findViewById(R.id.Btnone);
        btnrabbit = (Button)findViewById(R.id.Btnrabbit);
        btnfive = (Button)findViewById(R.id.Btnfive);
        btnseven = (Button)findViewById(R.id.Btnseven);
        btndog = (Button)findViewById(R.id.Btndog);
        btnpawpaw = (Button)findViewById(R.id.Btnpawpaw);
        btnpurple = (Button)findViewById(R.id.Btnpurple);
        btnzero = (Button)findViewById(R.id.Btnzero);
        btnwatermelon = (Button)findViewById(R.id.Btnwatermelon);

        jugar = (Button)findViewById(R.id.jugar);
        salir = (Button) findViewById(R.id.salir);

        idMensaje = (TextView)findViewById(R.id.TextContador);

        btnpear.setOnClickListener(this);
        btnpink.setOnClickListener(this);
        btnbanna.setOnClickListener(this);
        btnmango.setOnClickListener(this);
        btnsix.setOnClickListener(this);
        btnhorse.setOnClickListener(this);
        btnone.setOnClickListener(this);
        btnrabbit.setOnClickListener(this);
        btnfive.setOnClickListener(this);
        btnseven.setOnClickListener(this);
        btndog.setOnClickListener(this);
        btnpawpaw.setOnClickListener(this);
        btnpurple.setOnClickListener(this);
        btnzero.setOnClickListener(this);
        btnwatermelon.setOnClickListener(this);
        jugar.setOnClickListener(this);
        salir.setOnClickListener(this);
        desactivar();





    }

    public void desactivar(){

        btnpear.setEnabled(false);
        btnpink.setEnabled(false);
        btnbanna.setEnabled(false);
        btnmango.setEnabled(false);
        btnsix.setEnabled(false);
        btnhorse.setEnabled(false);
        btnone.setEnabled(false);
        btnrabbit.setEnabled(false);
        btnfive.setEnabled(false);
        btnseven.setEnabled(false);
        btndog.setEnabled(false);
        btnpawpaw.setEnabled(false);
        btnpurple.setEnabled(false);
        btnzero.setEnabled(false);
        btnwatermelon.setEnabled(false);
    }

    public void activar(){
        btnpear.setEnabled(true);
        btnpink.setEnabled(true);
        btnbanna.setEnabled(true);
        btnmango.setEnabled(true);
        btnsix.setEnabled(true);
        btnhorse.setEnabled(true);
        btnone.setEnabled(true);
        btnrabbit.setEnabled(true);
        btnfive.setEnabled(true);
        btnseven.setEnabled(true);
        btndog.setEnabled(true);
        btnpawpaw.setEnabled(true);
        btnpurple.setEnabled(true);
        btnzero.setEnabled(true);
        btnwatermelon.setEnabled(true);

    }

public void cronometro(){
    new CountDownTimer(60000, 1000){


        @Override
        public void onTick(long millisUntilFinished) {
            jugar.setEnabled(false);
            idMensaje.setText(""+millisUntilFinished/1000);


                idMensaje.setText("Ganaste");
                onFinish();



        }

        @Override
        public void onFinish() {
            desactivar();
            jugar.setEnabled(true);

                idMensaje.setText("Perdiste");


        }
    }.start();
}

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.Btnpear:
                Bitmap bitmap = ((BitmapDrawable)pear).getBitmap();
                Bitmap bitmap1 = ((BitmapDrawable)btnpear).getBitmap();
                if(bitmap==bitmap1){

                }
                break;
            case R.id.pink:
                pink.setImageResource(R.drawable.pink);
                break;
            case R.id.banna:
                banna.setImageResource(R.drawable.banna);
                break;
            case R.id.mango:
                mango.setImageResource(R.drawable.mango);
                break;
            case R.id.six:
                six.setImageResource(R.drawable.six);

            case R.id.horse:
                horse.setImageResource(R.drawable.horse);
                break;
            case R.id.one:
                one.setImageResource(R.drawable.one);
                break;
            case R.id.rabbit:
                rabbit.setImageResource(R.drawable.rabbit);
                break;
            case R.id.five:
                five.setImageResource(R.drawable.five);
                break;
            case R.id.seven:
                seven.setImageResource(R.drawable.seven);
                break;
            case R.id.dog:
                dog.setImageResource(R.drawable.dog);
                break;
            case R.id.pawpaw:
                pawpaw.setImageResource(R.drawable.pawpaw);
                break;
            case R.id.purple:
                purple.setImageResource(R.drawable.purple);
                break;
            case R.id.zero:
                zero.setImageResource(R.drawable.zero);
                break;
            case R.id.watermelon:
                watermelon.setImageResource(R.drawable.watermelon);
                break;

            case R.id.jugar:
                vfFotos = (ViewFlipper) findViewById(R.id.vfFotos);

                vfFotos.setFlipInterval(3000);
                vfFotos.startFlipping();
                activar();
                cronometro();
                break;
            case R.id.salir:
                finish();
                break;

        }

    }
}
    
asked by Feli Vieyra 20.11.2017 в 02:14
source

1 answer

0

Try the following:

imageView.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.id_imageView_a_comparar).getConstantState());

If you use a View other than ImageView, use getBackground().getConstantState() instead.

Or this:

Bitmap bitmap = ((BitmapDrawable)Drawable1).getBitmap();
Bitmap bitmap2 = ((BitmapDrawable)Drawable2).getBitmap();

if(bitmap == bitmap2){
  //Code blcok
}

Here's more information: link

    
answered by 20.11.2017 в 02:25