android - how to draw on a surface view? -


i'm trying make paint application this: user inter face

so when user put hand in surface view, user able draw lines inside surface view. xml:

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" >  <textview     android:id="@+id/textview1"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="paint"     android:layout_margintop="10sp"     android:layout_marginleft="5sp"     android:textappearance="?android:attr/textappearancelarge"     android:layout_weight="3" /> <textview         android:id="@+id/textv"         android:layout_width="wrap_content"         android:layout_height="match_parent"         android:layout_alignparenttop="true"         android:layout_margintop="20dip"         android:layout_torightof="@+id/ivimage"         android:text="user name"           />         <imageview         android:id="@+id/ivimage"         android:layout_width="50dp"         android:layout_height="match_parent"         android:src="@drawable/person"          />          </linearlayout>         <linearlayout         android:layout_width="match_parent"         android:layout_height="match_parent"         android:orientation="horizontal"         android:layout_margintop="20sp">         <scrollview        android:id="@+id/scrollview1"        android:layout_width="wrap_content"        android:layout_height="wrap_content" >         <linearlayout        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:orientation="vertical" >         <textview         android:id="@+id/textview2"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="assets models"          android:gravity="center"/>         <button         android:id="@+id/button1"         android:layout_width="match_parent"         android:drawableleft="@drawable/ic_launcher"         android:layout_height="wrap_content"         android:text="button" />          </linearlayout>         </scrollview>          <surfaceview          android:id="@+id/surfaceview1"          android:layout_width="wrap_content"          android:layout_height="match_parent"          android:layout_weight="2" />          <scrollview          android:id="@+id/scrollview2"          android:layout_width="wrap_content"          android:layout_height="wrap_content" >          <linearlayout          android:layout_width="wrap_content"          android:layout_height="match_parent"          android:orientation="vertical"          android:layout_gravity="right" >         <textview         android:id="@+id/textview3"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:gravity="center"         android:text="assets models" />         <button         android:id="@+id/button2"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:drawableleft="@drawable/ic_launcher"         android:text="button" />         </linearlayout>        </scrollview>        </linearlayout>           </linearlayout> 

does know how surfaceview or view defined in xml file not in dynamic way put elements e.g buttons, scroll, user name , image @ same activity. appreciate help. thanks

to manage that, can follow below steps.

  1. create java class , extend surfaceview.

  2. use code below

  3. and use ui component in "yourlayout.xml"

    public class mysurfaceview extends surfaceview {    public static paint mpaint;   public static path path;   public static bitmap mbitmap;   public static canvas mcanvas;   private arraylist<pathwithpaint> _graphics1 = new arraylist<pathwithpaint>();    public mysurfaceview(context context, attributeset attrs) {     super(context, attrs);     path = new path();     mbitmap = bitmap.createbitmap(820, 480, bitmap.config.argb_8888);     mcanvas = new canvas(mbitmap);     this.setbackgroundcolor(color.black);     mpaint = new paint();     mpaint.setdither(true);     mpaint.setcolor(0xffffff00);     mpaint.setstyle(paint.style.stroke);     mpaint.setstrokejoin(paint.join.round);     mpaint.setstrokecap(paint.cap.round);     mpaint.setstrokewidth(3);   }    @override   public boolean ontouchevent(motionevent event) {     pathwithpaint pp = new pathwithpaint();     mcanvas.drawpath(path, mpaint);     if (event.getaction() == motionevent.action_down) {       path.moveto(event.getx(), event.gety());       path.lineto(event.getx(), event.gety());     } else if (event.getaction() == motionevent.action_move) {       path.lineto(event.getx(), event.gety());       pp.setpath(path);       pp.setmpaint(mpaint);       _graphics1.add(pp);     }     invalidate();     return true;   }    @override   protected void ondraw(canvas canvas) {     super.ondraw(canvas);     if (_graphics1.size() > 0) {       canvas.drawpath(_graphics1.get(_graphics1.size() - 1).getpath(),             _graphics1.get(_graphics1.size() - 1).getmpaint());     }   }    public class pathwithpaint {     private path path;     public path getpath() {       return path;     }     public void setpath(path path) {       this.path = path;     }     private paint mpaint;     public paint getmpaint() {       return mpaint;     }     public void setmpaint(paint mpaint) {       this.mpaint = mpaint;     }   } } 

and use in layout

<relativelayout 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:paddingleft="@dimen/activity_horizontal_margin"     android:paddingright="@dimen/activity_horizontal_margin"     android:paddingtop="@dimen/activity_vertical_margin"     android:paddingbottom="@dimen/activity_vertical_margin"     tools:context=".mainactivity">  <ayttunc.example.com.drawingstackoverflow.mysurfaceview     android:layout_width="match_parent"     android:layout_height="match_parent" />  </relativelayout> 

then run want notice didn't write anycode in mainactivity, , after run see this!

http://i.stack.imgur.com/yjgk6.png

and if want more information can send me mail via aytuncmatrac@anadolu.edu.tr.


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -