0
我一直在嘗試只在屏幕上的一部分在Android上實施滑動手勢。我可以管理檢測屏幕上任何地方的滑動,但似乎無法進行檢查,以確保只向上/向下滑動,例如,從左側的前200個像素處發生。 以下是我希望在其中實施滑動手勢的代碼部分(由於滑動不起作用,我目前已經爲移動實現了箭頭,需要用代碼替換這些代碼以僅檢測滑動屏幕)。如何在Android屏幕上僅實現輕掃一下?
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));
}
}
擴展文件Screen是進一步調用類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;
}
}
任何幫助,將不勝感激。非常感謝..
是感謝,多數民衆贊成在一個好主意,我會嘗試和實施.. – Rachit
如果它是有用的,你可以接受的答案! – Pramod
雖然實際上不能實現它。但是,無論如何謝謝.. – Rachit