我需要实现这样的功能:当我从某个项目上移开手指时,需要触发一些事件。因此,我需要相应的事件来实现。
***Scenario:***
1. Touch the image view using finger.
2. Remove the finger.
3. Now implement the functionality.
我希望在第二步得到事件回调。
如果有预定义的事件,请建议名称。
好的。当你触摸屏幕并拿开手指时,会调用以下事件:
祝你好运!
在这种情况下,您可以为ImageView实现OnTouchListener。
yourImageView.setOnTouchListener(new OnTouchListener () {
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
Log.d("TouchTest", "Touch down");
}
else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
Log.d("TouchTest", "Touch up");
}
}
}
public class TestTouchEvents extends Activity implements OnTouchListener {
ImageView imageView;
Bitmap bitmap;
Canvas canvas;
Paint paint;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView) this.findViewById(R.id.ImageView);
Display currentDisplay = getWindowManager().getDefaultDisplay();
float dw = currentDisplay.getWidth();
float dh = currentDisplay.getHeight();
bitmap = Bitmap.createBitmap((int) dw, (int) dh,Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
paint = new Paint();
paint.setColor(Color.GREEN);
imageView.setImageBitmap(bitmap);
imageView.setOnTouchListener(this);
}
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
// Do Something
Log.d("Touch", "Touch down");
break;
case MotionEvent.ACTION_MOVE:
// Do Something
Log.d("Touch", "Touch move");
//imageView.invalidate();
break;
case MotionEvent.ACTION_UP:
// Do Something
Log.d("Touch", "Touch up");
//imageView.invalidate();
break;
case MotionEvent.ACTION_CANCEL:
Log.d("Touch", "Touch cancel");
break;
default:
break;
}
return true;
}
}