TouchEvent in Android

1.Code:

View view = findViewById(R.id.activity_main);
 view.setOnTouchListener(new View.OnTouchListener(){
 String startX,endX;
 @Override
 public boolean onTouch(View view, MotionEvent event) {
 float eventX = event.getX();
 boolean re=false;
 switch (event.getAction()) {
 case MotionEvent.ACTION_DOWN:
 startX= String.valueOf(eventX);
 re = true;
 case MotionEvent.ACTION_MOVE:
 break;
 case MotionEvent.ACTION_UP:
 // nothing to do
 float xstart=Float.parseFloat(startX);
 if(xstart< eventX){
 if(currentImage==0){
 currentImage= images.length -1;
 }else {
 currentImage--;
 }
 ImageView preWallpaper = (ImageView) findViewById(R.id.wallpaper);
 preWallpaper.setImageResource(images[currentImage]);
 Toast.makeText(getApplicationContext(), "Pre Successes", Toast.LENGTH_SHORT).show();
 }else{
 if(currentImage==images.length-1){
 currentImage= 0;
 }else {
 currentImage++;
 }
 ImageView preWallpaper = (ImageView) findViewById(R.id.wallpaper);
 preWallpaper.setImageResource(images[currentImage]);
 Toast.makeText(getApplicationContext(), "Next Successes", Toast.LENGTH_SHORT).show();
 }
 break;
 default:
 re = false;
 }
 return re;
 }
 });

2. Link tham khảo:
http://www.vogella.com/tutorials/AndroidTouch/article.html
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s