android 左右页面滑动(滑屏)增加layout文件 而不是drawable(还有activity)
|字号 订阅
ViewFlipper + GestureDetector
简单的实现:
这里还需实现 implements OnGestureListener 。
另外需要4个动画的配置文件: 说到Animation,我们先看下如何在Android中实现自定义Animation。自定义的Animation是以XML格式定义的,定义好的XML文件存放在res/anim中。
一般的Animation有以下四种类型:
1. Alpha:渐变透明度动画效果
2. Scale:渐变尺寸伸缩动画效果
3. Translate:画面转换位置移动动画效果
4. Rotate:画面转换位置移动动画效果
push_left_in.xml文件中代码:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="100%p" android:toXDelta="0"
android:duration="500" />
<alpha android:fromAlpha="0.1" android:toAlpha="1.0"
android:duration="500" />
</set>
push_left_out.xml文件中代码:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="-100%p"
android:duration="500" />
<alpha android:fromAlpha="1.0" android:toAlpha="0.1"
android:duration="500" />
</set>
push_right_in.xml文件中代码:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-100%p" android:toXDelta="0"
android:duration="500" />
<alpha android:fromAlpha="0.1" android:toAlpha="1.0"
android:duration="500" />
</set>
push_right_out.xml文件中代码:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="100%p"
android:duration="500" />
<alpha android:fromAlpha="1.0" android:toAlpha="0.1"
android:duration="500" />
</set>
//以下是主代码了:
private ViewFlipper _viewFlipper;
private GestureDetector detector;
//增加activity 而不是drawable
private View addViewObject(int layout){
return LayoutInflater.from(this).inflate(layout, null);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main_ui);
detector= new GestureDetector(this);
_viewFlipper=(ViewFlipper) findViewById(R.id.viewflipper_ui);
_viewFlipper.addView(this.addViewObject(R.layout.viewpager_two));
_viewFlipper.addView(this.addViewObject(R.layout.viewpager_two));
_viewFlipper.addView(this.addViewObject(R.layout.viewpager_three));
detector= new GestureDetector(this);
_viewFlipper=(ViewFlipper) findViewById(R.id.viewflipper_ui);
}
// onTouchEvent
@Override public boolean onTouchEvent(MotionEvent event) {
// return super.onTouchEvent(event);
return detector.onTouchEvent(event);
}
@Override public boolean onDown(MotionEvent e) { return false; }
//主要是这个方法,其它方法都是不需要写代码的。不过看个人需求吧。
@Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (e1.getX() - e2.getX() > 50) {
this._viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
this._viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
this._viewFlipper.showNext(); return true;
} else if (e1.getX() - e2.getX() < -50) {
this._viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_in));
this._viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,R.anim.push_right_out)); this._viewFlipper.showPrevious(); return true; } 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) {
// TODO Auto-generated method stub }
@Override public boolean onSingleTapUp(MotionEvent e) { // TODO Auto-generated method stub return false; }
//研究了好久。
滑动跳转为activity:
继承extends ActivityGroup
然后:
_viewFlipper.addView(getLocalActivityManager().startActivity("", new Intent(MainUiActivity.this, GridViewMore.class)).getDecorView(), 0); _viewFlipper.addView(getLocalActivityManager().startActivity("", new Intent(MainUiActivity.this,GridViewIntroduce.class)).getDecorView(),
1); _viewFlipper.setDisplayedChild(0);
其余代码不变!