Unable to draw line over gridview elements properly through textview cells in Android -
i implement word search app. part of implementation have come across canvas , drawing line on grid view cells( letters form word) indicate user touching finger on letters form word.
i have succeeded partially of can draw line on letters of grid view line not through center of views of grid view.
please can assist me valuable suggestions .
have glance on below screen shot clear idea.
edited: i'm posting code idea of how i'm implementing it.
xml:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/root" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#fff" > <relativelayout android:id="@+id/topbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:background="#a9e2f3" > <linearlayout android:id="@+id/topbar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#336699" android:orientation="horizontal" > <button android:id="@+id/btn_pause" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="pause" /> <chronometer android:id="@+id/chronometer1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="chronometer" /> <textview android:id="@+id/counter" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:textsize="20sp" android:typeface="serif" /> </linearlayout> </relativelayout> <framelayout android:id="@+id/gridframe" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_above="@+id/wtable" android:layout_below="@+id/textdisplay" > <gridview android:id="@+id/grid" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#e7e8e9" android:fitssystemwindows="true" android:gravity="center" android:horizontalspacing="10sp" android:numcolumns="10" android:padding="1dp" android:stretchmode="columnwidth" android:verticalspacing="10sp" > </gridview> </framelayout> <gridview android:id="@+id/wtable" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:background="#fff" android:numcolumns="3" android:orientation="vertical" /> </relativelayout>
the paint drawing on frame layout contains grid view. grid view elements printed through custom text view file.
to draw line have used lineview.java
import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.util.log; import android.view.view; public class lineview extends view { public static final float line_width = 30.0f; public paint paint = new paint(); protected context context; public float startingx, startingy, endingx, endingy; public lineview(context context) { super(context); this.context = context; paint.setcolor(color.parsecolor("#2e9afe")); paint.setstrokewidth(line_width); paint.setstrokejoin(paint.join.round); paint.setstrokecap(paint.cap.round); paint.setdither(true); paint.setantialias(true); paint.setstyle(paint.style.fill); paint.setalpha(90); } public void setpoints(float startx, float starty, float endx, float endy) { startingx = startx; startingy = starty; endingx = endx; endingy = endy; invalidate(); } @override public void ondraw(canvas canvas) { log.e("lineview", "startingx" + startingx + " startingy:" + startingy); log.e("lineview", "endingx" + endingx + " endingy:" + endingy); // canvas.drawline(startingx, startingy, endingx, endingy, paint); canvas.drawline(startingx, startingy, endingx, endingy, paint); } } main activity logic implemented: written required logic here. newgrid = (gridview) findviewbyid(r.id.grid); newgrid.setadapter(new formthegridletters()); newgrid.setontouchlistener(new ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { // if (mp.isplaying()) { // mp.stop(); // } int action = event.getactionmasked(); switch (action) { case motionevent.action_down: case motionevent.action_move: case motionevent.action_up: // data paintviewholder newpaint = new paintviewholder(); newpaint.drawline = new lineview(wordsearchactivity.this); gridframe.addview(newpaint.drawline); buildword = new stringbuilder(); int x = (int) event.getx(); int y = (int) event.gety(); // test = new lineview(wordsearchactivity.this); int position = newgrid.pointtoposition(x, y); point one, two; if (position != gridview.invalid_position) { v.getparent().requestdisallowintercepttouchevent(true); cellview = (textview) newgrid.getchildat(position); string = cellview.gettext().tostring(); // log.v(">>>>><<<<<<<????????", a.tostring()); switch (action) { case motionevent.action_down: startx = event.getx(); starty = event.gety(); break; case motionevent.action_move: break; case motionevent.action_up: // checking list formed word ; //if found painted (int i1 = 0; i1 < ans.size(); i1++) { if (formedword.equals(ans.get(i1))) { answeradapter.notifydatasetchanged(); newpaint.drawline.setpoints(startx, starty, x, y); // painted letters passing starting , ending points } } break; } } else { if (mselecting) { mselecting = false; } } break; case motionevent.action_cancel: mselecting = false; break; } return true; } });
i don't know if fix issue answer anyway people may have these kind of problems. after recieve valid position, can center of view , can set these values beginning of draw.
like this:
if (position != gridview.invalid_position) { mylist.add(position); v.getparent().requestdisallowintercepttouchevent(true); textview cellview = (textview) gridview.getchildat(position); centrex = cellview.getx() + cellview.getwidth() / 2; centrey = cellview.gety() + cellview.getheight() / 2; switch (event.getaction()) { case motionevent.action_down: newpaint.drawline.touch_start(x, y,centrex,centrey);
i have tried code , working. don't think need anymore have joined site maybe other people. can post more code if want
newpaint.drawline.touch_start(x, y,centrex,centrey);
is trick issue. hope helps.
Comments
Post a Comment