Toast is not being shown

Asked

Viewed 428 times

3

I tried to search several sources, but I still don’t understand why Toast class DrawView is not displayed when I click on the screen.

Any idea?

public class MyActivity extends Activity{

public static Context context;

public int teste = 0;

DrawView drawView;

public static int width;
public static int height;

public static boolean displayInPortrait;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    context = getApplicationContext();

    // Para a activity pegar a tela inteira
    fullscreen();

    // Para saber o tamanho da tela
    Display display = getWindowManager().getDefaultDisplay();

    Point size = new Point();
    display.getSize(size);
    width = size.x;
    height = size.y;

    Toast.makeText(getApplicationContext(), "Screen size is: " + width + " px (X), and " + height + "px (Y)!", Toast.LENGTH_LONG).show();

    if(height > width){
        displayInPortrait = true;
        Toast.makeText(getApplicationContext(), "Portrait Mode", Toast.LENGTH_SHORT).show();
    } else{
        displayInPortrait = false;
        Toast.makeText(getApplicationContext(), "Landscape Mode", Toast.LENGTH_SHORT).show();
    }

    // DrawView
    drawView = new DrawView(this);
    drawView.setBackgroundColor(Color.WHITE);

    setContentView(drawView);

}

public void fullscreen(){

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

}

}

Drawview class:

public class DrawView extends View implements View.OnTouchListener{

Context context = MyActivity.context;

Rect banheiro;

Paint paint = new Paint();

int deviceWidth = MyActivity.width;
int deviceHeight = MyActivity.height;
float deviceRatio;

float difAltura;
float difLargura;

float scaledPhotoHeight_1;
float scaledPhotoWidth_1;

float scaledPhotoHeight_2;
float scaledPhotoWidth_2;

boolean deviceOrientation = MyActivity.displayInPortrait;

Bitmap fotoOriginal;
Bitmap fotoScaled;
float myRatio;
float myScaledRatio;

public DrawView(Context context) {
    super(context);

    paint.setColor(Color.BLACK);

    deviceRatio = (float) deviceHeight / deviceWidth;

    fotoOriginal = BitmapFactory.decodeResource(getResources(), R.drawable.ilha_de_malaga_transparente);
    myRatio = (float)fotoOriginal.getHeight() / fotoOriginal.getWidth();

    redimensionar();

    fotoScaled = Bitmap.createScaledBitmap(fotoOriginal, (int) scaledPhotoWidth_2, (int) scaledPhotoHeight_2, false);
    myScaledRatio = (float) fotoScaled.getHeight() / fotoScaled.getWidth();

    banheiro = new Rect(270,300,380,470);

}

@Override
protected void onDraw(Canvas canvas) {

    float desenharNoMeioDaTelaWidth = (float) (deviceWidth - scaledPhotoWidth_2)/2;
    float desenharNoMeioDaTelaHeight = (float) (deviceHeight - scaledPhotoHeight_2)/2;

    canvas.drawBitmap(fotoScaled, desenharNoMeioDaTelaWidth, desenharNoMeioDaTelaHeight, paint);

    paint.setColor(Color.RED);
    paint.setTextSize(25.0f);

    canvas.drawText("Device Height: " + deviceHeight, 0, 25, paint);
    canvas.drawText("Device Width: " + deviceWidth, 0, 50, paint);
    canvas.drawText("Device Ratio is: " + deviceRatio, 0, 75, paint);

    canvas.drawText("Original Photo Height: " + fotoOriginal.getHeight(), 0, 125, paint);
    canvas.drawText("Original Photo Width: " + fotoOriginal.getWidth(), 0, 150, paint);
    canvas.drawText("Original Photo Ratio is: " + myRatio, 0, 175, paint);

    canvas.drawText("Scaled Photo Height: " + fotoScaled.getHeight(), 0, 225, paint);
    canvas.drawText("Scaled Photo Width: " + fotoScaled.getWidth(), 0, 250, paint);
    canvas.drawText("Scaled Photo Ratio is: " + myScaledRatio, 0, 275, paint);

    paint.setColor(Color.argb(100,255,255,0));
    canvas.drawRect(banheiro,paint);

}

protected void redimensionar(){

    // O redimensionamento da foto é interessante, pois cada tela de celular
    // tem uma largura e altura, bem como cada foto. Dependendo das dimensões
    // da imagem, redimensionar só um lado (e aplicar a razão largura/altura)
    // ao outro já dá certo. As vezes tem que redimensionar duas vezes.

    difAltura = fotoOriginal.getHeight() - deviceHeight;
    difLargura = fotoOriginal.getWidth() - deviceWidth;

    // A largura e altura estão fora da tela.
    if(difAltura > 0 && difLargura > 0){

        // Começamos a redimensionar pelo lado que está mais fora da tela, e depois
        // aplicamos ao outro, se necessário.
        if(difAltura > difLargura) {

            float ratio = (float) deviceHeight / fotoOriginal.getHeight();

            scaledPhotoHeight_1 = ratio * fotoOriginal.getHeight();
            scaledPhotoWidth_1 = ratio * fotoOriginal.getWidth();

            // Neste ponto a altura da foto foi redimensionado para ser igual a altura da tela,
            // e a largura seguiu a proporção. Mas será que a largura ficou dentro da tela também?

            float difLargura_2 = scaledPhotoWidth_1 - deviceWidth;

            // A altura ficou fora da tela e tem que ser redimensionada
            if(difLargura_2 > 0){

                float ratio_2 = (float) deviceWidth / scaledPhotoWidth_1;

                scaledPhotoHeight_2 = ratio_2 * scaledPhotoHeight_1;
                scaledPhotoWidth_2 = ratio_2 * scaledPhotoWidth_1;

                // Fim, ambas as dimensões foram redimensionadas

            }

            else{

                scaledPhotoHeight_2 = scaledPhotoHeight_1;
                scaledPhotoWidth_2 = scaledPhotoWidth_1;

            }

        } else{

            float ratio = (float) deviceWidth / fotoOriginal.getWidth();

            scaledPhotoHeight_1 = ratio * fotoOriginal.getHeight();
            scaledPhotoWidth_1 = ratio * fotoOriginal.getWidth();

            // Neste ponto a largura da foto foi redimensionado para ser igual a largura da tela,
            // e a altura seguiu a proporção. Mas será que a altura ficou dentro da tela também?

            float difAltura_2 = (float) scaledPhotoHeight_1 - deviceHeight;

            // A altura ficou fora da tela e tem que ser redimensionada
            if(difAltura_2 > 0){

                float ratio_2 = (float) deviceHeight / scaledPhotoHeight_1;

                scaledPhotoHeight_2 = ratio_2 * scaledPhotoHeight_1;
                scaledPhotoWidth_2 = ratio_2 * scaledPhotoWidth_1;

                // Fim, ambas as dimensões foram redimensionadas

            }

            else{

                scaledPhotoHeight_2 = scaledPhotoHeight_1;
                scaledPhotoWidth_2 = scaledPhotoWidth_1;

            }

        }

        // Só 01 dimensão precisa ser ajustada
    } else if(difAltura > 0 || difLargura > 0){

        // Qual que tem que ser ajustada?
        if(difAltura > 0) {

            float ratio = (float) deviceHeight / fotoOriginal.getHeight();

            scaledPhotoHeight_1 = ratio * fotoOriginal.getHeight();
            scaledPhotoWidth_1 = ratio * fotoOriginal.getWidth();

        } else{

            float ratio = (float) deviceWidth / fotoOriginal.getWidth();

            scaledPhotoHeight_1 = ratio * fotoOriginal.getHeight();
            scaledPhotoWidth_1 = ratio * fotoOriginal.getWidth();

        }

        scaledPhotoHeight_2 = scaledPhotoHeight_1;
        scaledPhotoWidth_2 = scaledPhotoWidth_1;

        // Nenhuma dimensão está fora da tela
    } else{

        scaledPhotoHeight_2 = fotoOriginal.getHeight();
        scaledPhotoWidth_2 = fotoOriginal.getWidth();

    }

}

@Override
public boolean onTouch(View v, MotionEvent event) {

    if (event.getAction() == MotionEvent.ACTION_DOWN) {

        Toast.makeText(context, "(X, Y) = " + event.getX() + ", " + event.getY(), Toast.LENGTH_SHORT).show();

    }

    return false;
}
}
  • Delete the line Context context = MyActivity.context;, let alone Context context; and, in the builder of his class, place this.context = context. See if it works

  • @sicachester, unsuccessfully with this change...

  • I suggest leaving as @sicachester said, but you have already debugged and really onTouch function and if condition is being true?

  • Try to create the event onTouch within your Activity and pass as a parameter in your DrawView

1 answer

1

You forgot to register the event in Drawview. Try calling in the Drawview constructor:

this.setOnTouchListener(this);

Reference

Browser other questions tagged

You are not signed in. Login or sign up in order to post.