0
I’m making an app for Android using the Canvas class, but I’m having some problems.
1º. When executing 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 width ratio by 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 appear the real reason of the image, ie 1,617...?
second. As I said, the methods getWidth() and getHeight() return me 1580 and 977, respectively, but when I go in the photo file and leave the mouse on top of it, it appears "Dimensions: 1053 x 651", the same dimension that appears in Paint. Why is there a difference in dimension?
3°. What is the most correct (and where) way for me to add Onclicklistener(), for example to draw anything on Canvas when a user clicks on the screen?
Follows 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);
}
}