如何根据用户的触摸移动图像?
How can I move an image based on a user's touch?
我试图让图像在用户触摸设备屏幕的左侧或右侧时向左或向右移动。我有以下代码....我有 运行 模拟器,在 Android Studio 中,当我单击模拟器屏幕的右侧或左侧时......没有任何反应。这段代码有什么问题?欢迎大家回答!我在包含我要移动的图像的 Activity 中输入了以下代码:
public class GameScreen1 extends AppCompatActivity implements View.OnTouchListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_screen1);
ImageView circle1 = (ImageView) findViewById(R.id.circle1);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (v.getId()) {
case R.id.circle1:
if (event.getAction() == MotionEvent.ACTION_DOWN) {
//WHAT CODE SHOULD I PUT INSTEAD OF THE FLOAT X AND X++
int ScreenWidth = getResources().getDisplayMetrics().widthPixels;
float Xtouch = event.getRawX();
int sign = Xtouch > 0.5*ScreenWidth ? 1 : -1;
float XToMove = 50;
int durationMs = 50;
v.animate().translationXBy(sign*XToMove).setDuration(durationMs);
}
break;
}
return false;
}
}
将 ID 添加到 activity 中的根布局并在其上添加 TouchListener。
这是一个例子:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/cl_root"
android:layout_height="match_parent"
tools:context=".MainActivity">
</android.support.constraint.ConstraintLayout>
这是您 activity 的代码:
public class MainActivity extends AppCompatActivity {
ConstraintLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = findViewById(R.id.cl_root);
layout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int screenWidth = getResources().getDisplayMetrics().widthPixels;
int x = (int)event.getX();
if ( x >= ( screenWidth/2) ) {
//Right touch
}else {
//Left touch
}
return false;
}
});
}
}
我试图让图像在用户触摸设备屏幕的左侧或右侧时向左或向右移动。我有以下代码....我有 运行 模拟器,在 Android Studio 中,当我单击模拟器屏幕的右侧或左侧时......没有任何反应。这段代码有什么问题?欢迎大家回答!我在包含我要移动的图像的 Activity 中输入了以下代码:
public class GameScreen1 extends AppCompatActivity implements View.OnTouchListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_screen1);
ImageView circle1 = (ImageView) findViewById(R.id.circle1);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (v.getId()) {
case R.id.circle1:
if (event.getAction() == MotionEvent.ACTION_DOWN) {
//WHAT CODE SHOULD I PUT INSTEAD OF THE FLOAT X AND X++
int ScreenWidth = getResources().getDisplayMetrics().widthPixels;
float Xtouch = event.getRawX();
int sign = Xtouch > 0.5*ScreenWidth ? 1 : -1;
float XToMove = 50;
int durationMs = 50;
v.animate().translationXBy(sign*XToMove).setDuration(durationMs);
}
break;
}
return false;
}
}
将 ID 添加到 activity 中的根布局并在其上添加 TouchListener。
这是一个例子:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/cl_root"
android:layout_height="match_parent"
tools:context=".MainActivity">
</android.support.constraint.ConstraintLayout>
这是您 activity 的代码:
public class MainActivity extends AppCompatActivity {
ConstraintLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = findViewById(R.id.cl_root);
layout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int screenWidth = getResources().getDisplayMetrics().widthPixels;
int x = (int)event.getX();
if ( x >= ( screenWidth/2) ) {
//Right touch
}else {
//Left touch
}
return false;
}
});
}
}