Android 如何正确使用OnTouchEvent?
当用户触摸屏幕的右侧或左侧时,如何使用Android 如何正确使用OnTouchEvent?,android,ontouchlistener,touch-event,ontouch,Android,Ontouchlistener,Touch Event,Ontouch,当用户触摸屏幕的右侧或左侧时,如何使用ontochevent功能使ImageView向右或向左移动?下面的代码是我尝试过的。我能做些什么来改进它并使其工作 ImageView circle1 = (ImageView) findViewById(R.id.circle1); public boolean onTouchEvent(MotionEvent MotionEvent motionEvent; motionEvent){ switch (motionEvent
ontochevent
功能使ImageView
向右或向左移动?下面的代码是我尝试过的。我能做些什么来改进它并使其工作
ImageView circle1 = (ImageView) findViewById(R.id.circle1);
public boolean onTouchEvent(MotionEvent MotionEvent motionEvent;
motionEvent){
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
// Player has touched the screen
case MotionEvent.ACTION_DOWN:
if (motionEvent.getY() > screenHeight - screenWidth / 8) {
circleIsMovingRIGHT = true;
circleIsMovingLEFT = false;
} else {
circleIsMovingRIGHT = false;
circleIsMovingLEFT = true;
}
break;
}
return true;
}
}
试试这个
@SuppressLint("ClickableViewAccessibility")
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
anyView.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (v.getId()) {
case R.id.anyView:
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// perform here
}
break;
}
return false;
}
在res/anim/rotate.xml中添加此代码
<?xml version="1.0" encoding="UTF-8"?>
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:duration="1200"
android:interpolator="@android:anim/linear_interpolator"/>
如果您使用android:repeatCount=“infinite”,那么您将动态停止旋转,并在特定条件下应用此代码
imageView.clearAnimation();
太棒了,谢谢!最后一个问题,如何使图像移动?我应该把什么代码放在//perform here注释的位置?您在那里实际执行了什么,请详细告诉我。你的意思是,当用户单击ImageView时,它会在那里旋转。我想执行的是,当用户触摸设备屏幕的左侧或右侧时,图像会相应地向左或向右移动。因此,使用FrameLayout并设置ImageView,linearlayout set weightsum为1,orientation horizontal,此布局在设置其他两个布局中,并将权重设置为0.5,当您单击布局左侧时,显示图像位置-1,当您单击布局右侧时,将图像位置设置为+1。。我希望这位前任能对你有所帮助。
imageView.clearAnimation();