gpt4 book ai didi

android - 为 ImageView 实现 GestureDetector

转载 作者:太空宇宙 更新时间:2023-11-03 13:25:55 25 4
gpt4 key购买 nike

早安:

我的 Activity 中有一个 ImageView,我设置到我的 ImageView 的 OntOuchListner(),这意味着我像这样为我的 ImageView 实现了该接口(interface):

public class mapActivity extends Activity
{

//--------------------------------------
private ImageView imageView;

/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


setContentView(R.layout.main);
imageView = (ImageView) this.findViewById(R.id.imageView1);

AddImageViewEvents();
}
private void AddImageViewEvents() {


imageView.setOnTouchListener(new ImageView.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
int action, pointerCount, i;
action = event.getAction();
pointerCount = event.getPointerCount();

switch (action) {
case MotionEvent.ACTION_DOWN:
//code here
break;

case MotionEvent.ACTION_MOVE:
//code here
break;

default:
break;
}

return true;
}

}

我想以同样的方式将 GestureDetector 设置到我的 ImageView,我不知道这是否可行。

谢谢。

最佳答案

public class TestImageView extends ImageView  implements OnGestureListener{
public TestImageView(Context context) {
super(context);
// TODO Auto-generated constructor stub

setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-ge`enter code here`nerated method stub
return false;
}
});
}

@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
return false;
}

@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub

}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}

@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub

}

@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
}

现在您可以在 xml 中使用 TestImageView 而不仅仅是 ImageView。

关于android - 为 ImageView 实现 GestureDetector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20094017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com