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