java - How to draw lines in an existing activity? -
in fact, want code bridge constructor game , user must able draw these lines make bridge..
already answered : i've been trying create class allow me draw lines on existing layout, app crashed when try launch activity "game" (by pressing button in main menu). logcat told : "fatal exception : main" , have nullpointerexception.
[edit] : search how lines we've created individual objects (we suppose there bitmaps canvas created), don't know how these bitmaps...
here code of activity "customview" (use draw lines) :
package com.g70.buildmybridge; import java.util.linkedlist; import android.content.context; import android.graphics.bitmap; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.graphics.path; import android.view.motionevent; import android.view.view; public class customview extends view { private paint mpaint; private bitmap mbitmap; private canvas mcanvas; private path mpath; private paint mbitmappaint; private linkedlist<path> paths = new linkedlist<path>(); public customview (context context) { super(context); setfocusable(true); setfocusableintouchmode(true); mpath = new path(); mcanvas = new canvas(); mbitmappaint = new paint(); mpaint = new paint(); mpaint.setcolor(color.black); mpaint.setstyle(paint.style.stroke); mpaint.setstrokejoin(paint.join.bevel); mpaint.setstrokecap(paint.cap.round); mpaint.setstrokewidth(6); paths.add(mpath); } @override protected void ondraw(canvas canvas) { (path p : paths){ canvas.drawpath(p, mpaint); } } private float mx, my; private static final float touch_tolerance = 4; // à la touche posée private void touch_start(float x, float y) { x=math.round(x/100)*100; y=math.round(y/100)*100; mpath.reset(); // deplacement du path et positionnement en x,y mpath.moveto(x, y); mx = x; = y; } // la touche levée private void touch_up(float x, float y) { float dx=(x-mx); float dy=(y-my); if(dy>=0&dy<=50){ y=my; } if(dy>=50){ y=my+100; } if(dx>=0&dx<=50){ x=mx; } if(dx>=50){ x=mx+100; } if(dx<=0&dx>=-50){ x=mx; } if(dy<=0&dy>=-50){ y=my; } if(dx<=-50){ x=mx-100; } if(dy<=-50){ y=my-100; } mpath.lineto(x, y); // dessin du path sur l'objet paint mcanvas.drawpath(mpath, mpaint); // reset du path pour ne pas avoir le chemin précédent mpath = new path(); paths.add(mpath); mpath.reset(); } // lancement des methodes de touch de l'écran tactile du smartphone @override public boolean ontouchevent(motionevent event) { // prend l'axe des x float x = event.getx(); // prend l'axe des y float y = event.gety(); switch (event.getaction()) { case motionevent.action_down: touch_start(x, y); invalidate(); break; case motionevent.action_up: touch_up(x,y); invalidate(); break; } return true; } /**public void changecolor(string s){ if(s=="metal"){ mpaint.setcolor(color.gray); } if(s=="wood"){ mpaint.setcolor(color.yellow); } }**/ }
and here code of game :
package com.g70.buildmybridge; import android.app.activity; import android.content.intent; import android.os.bundle; import android.widget.button; import android.widget.gridlayout; import android.widget.linearlayout; import android.view.view; import android.view.view.onclicklistener; public class game extends activity { protected gridlayout drawlayout; customview customview; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.game1); drawlayout = (gridlayout) findviewbyid(r.id.game1); customview = new customview(this); drawlayout.addview(customview); customview.requestfocus(); final button exit = (button) findviewbyid(r.id.button4); exit.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { intent intent = new intent(game.this, mainactivity.class); startactivity(intent); } }); /**final button undo = (button) findviewbyid(r.id.button5); undo.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { customview.paths.polllast(); } });**/ } }
thank in advance !
here's error in game/oncreate:
mainlayout = (linearlayout) findviewbyid(r.layout.draw);
here must id, not layout...
so, in /res/layout/game must linearlayout id="@+id/draw"
solution: change above instruction to:
mainlayout = (linearlayout) findviewbyid(r.id.draw);
note: it's findviewbyid, not findviewbylayout.
[edit]
you never initialize path object in customview class. in constructor, after super line:
mpath = new path();
[edit 2]
also these objects need initialized:
private paint mpaint; private bitmap mbitmap; private canvas mcanvas; private paint mbitmappaint;
you start using new paint object, paint, try use mbitmappaint , mpaint, instead.
[edit 3]
i found post might find interesting: android how draw smooth line following finger
[edit 4]
finally works, want bitmap created objects (each line 1 one !) make them move ... don't find on how them ! edit our code see have done !
that should question... 1 q => 1 a.
makes no sense (and adds confusion) edit question , change different one.
invalidate previous answers...
Comments
Post a Comment