android - how to draw my custom view on GridView? -
i have created custom view displaying letter in fill circle. example circle filled green , letter on red.
now want draw several custom views in gridview. example if gridview 5x5, you'll see 25 custom views on screen (and maybe each custom view has different letter)
anyone know this? thank help
edit #1: custom view
package com.hoangtrinh.paintmycustomview; import android.content.context; import android.content.res.typedarray; import android.graphics.canvas; import android.graphics.paint; import android.graphics.paint.style; import android.util.attributeset; import android.view.view; public class letterview extends view { private int letterbackgroundcolor, lettercolor; private string lettertext; private paint myletterpaint; public int getletterbackgroundcolor() { return letterbackgroundcolor; } public void setletterbackgroundcolor(int letterbackgroundcolor) { this.letterbackgroundcolor = letterbackgroundcolor; invalidate(); requestlayout(); } public int getlettercolor() { return lettercolor; } public void setlettercolor(int lettercolor) { this.lettercolor = lettercolor; invalidate(); requestlayout(); } public string getlettertext() { return lettertext; } public void setlettertext(string lettertext) { this.lettertext = lettertext; invalidate(); requestlayout(); } public letterview(context context) { super(context); attributeset attrs = null; myletterpaint = new paint(); typedarray = context.gettheme().obtainstyledattributes(attrs, r.styleable.letterview, 0, 0); try { letterbackgroundcolor = a.getinteger( r.styleable.letterview_letterbackgroundcolor, 0); lettercolor = a.getinteger(r.styleable.letterview_lettercolor, 0); lettertext = a.getstring(r.styleable.letterview_lettertext); } { a.recycle(); } } public letterview(context context, attributeset attrs) { super(context); myletterpaint = new paint(); typedarray = context.gettheme().obtainstyledattributes(attrs, r.styleable.letterview, 0, 0); try { letterbackgroundcolor = a.getinteger( r.styleable.letterview_letterbackgroundcolor, 0); lettercolor = a.getinteger(r.styleable.letterview_lettercolor, 0); lettertext = a.getstring(r.styleable.letterview_lettertext); } { a.recycle(); } } @override protected void ondraw(canvas canvas) { int viewwidthhalf = this.getmeasuredwidth() / 2; int viewheighthalf = this.getmeasuredheight() / 2; int radius = 0; if(viewwidthhalf>viewheighthalf) radius=viewheighthalf-10; else radius=viewwidthhalf-10; myletterpaint.setantialias(true); myletterpaint.setstyle(style.fill); myletterpaint.setcolor(letterbackgroundcolor); canvas.drawcircle(viewwidthhalf, viewheighthalf, radius, myletterpaint); myletterpaint.setcolor(lettercolor); myletterpaint.settextalign(paint.align.center); myletterpaint.settextsize(50); canvas.drawtext(lettertext, viewwidthhalf, viewheighthalf, myletterpaint); } }
this custom view adapter
package com.hoangtrinh.paintmycustomview; import java.util.arraylist; import java.util.list; import android.content.context; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.linearlayout; public class letterviewadapter extends baseadapter { private list<letterview> customviews = new arraylist<letterview>(); private context context; public letterviewadapter(list<letterview> customviews, context context) { this.customviews = customviews; this.context = context; } @override public int getcount() { return customviews.size(); } @override public object getitem(int position) { return customviews.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { if (convertview == null) { // can construct view here. letterview letterview = new letterview(context); linearlayout layout = new linearlayout(context); layout.addview(letterview); return layout; // or return custom views array element // return customviews.get(position); } else { return convertview; } } }
this mainactivity
package com.hoangtrinh.paintmycustomview; import java.util.arraylist; import java.util.list; import android.app.activity; import android.os.bundle; import android.view.menu; import android.widget.gridview; public class mainactivity extends activity { gridview mygridview; static final string[] letter_case = new string[] { "a", "b","c", "d" }; list<letterview> customviews = new arraylist<letterview>(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mygridview = (gridview) findviewbyid(r.id.gridview1); mygridview.setadapter(new letterviewadapter(customviews, this)); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } }
this custom view (letter.xml)
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res/com.hoangtrinh.paintmycustomview" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.hoangtrinh.paintmycustomview.letterview android:id="@+id/lv1" android:layout_width="fill_parent" android:layout_height="wrap_content" custom:letterbackgroundcolor="#00ffff" custom:lettercolor="#ff0000" custom:lettertext="h" /> </linearlayout>
and activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <gridview xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gridview1" android:numcolumns="auto_fit" android:gravity="center" android:columnwidth="40dp" android:stretchmode="columnwidth" android:layout_width="fill_parent" android:layout_height="fill_parent" > </gridview>
you have write own adapter class. extend baseadapter class , override (http://developer.android.com/reference/android/widget/adapter.html#getview (int, android.view.view, android.view.viewgroup) return custom view.
example: http://developer.android.com/guide/topics/ui/layout/gridview.html
public class customadapter extends baseadapter { private list<customview> customviews = new arraylist<customview>(); private context context; public customadapter(list<customview> customviews, context context) { this.customviews = customviews; this.context = context; } @override public int getcount() { return customviews.size(); } @override public object getitem(int position) { return customviews.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { if (convertview == null) { // can construct view here. textview textview = new textview(context); textview.settext("" + position); imageview imageview = new imageview(context); imageview.setimagedrawable(context.getresources().getdrawable( r.drawable.btn_star)); linearlayout layout = new linearlayout(context); layout.addview(textview); layout.addview(imageview); return layout; // or return custom views array element // return customviews.get(position); } else { return convertview; } } }
Comments
Post a Comment