java - Android joystick won't show up -
i working on project right in android, , have been using edited joystick class has been great! however, when optimizing code project, used lint in eclipse identify trouble spots clean up. pointed out allocated memory objects in onmeasured method in dualjoystickview class, , suggested preallocate , reuse values creating since onmeasured used in draw. tried this, , code attaching reflects update. have commented out old code, in onmeasured method. problem is, preallocating , reusing values, joysticks don't show anymore! read post using joysticks in android, , poster able work around similar issue using if statement see if objects null or not, avoid wasting time updating them during draw, , tried no avail. can make joysticks show uncommenting old code, want efficient way can. suggestions appreciated!
code:
package ebork.myrobot.controllerside; import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.util.attributeset; import android.view.motionevent; import android.view.view; import android.view.viewgroup; import android.widget.linearlayout; public class dualjoystickview extends linearlayout { @suppresswarnings("unused") private static final string tag = dualjoystickview.class.getsimplename(); private final boolean d = false; private paint dbgpaint1; private joystickview stickl; private joystickview stickr; private view pad; //preallocating params onmeasured instead of allocating during draw float padw = getmeasuredwidth()-(getmeasuredheight()*2); int joywidth = (int) ((getmeasuredwidth()-padw)/2); layoutparams joylparams = new layoutparams(joywidth,getmeasuredheight());; viewgroup.layoutparams padlparams = new viewgroup.layoutparams((int) padw,getmeasuredheight()); public dualjoystickview(context context) { super(context); stickl = new joystickview(context); stickr = new joystickview(context); initdualjoystickview(); } public dualjoystickview(context context, attributeset attrs) { super(context, attrs); stickl = new joystickview(context, attrs); stickr = new joystickview(context, attrs); initdualjoystickview(); } private void initdualjoystickview() { setorientation(linearlayout.horizontal); stickl.sethandlecolor(color.rgb(0x70, 0x20, 0x20)); stickr.sethandlecolor(color.rgb(0x20, 0x50, 0x30)); if ( d ) { dbgpaint1 = new paint(paint.anti_alias_flag); dbgpaint1.setcolor(color.cyan); dbgpaint1.setstrokewidth(1); dbgpaint1.setstyle(paint.style.stroke); } pad = new view(getcontext()); } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { super.onmeasure(widthmeasurespec, heightmeasurespec); removeview(stickl); removeview(stickr); /* float padw = getmeasuredwidth()-(getmeasuredheight()*2); int joywidth = (int) ((getmeasuredwidth()-padw)/2); joylparams = new layoutparams(joywidth,getmeasuredheight()); */ stickl.setlayoutparams(joylparams); stickr.setlayoutparams(joylparams); stickl.tag = "l"; stickr.tag = "r"; stickl.setpointerid(joystickview.invalid_pointer_id); stickr.setpointerid(joystickview.invalid_pointer_id); addview(stickl); //padlparams = new viewgroup.layoutparams((int) padw,getmeasuredheight()); removeview(pad); pad.setlayoutparams(padlparams); addview(pad); addview(stickr); } @override protected void onlayout(boolean changed, int l, int t, int r, int b) { super.onlayout(changed, l, t, r, b); stickr.settouchoffset(stickr.getleft(), stickr.gettop()); } public void setautoreturntocenter(boolean left, boolean right) { stickl.setautoreturntocenter(left); stickr.setautoreturntocenter(right); } public void setonjostickmovedlistener(joystickmovedlistener left, joystickmovedlistener right) { stickl.setonjostickmovedlistener(left); stickr.setonjostickmovedlistener(right); } public void setonjostickclickedlistener(joystickclickedlistener left, joystickclickedlistener right) { stickl.setonjostickclickedlistener(left); stickr.setonjostickclickedlistener(right); } public void setyaxisinverted(boolean leftyaxisinverted, boolean rightyaxisinverted) { stickl.setyaxisinverted(leftyaxisinverted); stickr.setyaxisinverted(rightyaxisinverted); } public void setmovementconstraint(int movementconstraint) { stickl.setmovementconstraint(movementconstraint); stickr.setmovementconstraint(movementconstraint); } public void setmovementrange(float movementrangeleft, float movementrangeright) { stickl.setmovementrange(movementrangeleft); stickr.setmovementrange(movementrangeright); } public void setmoveresolution(float leftmoveresolution, float rightmoveresolution) { stickl.setmoveresolution(leftmoveresolution); stickr.setmoveresolution(rightmoveresolution); } public void setusercoordinatesystem(int leftcoordinatesystem, int rightcoordinatesystem) { stickl.setusercoordinatesystem(leftcoordinatesystem); stickr.setusercoordinatesystem(rightcoordinatesystem); } @override protected void dispatchdraw(canvas canvas) { super.dispatchdraw(canvas); if (d) { canvas.drawrect(1, 1, getmeasuredwidth()-1, getmeasuredheight()-1, dbgpaint1); } } @override public boolean dispatchtouchevent(motionevent ev) { boolean l = stickl.dispatchtouchevent(ev); boolean r = stickr.dispatchtouchevent(ev); return l || r; } @override public boolean ontouchevent(motionevent ev) { boolean l = stickl.ontouchevent(ev); boolean r = stickr.ontouchevent(ev); return l || r; } }
Comments
Post a Comment