所以即时尝试创建一个界面,用户可以将手指从右向左拖动以进入第二个视图,拖动的第一个视图将从右向左移动。非常类似于Android设备主屏幕,用户可以向左或向右拖动屏幕,另一个屏幕将从那里出现。到目前为止,我有一个View切换器,其中flipper将存储两个xml文件。但我不知道如何使用拖动手势?任何例子将不胜感激。
谢谢
答案 0 :(得分:0)
这里有a link你可能会发现有用的信息。我可以回复任何查询。
HorizontalPager for Android HorizontalPager是修改后的版本 Marc Reichelt的RealViewSwitcher。 它基本上是横向的 ScrollView捕捉到全宽 孩子(就像Android主屏幕一样) 转换行为)。这修改了 版本支持垂直滚动 儿童
答案 1 :(得分:0)
我只写了一个集成拖动手势和图像切换效果的例子。希望它有所帮助。
所有需要的资源和代码如下:
主要布局:将ImageSwitcher添加到布局
两个名为R.drawable.s3的画册,R.drawable.s4
两个动画文件,slide_right_in,slide_left_out
public class PureTestActivity extends Activity{
final String TAG = PureTestActivity.class.getSimpleName();
/** Called when the activity is first created. */
LinearLayout mainLL;
GestureDetector gestureDetector;
ImageSwitcher vs;
int imageIdx = 0;
int[] imageResId = {R.drawable.s3, R.drawable.s4};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainLL = (LinearLayout)findViewById(R.id.main);
vs = (ImageSwitcher)findViewById(R.id.viewSwitcher1);
vs.setFactory(new ViewFactory(){
@Override
public View makeView() {
ImageView i = new ImageView(PureTestActivity.this);
i.setBackgroundColor(0xFF000000);
i.setImageResource(R.drawable.s3);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return i;
}
});
vs.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
vs.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
//start for touch events Gesture detection
gestureDetector = new GestureDetector(new MyGestureDetector());
View.OnTouchListener gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
Log.d(TAG, "ontouch event.");
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
};
vs.setOnTouchListener(gestureListener);
}
class MyGestureDetector extends SimpleOnGestureListener {
final String TAG = MyGestureDetector.class.getSimpleName();
// for touch left or touch right events
private static final int SWIPE_MIN_DISTANCE = 60; //default is 120
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
@Override
public boolean onDown(MotionEvent e) {
Log.d(TAG, " on down events :" + e.getAction() );
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.d(TAG, " on filing event, first velocityX :" + velocityX +
" second velocityY" + velocityY);
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
onHorizonTouch(true); // left
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
onHorizonTouch(false); // right
}
} catch (Exception e) {
// nothing
}
return false;
}
void onHorizonTouch(Boolean toLeft){
if(toLeft && imageIdx>0)
{
vs.setInAnimation(AnimationUtils.loadAnimation(
getApplicationContext(), R.anim.slide_right_in));
// 设置切出动画
vs.setOutAnimation(AnimationUtils.loadAnimation(
getApplicationContext(), R.anim.slide_left_out));
PureTestActivity.this.vs.setImageResource(imageResId[--imageIdx]);
}
if(!toLeft && imageIdx<1)
{
vs.setInAnimation(AnimationUtils.loadAnimation(PureTestActivity.this,
android.R.anim.slide_in_left));
vs.setOutAnimation(AnimationUtils.loadAnimation(PureTestActivity.this,
android.R.anim.slide_out_right));
PureTestActivity.this.vs.setImageResource(imageResId[++imageIdx]);
}
};
}
}