如何在屏幕的一部分上实现Android中的滑动?

时间:2014-02-20 05:23:50

标签: android

我一直在尝试在Android屏幕的一部分上实现滑动手势。我可以管理在屏幕上的任何位置检测滑动,但我似乎无法检查以确保仅在例如左边的前200个像素中进行向上/向下滑动。 以下是我希望实现Swipe手势的代码部分(由于滑动不起作用,我目前已经为机芯实现了箭头,需要通过代码替换这些代码来检测仅针对该部分的滑动屏幕)。

import java.util.List;
import android.graphics.Color;
import android.graphics.Paint;

import com.osahub.framework.Game;
import com.osahub.framework.Graphics;
import com.osahub.framework.Input.TouchEvent;
import com.osahub.framework.Screen;

public class LevelDetailsScreen extends Screen {

    public Buttons buttons[] = new Buttons[Assets.NUMBER_OF_LEVELS + 1];
    public static int counter = 1;
    public static boolean isUpEnabled = false, isDownEnabled = true;
    public int displayInfoLevelNumber = 0;

    public LevelDetailsScreen(Game game) {
        super(game);
        for (int i = 1; i <= Assets.NUMBER_OF_LEVELS; i++) {
            buttons[i] = new Buttons(10, (60 + 75 * (i - 1)));
        }
    }

    public class Buttons {
        int centerX, centerY, speedY;
        boolean movingDown = false, movingUp = false;
        public int levelNumber = counter++;

        Buttons(int centerX, int centerY) {
            setCenterX(centerX);
            setCenterY(centerY);
        }

        public void moveUp() {
            speedY = -5;
        }

        public void moveDown() {
            speedY = 5;
        }

        public void stopUp() {
            setMovingUp(false);
            stop();
        }

        public void stopDown() {
            setMovingDown(false);
            stop();
        }

        public void stop() {
            if (isMovingDown() == false && isMovingUp() == false) {
                speedY = 0;
            }

            if (isMovingDown() == false && isMovingUp() == true) {
                moveDown();
            }

            if (isMovingDown() == true && isMovingUp() == false) {
                moveUp();
            }
        }

        public void update() {
            centerY += speedY;
        }

        public void isValidMove() {
            if (centerY >= 60 + 75 * (levelNumber - 1)) {
                isUpEnabled = false;
                stopUp();
            } else {
                isUpEnabled = true;
            }
            if (centerY <= 360 + 75 * (levelNumber - 10)) {
                isDownEnabled = false;
                stopDown();
            } else {
                isDownEnabled = true;
            }
        }

        public int getCenterX() {
            return centerX;
        }

        public void setCenterX(int centerX) {
            this.centerX = centerX;
        }

        public int getCenterY() {
            return centerY;
        }

        public void setCenterY(int centerY) {
            this.centerY = centerY;
        }

        public int getSpeedY() {
            return speedY;
        }

        public void setSpeedY(int speedY) {
            this.speedY = speedY;
        }

        public boolean isMovingUp() {
            return movingUp;
        }

        public void setMovingUp(boolean movingUp) {
            this.movingUp = movingUp;
        }

        public boolean isMovingDown() {
            return movingDown;
        }

        public void setMovingDown(boolean movingDown) {
            this.movingDown = movingDown;
        }

    }

    @Override
    public void update(float deltaTime) {

        for (int i = 1; i < MainMenuScreen.levels.length; i++) {
            MainMenuScreen.levels[i] = game.retrieveLevelsData(i);
        }

        List<TouchEvent> touchEvents = game.getInput().getTouchEvents();

            // Swipe Needs to be implemented here, instead of the touch events.
        int len = touchEvents.size();
        for (int i = 0; i < len; i++) {
            TouchEvent event = touchEvents.get(i);
            if (event.type == TouchEvent.TOUCH_DOWN) {
                if (inBounds(event, 60, 8, 43, 39)) {
                    for (int j = 1; j < buttons.length; j++) {
                        buttons[j].moveUp();
                        buttons[j].setMovingUp(true);
                    }
                }
                if (inBounds(event, 60, 433, 43, 39)) {
                    for (int j = 1; j < buttons.length; j++) {
                        buttons[j].moveDown();
                        buttons[j].setMovingDown(true);
                    }
                }
                for (int j = 1; j < buttons.length; j++) {
                    if (inBounds(event, buttons[j].centerX, buttons[j].centerY, buttons[j].centerX + 151,
                            buttons[j].centerY + 60)) {
                        displayInfoLevelNumber = buttons[j].levelNumber;
                    }
                }
            }

            if (event.type == TouchEvent.TOUCH_UP) {
                if (inBounds(event, 60, 8, 43, 39)) {
                    for (int j = 1; j < buttons.length; j++) {
                        buttons[j].stopUp();
                    }
                }
                if (inBounds(event, 60, 433, 43, 39)) {
                    for (int j = 1; j < buttons.length; j++) {
                        buttons[j].stopDown();
                    }
                }
            }
        }
        for (int j = 1; j < buttons.length; j++) {
            buttons[j].update();
            buttons[j].isValidMove();
        }
    }

    private boolean inBounds(TouchEvent event, int x, int y, int width, int height) {
        if (event.x > x && event.x < x + width - 1 && event.y > y && event.y < y + height - 1)
            return true;
        else
            return false;
    }

    @Override
    public void paint(float deltaTime) {
        // drawing things on screen
}

    @Override
    public void pause() {
    }

    @Override
    public void resume() {

    }

    @Override
    public void dispose() {

    }

    @Override
    public void backButton() {
        counter = 1;
        game.setScreen(new LevelSelectionScreen(game));
    }
    }

扩展文件屏幕是一个进一步调用AndroidGame类的接口。 AndroidGame如下:

package com.osahub.framework.implementation;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedIns

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.view.Window;
import android.view.WindowManager;

import com.osahub.framework.Audio;
import com.osahub.framework.FileIO;
import com.osahub.framework.Game;
import com.osahub.framework.Graphics;
import com.osahub.framework.Input;
import com.osahub.framework.Screen;
import com.osahub.robotgame.Assets;
import com.osahub.robotgame.Level;
import com.osahub.robotgame.MainMenuScreen;

public abstract class AndroidGame extends Activity implements Game {
    AndroidFastRenderView renderView;
    Graphics graphics;
    Audio audio;
    Input input;
    FileIO fileIO;
    Screen screen;
    WakeLock wakeLock;

@OverridetanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    boolean isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
    int frameBufferWidth = isPortrait ? 480 : 800;
    int frameBufferHeight = isPortrait ? 800 : 480;
    Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth, frameBufferHeight, Config.RGB_565);

    float scaleX = (float) frameBufferWidth / getWindowManager().getDefaultDisplay().getWidth();
    float scaleY = (float) frameBufferHeight / getWindowManager().getDefaultDisplay().getHeight();

    renderView = new AndroidFastRenderView(this, frameBuffer);
    graphics = new AndroidGraphics(getAssets(), frameBuffer);
    fileIO = new AndroidFileIO(this);
    audio = new AndroidAudio(this);
    input = new AndroidInput(this, renderView, scaleX, scaleY);
    screen = getInitScreen();
    setContentView(renderView);

    PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "MyGame");

}

@Override
public void onResume() {
    super.onResume();
    wakeLock.acquire();
    screen.resume();
    renderView.resume();
}

@Override
public void onPause() {
    super.onPause();
    wakeLock.release();
    renderView.pause();
    screen.pause();

    if (isFinishing())
        screen.dispose();
}

@Override
public Input getInput() {
    return input;
}

@Override
public FileIO getFileIO() {
    return fileIO;
}

@Override
public Graphics getGraphics() {
    return graphics;
}

@Override
public Audio getAudio() {
    return audio;
}

@Override
public void setScreen(Screen screen) {
    if (screen == null)
        throw new IllegalArgumentException("Screen must not be null");

    this.screen.pause();
    this.screen.dispose();
    screen.resume();
    screen.update(0);
    this.screen = screen;
}

public Screen getCurrentScreen() {
    return screen;
}
}

任何帮助将不胜感激。非常感谢..

1 个答案:

答案 0 :(得分:0)

您可以在想要获得触摸列表的区域设置transperent sprite或图像,并通过以下方式监听她的触摸 image.setonTouchListener()

希望这有帮助