Android操纵杆不会出现

时间:2013-04-19 22:19:19

标签: java android draw joystick

我现在正在Android中开展一个项目,我们一直在使用一个非常棒的编辑过的操纵杆类!但是,在优化项目代码时,我在Eclipse中使用Lint来识别要清理的任何故障点。它指出我在我的onMeasured方法中为DualJoystickView类中的对象分配了内存,并建议我预先分配并重用我创建的值,因为onMeasured用于Draw。我试过这个,我附加的代码反映了这个更新。我在onMeasured方法中注释掉了旧代码。问题是,既然我正在预先分配并重新使用这些值,那么我的操纵杆就不会出现了!我读了另一篇关于在an​​droid中使用游戏杆的帖子,海报能够解决类似的问题,使用if语句查看他们的对象是否为空,以避免浪费时间在Draw中更新它们,我试过这个也无济于事。我可以通过取消注释我的旧代码来显示操纵杆,但我想以最有效的方式做到这一点。任何建议将不胜感激!

代码:

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;
    }
}

0 个答案:

没有答案