I am working on a project right now in Android, and we have been using an edited joystick class that has been great! However, when optimizing the code for the project, I used Lint in Eclipse to identify any trouble spots to clean up. It pointed out that I allocated memory for objects in my onMeasured method in my DualJoystickView class, and suggested that I preallocate and reuse the values I am creating since onMeasured is used in Draw. I tried this, and the code I am attaching reflects this update. I have commented out the old code, in the onMeasured method. The problem is, now that I am preallocating and reusing the values, my joysticks don't show up anymore! I read another post about using joysticks in android, and the poster was able to work around a similar issue by using an if statement to see if their objects were null or not, to avoid wasting time updating them during the Draw, and I tried this as well to no avail. I can make the joysticks show up by uncommenting my old code, but I want to do this the most efficient way that I can. Any suggestions would be greatly 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 from 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;
}
}