Canvas - getWidth (), getHeight () and OnClickListener ()

0

I'm making an app for Android using the Canvas class, but I'm having some problems.

1st. When you run the methods below,

canvas.drawText("Width: " + fotoOriginal.getWidth(), 0, 10, paint);
canvas.drawText("Height: " + fotoOriginal.getHeight(), 0, 20, paint);

I see on the screen: "Width: 1580" and "Height: 977", that is, my photo has 1580 x 977 px.

But when I try to write on the screen the ratio of width to height,

myRatio = fotoOriginal.getWidth() / fotoOriginal.getHeight();
canvas.drawText("Ratio is: " + myRatio, 0, 30, paint);

The result that appears on the screen is 1.0. No matter what I do, the result is always 1 or 0. Why does not the real reason for the image appear, that is, 1,617 ...

2nd. As I said, the getWidth () and getHeight () methods return me 1580 and 977 respectively, but when I go to the photo file and hover over it, "Dimensions: 1053 x 651" appears, the same dimension that appears in Paint. Why is there a difference in size?

3 °. What is the most correct way (and where) to add the OnClickListener (), for example to draw anything on Canvas when a user clicks the screen?

Follow the code:

public class MyActivity extends Activity{

public int teste = 0;

DrawView drawView;

public static int width;
public static int height;

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

    // 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();

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

    setContentView(drawView);

}

public void fullscreen(){

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

}

public class HeyClick implements View.OnClickListener{

    @Override
    public void onClick(View v) {

        Toast.makeText(getApplicationContext(), "" + teste, Toast.LENGTH_SHORT).show();
        teste++;

    }

}

}

The other class:

public class DrawView extends View{

Paint paint = new Paint();

int x = MyActivity.width;
int y = MyActivity.height;

Bitmap fotoOriginal;
Bitmap fotoScaled;
float myRatio;


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

    paint.setColor(Color.BLACK);

    fotoOriginal = BitmapFactory.decodeResource(getResources(), R.drawable.ilha_de_malaga_transparente);
    fotoScaled = Bitmap.createScaledBitmap(fotoOriginal, x, y, false);

    myRatio = fotoOriginal.getWidth() / fotoOriginal.getHeight();

}

@Override
protected void onDraw(Canvas canvas) {

    canvas.drawBitmap(fotoScaled,0,0, paint);
    canvas.drawText("Width: " + fotoOriginal.getWidth(), 0, 10, paint);
    canvas.drawText("Height: " + fotoOriginal.getHeight(), 0, 20, paint);
    canvas.drawText("Ratio is: " + myRatio, 0, 30, paint);

}

}
    
asked by anonymous 19.01.2015 / 00:57

1 answer

1

Your problem is that you are dividing two integers and the result ends up being another integer.

You can cast one of the operators to float that the result of the division will also be a float.

myRatio = (float)fotoOriginal.getWidth() / fotoOriginal.getHeight();

If you want to show only two decimal places you can use String.format

canvas.drawText(String.format("Ratio is: %.2f", myRatio), 0, 30, paint);
    
19.01.2015 / 01:35