Xử lý sự kiện vuốt màn hình
MainActivity.java
package com.switcher;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ViewSwitcher;
public class MainActivity extends Activity implements OnClickListener,OnGestureListener{
private ViewSwitcher switcher;
private Button btnNext,btnPrevious;
private GestureDetector gesturedetector = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
switcher = (ViewSwitcher) findViewById(R.id.viewSwitcher);
btnNext = (Button) findViewById(R.id.btnNext);
btnPrevious = (Button) findViewById(R.id.btnPrevious);
btnNext.setOnClickListener(this);
btnPrevious.setOnClickListener(this);
gesturedetector = new GestureDetector(this, this);
}
@Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.btnNext:
//Button Next Style
Animation animationNext = AnimationUtils.loadAnimation(this, R.anim.btn_style_next);
btnNext.startAnimation(animationNext);
nextView();
break;
case R.id.btnPrevious:
//Button Previous Style
Animation animationPrevious = AnimationUtils.loadAnimation(this, R.anim.btn_style_previous);
btnPrevious.startAnimation(animationPrevious);
previousView();
break;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return gesturedetector.onTouchEvent(event);
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
int SWIPE_MIN_VELOCITY = 100;
int SWIPE_MIN_DISTANCE = 100;
//Sự kiện khi bạn vuốt màn hình đưa sang một bên nào đó
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {
//Get Position
float ev1X = e1.getX();
float ev2X = e2.getX();
//Get distance of X (e1) to X (e2)
final float xdistance = Math.abs(ev1X - ev2X);
//Get veclocity of cusor
//Vận tốc = số điểm ảnh (px) / giây
final float xvelocity = Math.abs(velocityX);
//Vận tốc chuyển đổi X > 100 và khoảng cách từ điểm kéo đầu đến điểm kéo cuối > 100
if( (xvelocity > SWIPE_MIN_VELOCITY) && (xdistance > SWIPE_MIN_DISTANCE) )
{
if(ev1X > ev2X)//Switch Left
{
previousView();
}
else//Switch Right
{
nextView();
}
}
return false;
}
@Override
public void onLongPress(MotionEvent e) {
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY) {
return false;
}
@Override
public void onShowPress(MotionEvent e) {
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
//Next, Previous Views
private void previousView() {
// Previous View
switcher.setInAnimation(this, R.anim.in_animation1);
switcher.setOutAnimation(this, R.anim.out_animation1);
switcher.showPrevious();
}
private void nextView() {
// Next View
switcher.setInAnimation(this, R.anim.in_animation);
switcher.setOutAnimation(this, R.anim.out_animation);
switcher.showNext();
}
}
0 nhận xét:
Đăng nhận xét