我编码的特殊方法疑问请解释

时间:2011-02-28 12:54:20

标签: java android

现在我发布了我的完整编码....我对某些方法有疑问,你可以解释这个方法......请...

完整编码:

package com.momojo.gba.input;

import com.androidemu.Emulator;
import com.momojo.gba.R;

import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public abstract class AbstractMultitouchPad implements MultitouchPad
{
public final Paint       myPaint      = new Paint(Paint.ANTI_ALIAS_FLAG);
public int orientation = Configuration.ORIENTATION_UNDEFINED;

public VirtualButton     aButton      = null;
public VirtualButton     bButton      = null;
public VirtualButton     selectButton = null;
public VirtualButton     startButton  = null;
public VirtualButton     leftButton  = null;
public VirtualButton     rightButton  = null;
public VirtualButton     dpadButton   = null;

VirtualButton[]          buttons      = new VirtualButton[7];
private static final int DEAD_ZONE    = 20;
private static final int DPAD_INDEX = 6;
private View             view         = null;
private int[]            KEYVALS      = new int[] { Emulator.GAMEPAD_A,
        Emulator.GAMEPAD_B, Emulator.GAMEPAD_LEFT, Emulator.GAMEPAD_RIGHT, Emulator.GAMEPAD_SELECT, Emulator.GAMEPAD_START };

private GameKeyListener  gameKeyListener;
private int              keyStates;

public AbstractMultitouchPad(Resources resources, View view)
{
    myPaint.setColor(0xFFFF0000);
    myPaint.setAlpha(100);
    setView(view);
    aButton = new VirtualButton(resources, R.drawable.a);
    bButton = new VirtualButton(resources, R.drawable.b);
    selectButton = new VirtualButton(resources, R.drawable.start);
    startButton = new VirtualButton(resources, R.drawable.start);
    dpadButton = new VirtualButton(resources, R.drawable.dpad);
    leftButton = new VirtualButton(resources, R.drawable.l);
    rightButton = new VirtualButton(resources, R.drawable.r);

    buttons[0] = aButton;
    buttons[1] = bButton;
    buttons[2] = leftButton;
    buttons[3] = rightButton;
    buttons[4] = selectButton;
    buttons[5] = startButton;
    buttons[6] = dpadButton;
}

public void setView(View view)
{
    this.view = view;
}

@Override
public abstract void draw(View view, Canvas canvas);

public boolean handleEvent(MotionEvent event)
{

    int action = event.getAction();
    int key = 0;
    int actionCode = action & MotionEvent.ACTION_MASK;

    int index = 0;

    if (actionCode == MotionEvent.ACTION_POINTER_DOWN
                || actionCode == MotionEvent.ACTION_POINTER_UP)
    {

        index = action >> MotionEvent.ACTION_POINTER_ID_SHIFT;
    }


   key = getGameKey(event.getX(index), event.getY(index));

    switch (actionCode)
    {
    case MotionEvent.ACTION_POINTER_DOWN:            
    case MotionEvent.ACTION_DOWN:
    case MotionEvent.ACTION_MOVE:
        keyStates |= key;
        break;
    case MotionEvent.ACTION_POINTER_UP:
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_OUTSIDE:
    case MotionEvent.ACTION_CANCEL:
    {          
        keyStates &= ~key;            
        break;
    }
    default:
    {            
       return false;
    }
    }

    gameKeyListener.onGameKeyChanged();
    return true;

}

private int getGameKey(float x, float y)
{
    int key = 0;
    boolean foundKey = false;

    for (int j = 0; j < buttons.length - 1 && !foundKey; j++)
    {
        if (buttons[j].isHit(x, y))
        {
            key = KEYVALS[j];
            foundKey = true;
        }
    }

    if (!foundKey && buttons[DPAD_INDEX].isHit(x, y))
    {  
        key = getDpadKey(x, y);
        foundKey = true;
    }

    if(!foundKey )
    {
        if((keyStates & Emulator.GAMEPAD_A) == Emulator.GAMEPAD_A)
        {
            keyStates &= ~Emulator.GAMEPAD_A;
        }
        if((keyStates & Emulator.GAMEPAD_B) == Emulator.GAMEPAD_B)
        {
            keyStates &= ~Emulator.GAMEPAD_B;
        }
    }

   return key;

}

private int getDpadKey(float fx, float fy)
{
    final int x = (int) (fx + 0.5f);
    final int y = (int) (fy + 0.5f);
    final int cx = (int) dpadButton.x + dpadButton.bitmap.getWidth() / 2;
    final int cy = (int) dpadButton.y + dpadButton.bitmap.getHeight() / 2;

    int key = 0;
    if (x < cx - DEAD_ZONE && x > dpadButton.x)
    {
        if((keyStates & Emulator.GAMEPAD_RIGHT) == Emulator.GAMEPAD_RIGHT)
        {
            keyStates &= ~Emulator.GAMEPAD_RIGHT;
        }
        key |= Emulator.GAMEPAD_LEFT;                        
    }
    else if (x > cx + DEAD_ZONE && x < dpadButton.x2)
    {
        if((keyStates & Emulator.GAMEPAD_LEFT) == Emulator.GAMEPAD_LEFT)
        {
            keyStates &= ~Emulator.GAMEPAD_LEFT;
        }
        key |= Emulator.GAMEPAD_RIGHT;            
    }
    if (y < cy - DEAD_ZONE && y > dpadButton.y)
    {
        if((keyStates & Emulator.GAMEPAD_DOWN) == Emulator.GAMEPAD_DOWN)
        {
            keyStates &= ~Emulator.GAMEPAD_DOWN;
        }
        key |= Emulator.GAMEPAD_UP;
    }
    else if (y > cy + DEAD_ZONE && y < dpadButton.y2)
    {
        if((keyStates & Emulator.GAMEPAD_UP) == Emulator.GAMEPAD_UP)
        {
            keyStates &= ~Emulator.GAMEPAD_UP;
        }
        key |= Emulator.GAMEPAD_DOWN;
    }
    return key;
}

public void setGameKeyListener(GameKeyListener listener)
{
    gameKeyListener = listener;
}

public int getKeyStates()
{
    return keyStates;
}

private void setKeyStates(int newStates)
{
    if (keyStates != newStates)
    {
        keyStates = newStates;
        gameKeyListener.onGameKeyChanged();
    }
}

public void reset()
{
    keyStates = 0;
}

public void setOrientation(int orientation)
{
    this.orientation = orientation;
}

@Override
public void setTransparency(int level)
{
    myPaint.setAlpha((int)((level/100f) * 255));

}  

}

疑惑:

    public AbstractMultitouchPad(Resources resources, View view)
{
    myPaint.setColor(0xFFFF0000);
    myPaint.setAlpha(100);
    setView(view);
    aButton = new VirtualButton(resources, R.drawable.a);
    bButton = new VirtualButton(resources, R.drawable.b);
    selectButton = new VirtualButton(resources, R.drawable.start);
    startButton = new VirtualButton(resources, R.drawable.start);
    dpadButton = new VirtualButton(resources, R.drawable.dpad);
    leftButton = new VirtualButton(resources, R.drawable.l);
    rightButton = new VirtualButton(resources, R.drawable.r);

    buttons[0] = aButton;
    buttons[1] = bButton;
    buttons[2] = leftButton;
    buttons[3] = rightButton;
    buttons[4] = selectButton;
    buttons[5] = startButton;
    buttons[6] = dpadButton;
}

public void setView(View view)
{
    this.view = view;
}

@Override
public abstract void draw(View view, Canvas canvas);

public boolean handleEvent(MotionEvent event)
{

    int action = event.getAction();
    int key = 0;
    int actionCode = action & MotionEvent.ACTION_MASK;

    int index = 0;

    if (actionCode == MotionEvent.ACTION_POINTER_DOWN
                || actionCode == MotionEvent.ACTION_POINTER_UP)
    {

        index = action >> MotionEvent.ACTION_POINTER_ID_SHIFT;
    }


   key = getGameKey(event.getX(index), event.getY(index));

    switch (actionCode)
    {
    case MotionEvent.ACTION_POINTER_DOWN:            
    case MotionEvent.ACTION_DOWN:
    case MotionEvent.ACTION_MOVE:
        keyStates |= key;
        break;
    case MotionEvent.ACTION_POINTER_UP:
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_OUTSIDE:
    case MotionEvent.ACTION_CANCEL:
    {          
        keyStates &= ~key;            
        break;
    }
    default:
    {            
       return false;
    }
    }

    gameKeyListener.onGameKeyChanged();
    return true;

}

private int getGameKey(float x, float y)
{
    int key = 0;
    boolean foundKey = false;

    for (int j = 0; j < buttons.length - 1 && !foundKey; j++)
    {
        if (buttons[j].isHit(x, y))
        {
            key = KEYVALS[j];
            foundKey = true;
        }
    }

    if (!foundKey && buttons[DPAD_INDEX].isHit(x, y))
    {  
        key = getDpadKey(x, y);
        foundKey = true;
    }

    if(!foundKey )
    {
        if((keyStates & Emulator.GAMEPAD_A) == Emulator.GAMEPAD_A)
        {
            keyStates &= ~Emulator.GAMEPAD_A;
        }
        if((keyStates & Emulator.GAMEPAD_B) == Emulator.GAMEPAD_B)
        {
            keyStates &= ~Emulator.GAMEPAD_B;
        }
    }

   return key;

}

private int getDpadKey(float fx, float fy)
{
    final int x = (int) (fx + 0.5f);
    final int y = (int) (fy + 0.5f);
    final int cx = (int) dpadButton.x + dpadButton.bitmap.getWidth() / 2;
    final int cy = (int) dpadButton.y + dpadButton.bitmap.getHeight() / 2;

    int key = 0;
    if (x < cx - DEAD_ZONE && x > dpadButton.x)
    {
        if((keyStates & Emulator.GAMEPAD_RIGHT) == Emulator.GAMEPAD_RIGHT)
        {
            keyStates &= ~Emulator.GAMEPAD_RIGHT;
        }
        key |= Emulator.GAMEPAD_LEFT;                        
    }
    else if (x > cx + DEAD_ZONE && x < dpadButton.x2)
    {
        if((keyStates & Emulator.GAMEPAD_LEFT) == Emulator.GAMEPAD_LEFT)
        {
            keyStates &= ~Emulator.GAMEPAD_LEFT;
        }
        key |= Emulator.GAMEPAD_RIGHT;            
    }
    if (y < cy - DEAD_ZONE && y > dpadButton.y)
    {
        if((keyStates & Emulator.GAMEPAD_DOWN) == Emulator.GAMEPAD_DOWN)
        {
            keyStates &= ~Emulator.GAMEPAD_DOWN;
        }
        key |= Emulator.GAMEPAD_UP;
    }
    else if (y > cy + DEAD_ZONE && y < dpadButton.y2)
    {
        if((keyStates & Emulator.GAMEPAD_UP) == Emulator.GAMEPAD_UP)
        {
            keyStates &= ~Emulator.GAMEPAD_UP;
        }
        key |= Emulator.GAMEPAD_DOWN;
    }
    return key;
}

1 个答案:

答案 0 :(得分:3)

你真的不是在问什么,但是,现在,

public AbstractMultitouchPad(Resources resources, View view)

此方法设置一些变量,如颜色,并设置一些按钮。

public void setView(View view)

view变量是私有的,所以你需要一个setter来从'outside'改变它

@Override
public abstract void draw(View view, Canvas canvas);

这意味着扩展此抽象类的所有实际类都必须实现此方法。

public boolean handleEvent(MotionEvent event)

处理事件

private int getGameKey(float x, float y)

似乎返回按下的键。

private int getDpadKey(float fx, float fy)

似乎返回了在dpad上按下的键

 public void setGameKeyListener(GameKeyListener listener)

就像setView一样,如果你想指定一个监听器,你需要这个功能,因为你不能从外面改变监听器。所以基本上就是方法名称所说的:)