让图像出现在用户点击的地方
Make image go where user taps
对于一个 android 应用程序即时编码,我正在努力做到这一点,因此当用户点击屏幕时,"player" 图像会滑过,直到它达到该 x 值
我尝试在玩家触摸 MotionEvent 中的屏幕时调用此方法
public void movementUpdate(int x){
if(goalX < x){
goalX -= width;
}
if(goalX > x){
goalX += width;
}
goalX = x;
}
然后在我调用的更新方法中
public void update(){
if(goalX > x)
x += 4;
if(goalX < x)
x -= 4;
}
但图像总是超过它或不够远
此外,由于当您点击图像前面时图像是从左角 java 绘制的,您是否必须将宽度减去 1/2 以便它停在中间而不是边缘,并且如果你也点击后面的负 1/2?
我做了一个基本结构供你使用。如果您粘贴代码,您可能必须添加包名称、更改 activity 名称、更改布局名称,并将名为 grey_star 的 png 图像添加到可绘制文件夹。
import android.os.CountDownTimer;
import android.provider.ContactsContract;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
//the vars that contains the users touch coords, I set some place holders the disapear as soon
//as the user touches the screen
private float touchX = 100;
private float touchY = 100;
//number of pixels to be moved every tick you should
//calculate them based on the screen size in a setup method
private int xSpeed = 10;
private int ySpeed = 10;
//bounds of the screen
private int leftBound;
private int rightBound;
private int topBound;
private int bottomBound;
//screen dimensions
private float pxScreenWidth;
private float pxScreenHeight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//make the scree go fullscreen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//remove action bar, and set content view
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
//obtain the width and height of the screen in pixels
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
pxScreenHeight = displayMetrics.heightPixels;
pxScreenWidth = displayMetrics.widthPixels;
//set bounds
leftBound = 0;
rightBound = (int) pxScreenWidth;
topBound = 0;
bottomBound = (int) pxScreenHeight;
mainGame();
}
private void mainGame() {
//some kind of timer I am lazy and used the countdown for this example
//it does something every millisecond
//first value is number of ticks second is the size 1 = 1ms 1000=1s
new CountDownTimer(1000000, 1) {
public void onTick(long millisUntilFinished) {
//get the image view
ImageView thing = (ImageView) findViewById(R.id.thing);
int x = (int) thing.getX();
int y = (int) thing.getY();
moveThing(x, y, (int) touchX, (int) touchY);
}
public void onFinish() {
}
}.start();
}
private void moveThing(int xCoord, int yCoord, int targetX, int targetY) {
//adjust these values to center
ImageView thing = (ImageView) findViewById(R.id.thing);
targetX = targetX - thing.getWidth() / 2;
targetY = targetY - thing.getHeight() / 2;
int width = thing.getWidth();
int height = thing.getHeight();
//for recording intended position
int tempX = 0;
int tempY = 0;
//check so don't run if don't have to.
if (xCoord != targetX) {
boolean lessX = false;
//set value to move forward
int x = xCoord + xSpeed;
//but if has to move backwards set the value to move backwards
if (targetX < xCoord) {
x = xCoord - xSpeed;
lessX = true;
}
//if the amount of pixes goes over the target set it to the target
if (lessX == false && x > targetX) {
x = targetX;
} else if (lessX == true && x < targetX) {
x = targetX;
}
//check x bounds
int temp = checkXBounds(x, width, leftBound, rightBound);
if(temp != -1){
x = temp;
}
//draw the thing in the new location
if (xCoord < targetX || (xCoord > targetX && lessX == true)) {
thing.setX(x);
}
tempX = (int) x;
}
//check so don't run if don't have to.
if (yCoord != targetY) {
//set value to move forward
int y = yCoord + ySpeed;
boolean lessY = false;
//but if has to move backwards set the value to move backwards
if (targetY < yCoord) {
y = yCoord - ySpeed;
lessY = true;
}
//if the amount of pixes goes over the target set it to the target
if (y > targetY && lessY == false) {
y = targetY;
} else if (y < targetY && lessY == true) {
y = targetY;
}
//check y bounds
int temp = checkYBounds(y, topBound, bottomBound, height);
if(temp != -1){
y = temp;
}
//draw the thing in the new location
if (yCoord < targetY || (yCoord > targetY && lessY == true)) {
thing.setY(y);
}
tempY = (int) y;
}
TextView textView = (TextView) findViewById(R.id.coords);
textView.setText("x: " + tempX + " " + "y: " + tempY);
}
private int checkXBounds(int xCoord, int width, int leftBound, int rightBound){
if(checkLeftBound(xCoord, leftBound) == false){
return leftBound;
}
if(checkRightBound(xCoord, rightBound, width) == false){
return rightBound - width;
}
return -1;
}
private int checkYBounds(int yCoord, int topBound, int bottomBound, int height){
if(checkTopBound(yCoord, topBound) == false){
return topBound;
}
if(checkBottomBound(yCoord, bottomBound, height) == false){
return bottomBound - height;
}
return -1;
}
private boolean checkLeftBound(int xCoord, int leftBound) {
if(xCoord < leftBound){
return false;
}
return true;
}
private boolean checkRightBound(int xCoord, int rightBound, int width){
if(xCoord + width > rightBound){
return false;
}
return true;
}
private boolean checkTopBound(int yCoord, int topBound){
if(yCoord < topBound){
return false;
}
return true;
}
private boolean checkBottomBound(int yCoord, int bottomBound, int height){
if(yCoord + height > bottomBound){
return false;
}
return true;
}
//gets touch coordinates and handles moving the ship
@Override
public boolean onTouchEvent(MotionEvent event) {
//get the touch coordinates
touchX = event.getX();
touchY = event.getY();
return super.onTouchEvent(event);
}
}
这是 XML 文件。在真正的游戏中,我不会这样造船。您需要稍微更改 XML 才能使其正常工作。此外,您将希望以不同的方式设置图像视图的大小,因为像我这样对值进行硬编码是不好的。我这样做是为了速度。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.honeymanappdev.bradleyhoneyman.moveaimageviewbasedonusertouch.MainActivity">
<ImageView
android:id="@+id/thing"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/grey_star"/>
<TextView
android:id="@+id/coords"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"/>
我建议使用其他方式加载位图。
我读了这篇文章,并推荐使用 Picasso 来加载您的位图。这是一项了不起的服务。而且它是开源的。
对于一个 android 应用程序即时编码,我正在努力做到这一点,因此当用户点击屏幕时,"player" 图像会滑过,直到它达到该 x 值
我尝试在玩家触摸 MotionEvent 中的屏幕时调用此方法
public void movementUpdate(int x){
if(goalX < x){
goalX -= width;
}
if(goalX > x){
goalX += width;
}
goalX = x;
}
然后在我调用的更新方法中
public void update(){
if(goalX > x)
x += 4;
if(goalX < x)
x -= 4;
}
但图像总是超过它或不够远
此外,由于当您点击图像前面时图像是从左角 java 绘制的,您是否必须将宽度减去 1/2 以便它停在中间而不是边缘,并且如果你也点击后面的负 1/2?
我做了一个基本结构供你使用。如果您粘贴代码,您可能必须添加包名称、更改 activity 名称、更改布局名称,并将名为 grey_star 的 png 图像添加到可绘制文件夹。
import android.os.CountDownTimer;
import android.provider.ContactsContract;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
//the vars that contains the users touch coords, I set some place holders the disapear as soon
//as the user touches the screen
private float touchX = 100;
private float touchY = 100;
//number of pixels to be moved every tick you should
//calculate them based on the screen size in a setup method
private int xSpeed = 10;
private int ySpeed = 10;
//bounds of the screen
private int leftBound;
private int rightBound;
private int topBound;
private int bottomBound;
//screen dimensions
private float pxScreenWidth;
private float pxScreenHeight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//make the scree go fullscreen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//remove action bar, and set content view
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
//obtain the width and height of the screen in pixels
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
pxScreenHeight = displayMetrics.heightPixels;
pxScreenWidth = displayMetrics.widthPixels;
//set bounds
leftBound = 0;
rightBound = (int) pxScreenWidth;
topBound = 0;
bottomBound = (int) pxScreenHeight;
mainGame();
}
private void mainGame() {
//some kind of timer I am lazy and used the countdown for this example
//it does something every millisecond
//first value is number of ticks second is the size 1 = 1ms 1000=1s
new CountDownTimer(1000000, 1) {
public void onTick(long millisUntilFinished) {
//get the image view
ImageView thing = (ImageView) findViewById(R.id.thing);
int x = (int) thing.getX();
int y = (int) thing.getY();
moveThing(x, y, (int) touchX, (int) touchY);
}
public void onFinish() {
}
}.start();
}
private void moveThing(int xCoord, int yCoord, int targetX, int targetY) {
//adjust these values to center
ImageView thing = (ImageView) findViewById(R.id.thing);
targetX = targetX - thing.getWidth() / 2;
targetY = targetY - thing.getHeight() / 2;
int width = thing.getWidth();
int height = thing.getHeight();
//for recording intended position
int tempX = 0;
int tempY = 0;
//check so don't run if don't have to.
if (xCoord != targetX) {
boolean lessX = false;
//set value to move forward
int x = xCoord + xSpeed;
//but if has to move backwards set the value to move backwards
if (targetX < xCoord) {
x = xCoord - xSpeed;
lessX = true;
}
//if the amount of pixes goes over the target set it to the target
if (lessX == false && x > targetX) {
x = targetX;
} else if (lessX == true && x < targetX) {
x = targetX;
}
//check x bounds
int temp = checkXBounds(x, width, leftBound, rightBound);
if(temp != -1){
x = temp;
}
//draw the thing in the new location
if (xCoord < targetX || (xCoord > targetX && lessX == true)) {
thing.setX(x);
}
tempX = (int) x;
}
//check so don't run if don't have to.
if (yCoord != targetY) {
//set value to move forward
int y = yCoord + ySpeed;
boolean lessY = false;
//but if has to move backwards set the value to move backwards
if (targetY < yCoord) {
y = yCoord - ySpeed;
lessY = true;
}
//if the amount of pixes goes over the target set it to the target
if (y > targetY && lessY == false) {
y = targetY;
} else if (y < targetY && lessY == true) {
y = targetY;
}
//check y bounds
int temp = checkYBounds(y, topBound, bottomBound, height);
if(temp != -1){
y = temp;
}
//draw the thing in the new location
if (yCoord < targetY || (yCoord > targetY && lessY == true)) {
thing.setY(y);
}
tempY = (int) y;
}
TextView textView = (TextView) findViewById(R.id.coords);
textView.setText("x: " + tempX + " " + "y: " + tempY);
}
private int checkXBounds(int xCoord, int width, int leftBound, int rightBound){
if(checkLeftBound(xCoord, leftBound) == false){
return leftBound;
}
if(checkRightBound(xCoord, rightBound, width) == false){
return rightBound - width;
}
return -1;
}
private int checkYBounds(int yCoord, int topBound, int bottomBound, int height){
if(checkTopBound(yCoord, topBound) == false){
return topBound;
}
if(checkBottomBound(yCoord, bottomBound, height) == false){
return bottomBound - height;
}
return -1;
}
private boolean checkLeftBound(int xCoord, int leftBound) {
if(xCoord < leftBound){
return false;
}
return true;
}
private boolean checkRightBound(int xCoord, int rightBound, int width){
if(xCoord + width > rightBound){
return false;
}
return true;
}
private boolean checkTopBound(int yCoord, int topBound){
if(yCoord < topBound){
return false;
}
return true;
}
private boolean checkBottomBound(int yCoord, int bottomBound, int height){
if(yCoord + height > bottomBound){
return false;
}
return true;
}
//gets touch coordinates and handles moving the ship
@Override
public boolean onTouchEvent(MotionEvent event) {
//get the touch coordinates
touchX = event.getX();
touchY = event.getY();
return super.onTouchEvent(event);
}
}
这是 XML 文件。在真正的游戏中,我不会这样造船。您需要稍微更改 XML 才能使其正常工作。此外,您将希望以不同的方式设置图像视图的大小,因为像我这样对值进行硬编码是不好的。我这样做是为了速度。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.honeymanappdev.bradleyhoneyman.moveaimageviewbasedonusertouch.MainActivity">
<ImageView
android:id="@+id/thing"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/grey_star"/>
<TextView
android:id="@+id/coords"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"/>
我建议使用其他方式加载位图。
我读了这篇文章,并推荐使用 Picasso 来加载您的位图。这是一项了不起的服务。而且它是开源的。