java - Coordinate offset between MainActivity and Custom View -



java - Coordinate offset between MainActivity and Custom View -

in application, draw graphics using canvas object in mainactivity class. after when user swipes on screen, utilize custom view draw horizontal , vertical lines on created graphics.

the problem there slight coordinate offset between mainactivity , customview. due lines not in accordance other graphic objects on game. confirm this, made next phone call mainactivity , customview class:

canvas.drawline(0, 0, 50, 50, paint);

the result 2 parallel lines. origin customview lies @ top right corner of phone there offset origin of mainactivity class.

(screenshot: http://i.stack.imgur.com/x3bdk.png )

i grateful if point out on issue here.

following relevant files:

activity_main.xml

<com.example.offsettest.customlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/customlayout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".mainactivity" android:orientation="vertical" android:paddingleft="0dp" android:paddingright="0dp" android:paddingbottom="0dp" android:paddingtop="0dp" android:layout_marginleft="0dp" android:layout_marginright="0dp" android:layout_margintop="0dp" android:layout_marginbottom="0dp"> </com.example.offsettest.customlayout>

mainactivity.java

package com.example.offsettest; import com.rgtech.offsettest.r; import android.app.activity; import android.graphics.bitmap; import android.graphics.bitmap.config; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.os.bundle; import android.util.displaymetrics; import android.view.menu; import android.widget.imageview; public class mainactivity extends activity { private bitmap bitmap; private canvas mcanvas; private paint paint; private customlayout customlayout; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); draw(); } public void draw(){ displaymetrics metrics = this.getresources().getdisplaymetrics(); float width = metrics.widthpixels; float height = metrics.heightpixels; bitmap = bitmap.createbitmap((int) width, (int) height, config.rgb_565); mcanvas = new canvas(bitmap); mcanvas.drawcolor(color.green); paint = new paint(); paint.setantialias(true); customlayout = (customlayout) findviewbyid(r.id.customlayout); imageview imageview = new imageview(this); imageview.setimagebitmap(bitmap); imageview.setpadding(0, 0, 0, 0); customlayout.addview(imageview); paint.setcolor(color.black); mcanvas.drawline(0, 0, 50, 50, paint); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.main, menu); homecoming true; } }

customlayout.java

package com.example.offsettest; import android.annotation.suppresslint; import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.util.attributeset; import android.widget.linearlayout; @suppresslint("newapi") public class customlayout extends linearlayout{ private paint paint = new paint(); public customlayout(context context) { super(context); } public customlayout(context context, attributeset attrs) { super(context, attrs); } public customlayout(context context, attributeset attrs,int defstyle) { super(context, attrs, defstyle); } @override public void dispatchdraw(canvas canvas) { super.dispatchdraw(canvas); paint.setcolor(color.black); paint.setantialias(true); paint.setdither(true); paint.setstyle(paint.style.stroke); paint.setstrokejoin(paint.join.round); paint.setstrokewidth(5); paint.setcolor(color.red); canvas.drawline(0, 0, 50, 50, paint); } }

in add-on these files, set android:theme="@android:style/theme.notitlebar" in manifest file.

the easiest way solve remove padding or margin of image view or of root view of layout. (if there any) possibility calculate offset/distance between left-top-corner positions using view.getx() respectively view.getrawx() (and y well) , draw lines not @ (0, 0, 50, 50) @ (offsetx, offsety, 50 + offsetx, 50 + offsety).

edit: played around code little bit , noticed calling imageview.setadjustviewbounds(true); solves problem. draw method in mainactivity.java should this:

public void draw() { displaymetrics metrics = this.getresources().getdisplaymetrics(); float width = metrics.widthpixels; float height = metrics.heightpixels; bitmap = bitmap.createbitmap((int) width, (int) height, config.rgb_565); mcanvas = new canvas(bitmap); mcanvas.drawcolor(color.green); paint = new paint(); paint.setantialias(true); customlayout = (customlayout) findviewbyid(r.id.customlayout); imageview imageview = new imageview(this); imageview.setimagebitmap(bitmap); imageview.setpadding(0, 0, 0, 0); imageview.setadjustviewbounds(true); // phone call method customlayout.addview(imageview); paint.setcolor(color.black); mcanvas.drawline(0, 0, 50, 50, paint); }

java android graphics coordinates

Comments

Popular posts from this blog

php - Edges appear in image after resizing -

ios8 - iOS custom keyboard - preserve state between appearances -

Delphi change the assembly code of a running process -