0

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

代码:

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

0 回答 0