public class ScaleTouchListener implements View.OnTouchListener { @SuppressLint("ClickableViewAccessibility") @Override public boolean onTouch(View view, MotionEvent motionEvent) { switch (motionEvent.getAction()) { case MotionEvent.ACTION_DOWN: view.setScaleX(0.9f); view.setScaleY(0.9f); break; case MotionEvent.ACTION_UP: view.setScaleX(1); view.setScaleY(1); break; default: } return false; } }
时间: 2024-02-10 17:11:11 浏览: 146
这段代码是一个实现了缩放效果的触摸监听器类,它可以被添加到一个视图上,当用户点击或触摸这个视图时,该视图会缩小一定比例,当用户松开手指时,视图会恢复原始大小。具体来说,它重写了OnTouchListener接口的onTouch()方法,当用户按下时,会调用ACTION_DOWN分支,将视图的缩放比例设置为0.9f,当用户松开手指时,会调用ACTION_UP分支,将视图的缩放比例设置为1,表示恢复原始大小。最后,该方法返回false,表示事件未被消费,也就是其他的触摸监听器或视图可以继续处理该事件。
相关问题
package com.blog.demo11; import android.annotation.SuppressLint; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.widget.ImageView; import androidx.annotation.Nullable; import com.blog.BaseActivity; import com.blog.R; import static android.view.MotionEvent.INVALID_POINTER_ID; /** * 可拖拽效果类。 */ public class ViewDragActivity extends BaseActivity { private ImageView backgroundImage; private ImageView image1; private ImageView image2; @SuppressLint("MissingInflatedId") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); backgroundImage = findViewById(R.id.background_image); image1 = findViewById(R.id.image1); image2 = findViewById(R.id.image2); // Set touch listeners for each image image1.setOnTouchListener(new MultiTouchListener()); image2.setOnTouchListener(new MultiTouchListener()); } private class MultiTouchListener implements View.OnTouchListener { private float lastTouchX, lastTouchY; private int activePointerId = INVALID_POINTER_ID; @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: { final int pointerIndex = event.getActionIndex(); final float x = event.getX(pointerIndex); final float y = event.getY(pointerIndex); lastTouchX = x; lastTouchY = y; activePointerId = event.getPointerId(pointerIndex); break; } case MotionEvent.ACTION_MOVE: { final int pointerIndex = event.findPointerIndex(activePointerId); final float x = event.getX(pointerIndex); final float y = event.getY(pointerIndex); final float dx = x - lastTouchX; final float dy = y - lastTouchY; v.setX(v.getX() + dx); v.setY(v.getY() + dy); lastTouchX = x; lastTouchY = y; break; } case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: { activePointerId = INVALID_POINTER_ID; break; } case MotionEvent.ACTION_POINTER_UP: { final int pointerIndex = event.getActionIndex(); final int pointerId = event.getPointerId(pointerIndex); if (pointerId == activePointerId) { final int newPointerIndex = pointerIndex == 0 ? 1 : 0; lastTouchX = event.getX(newPointerIndex); lastTouchY = event.getY(newPointerIndex); activePointerId = event.getPointerId(newPointerIndex); } break; } } return true; } } }
这是一段 Android 应用程序代码,实现了一个可拖拽效果类 ViewDragActivity。在该类中,通过监听 onTouch 事件来实现多指触摸拖拽图片的效果,其中 MultiTouchListener 类实现了 View.OnTouchListener 接口,重写了 onTouch 方法,通过获取 MotionEvent 的不同事件来实现图片的移动。具体实现过程可以参考代码中的注释。
activity ontouchlistener如何使用
在Android中,可以使用以下步骤来使用activity ontouchlistener。
1.创建一个新的Activity类并实现OnTouchListener接口
```java
public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
```
2.重写onTouch方法,并在其中添加触摸事件的代码
```java
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// 手指按下时的操作
break;
case MotionEvent.ACTION_MOVE:
// 手指移动时的操作
break;
case MotionEvent.ACTION_UP:
// 手指抬起时的操作
break;
}
return true;
}
```
3.在onCreate方法中将Activity设置为Touch事件的监听器
```java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 将Activity设置为Touch事件的监听器
getWindow().getDecorView().setOnTouchListener(this);
}
```
4.在onDestroy方法中移除Activity的Touch事件监听器
```java
@Override
protected void onDestroy() {
super.onDestroy();
// 移除Activity的Touch事件监听器
getWindow().getDecorView().setOnTouchListener(null);
}
```
这样,你就可以在Activity中使用ontouchlistener了。
阅读全文