Android Views Drawing In Canvas
I am creating my own view and drawing a bitmap in t开发者_高级运维he canvas.If I set it at say (100,100) it is fine on a 320*480 screen but on bigger 480*800 it does not show up in the same place.
How can I get it to be at the same place.I have searched Google much but can't find a solution.
Any tips? Tutorials?
Thanks
I think you must take the display metrics
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
and give to your canvas dynamic values. Something like this width / 5
.
I hope this will help you.
You can use this method in your custom view to get the exact size of the canvas. Then you can calculate the position and the size of the rectangle where you will draw you bitmap.
@Override public void onSizeChanged (int w, int h, int oldw, int oldh){
super.onSizeChanged(w, h, oldw, oldh);
canvasW = w;
canvasH = h;
}
You can also resize your bitmaps within this method to accomodate the size of the canvas.
Even though your canvas may take the full screen, in Android 3.0 there is a bar on the bottom which will take some space, hence onSizeChanged gives a real size of the canvas.
精彩评论