Android custom View dimensions and canvas coordinates
NickName:Prettygeek Ask DateTime:2014-01-23T16:22:56

Android custom View dimensions and canvas coordinates

I have custom View that should paint a bitmap. I've created a simple line(bmp 1x50 px) to check where will be drawn exactly. In my onDraw(Canvas c) I'm using this line of code:

canvas.drawBitmap(bitmap, 0, 0, paint);

Where paint is simple Paint object, without any fancy settings. The problem is that this line shows at the middle of the view, not on top, as I thought it would be.

I'm setting View properties in layout xml:

<com.example.CustomView
    android:id="@+id/costom_view"
    android:layout_width="230px"
    android:layout_height="188px"
    android:layout_marginTop="10px"
    custom:srcDraw="@drawable/menu_indicators_level_draw_up" />

This custom property is the ID of the bitmap I use to draw on canvas.

Why won't the View behave properly? And why does my canvas have a size if 800x450 (size of the screen i suppose) and not 230x188 like I defined in the layout?

And very important thing, I want to multiply the same bitmap on this view many times. At the end of work I want to draw this line from bottom to top of my View.

Copyright Notice:Content Author:「Prettygeek」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/21302879/android-custom-view-dimensions-and-canvas-coordinates

More about “Android custom View dimensions and canvas coordinates” related questions

Android custom View dimensions and canvas coordinates

I have custom View that should paint a bitmap. I've created a simple line(bmp 1x50 px) to check where will be drawn exactly. In my onDraw(Canvas c) I'm using this line of code: canvas.drawBitmap(b...

Show Detail

What determines initial Canvas dimensions in a View?

I'm working with the 1.6 version of the ZXing barcode scanner code. Although the version of this app from the Android Market (3.53) runs fine on my Incredible, the ViewFinderView looks wrong when I

Show Detail

centered zoom in custom view - calculating canvas coordinates

I am working on my first "real" Android application, a graphical workflow editor. The drawing is done in a custom class, that is a subclass of View.At the moment my elements are rectangles, which are

Show Detail

How to Convert View Coordinates to Canvas Coordinates on Android?

I'm porting an app from ios to android that involves drag and drop items from a list view into a view that has a rectangle area. On Swift I used this Code to validate that the item is inside the m...

Show Detail

how to Get coordinates from Custom view android

I have some custom view with onTouchEvent where I'm setting mX and mY . mx=event.getX(); my=event.getY(); this is for getting the drawing path of touch event - working great, and I can see the

Show Detail

ANDROID: Custom view implemtation

I have a custom view. When will android call draw() on it? After it calls onMeasure and onLayout? 'package global.domination.pack; import android.content.Context; import android.graphics.Bitmap; ...

Show Detail

Android path not following coordinates in relative view

I have a custom path which I got the coordinates from Adobe Illustrator, it's relative to a 600x600 rectangle. When I try to draw that path on Android for my Custom View, it doesn't draw at the cor...

Show Detail

Android Screen coordinates to canvas view coordinates

I am trying to turn my screen x and y coordinates to the ones that's used to draw on screen. So I get my screen X and Y coordinates from MotionEvent thats fired by my touch listener. I thought it

Show Detail

Canvas in my View sets up as full screen dimensions, rather than View dimensions... why?

I have a class that extends View that I draw into. In spite of the fact that the view is set up as a child to a RelativeLayout that has specific dimensions, the View's canvas reports that it is 1280

Show Detail

Retrieving Custom Layout dimensions before setContentView()

I am trying to draw a Custom View and have it inside a Scroll View. The way I did it is define the Custom View and add it inside a Linear Layout, which is itself located inside a Scroll View. Pro...

Show Detail