android - How to draw a line over a GridView programmatically -
i new posting on stack overflow, best descriptive here.
right playing around android studio. have set gridview (i built own custom image adapter) contains square images form 5 6 grid padding in-between each grid space. want able draw line (using canvas , paint) on top of gridview programatically.
i have looked @ quite few posts on stack overflow in order try , solve issue, such one: draw line on top of existing layout programmatically
when following instructions link above ^^^, able create program draws line on imageview.
but when use similar code in program tries draw line on gridview, draws line , nothing else.
here code snippets of i'm trying do. xml file:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- game layout *er --> <gridview xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gridviewgame" android:layout_width="match_parent" android:layout_weight="1" android:layout_height="0dp" android:numcolumns="5" android:stretchmode="columnwidth" android:gravity="center" /> <com.example.ella.lazorsgame.drawview android:id="@+id/paintview" android:layout_width="match_parent" android:layout_height="match_parent" /> </linearlayout>
this custom view code:
public class drawview extends view { paint paint = new paint(); private int strokewidth = 8; public drawview(context context, attributeset attrs) { super(context, attrs); paint.setcolor(color.red); paint.setstrokewidth(strokewidth); } @override public void ondraw(canvas canvas) { canvas.drawline(0, 0, 300, 300, paint); } }
and i'm drawing grid of images in main activity (this code works itself... grid doesn't show when try , draw line on top):
public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_game_setup); gridview gridview = (gridview) findviewbyid(r.id.gridviewgame); blockgridadapter = new imageadapter(this, r.drawable.selectedblock, 30); // set grid space object array *er (int = 0; < blockspaces.length; i++) { blockspaces[i] = new blockspace(gridview, i); } // set grid visual display on phone screen *er gridview.setadapter(blockgridadapter); // ------------- initial game setup (block placements) -------------- *er blocksetup(levelselected); gridview.setonitemclicklistener(new adapterview.onitemclicklistener() { public void onitemclick(adapterview<?> parent, view v, int position, long id) { updateselection(position); } }); }
thank help! let me know if more information needed.
you not calling super method of ondraw
super.ondraw(canvas);
thus thing drawn line , system doesn't handle other views invalidated , needs de redrawn!!!
check out: http://developer.android.com/training/custom-views/custom-drawing.html
Comments
Post a Comment