个人理解viewflipper,他其实就是切换view视图很方便而已,在做广告的时候有利用的价值,它可以自己来进行图片的每隔多长时间来进行切换
Viewflipper是一个组件,可以在transitions中找到
我照着网页上面的步骤做了如下的操作,是可以实现的
Main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ViewFlipper
android:id="@+id/viewFlipper1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:inAnimation="@anim/push_in"
android:outAnimation="@anim/push_out"
android:persistentDrawingCache="animation" >
<LinearLayout
android:id="@+id/lin1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next1" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/content" />
</LinearLayout>
<LinearLayout
android:id="@+id/lin2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next2" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/content" />
</LinearLayout>
<LinearLayout
android:id="@+id/lin3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next3" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/content" />
</LinearLayout>
</ViewFlipper>
</RelativeLayout>
Push_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.0"
android:toAlpha="1.0"
android:duration="500" />
</set>
Push_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.0"
android:duration="500" />
</set>
ViewFlipperActivity
public class ViewFlipperActivity extends Activity implements OnClickListener,OnGestureListener{
private ViewFlipper vf;
private Button btn1,btn2,btn3;
private GestureDetector gd;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gd=new GestureDetector(this);
vf=(ViewFlipper)findViewById(R.id.viewFlipper1);
vf.setFlipInterval(1000);//每个view切换的时间是200微秒
vf.startFlipping();//开始不停地循环的显示
btn1=(Button)findViewById(R.id.button1);
btn2=(Button)findViewById(R.id.button2);
btn3=(Button)findViewById(R.id.button3);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
if (vf.isFlipping()) {
vf.stopFlipping();//停止自动切换
}
switch (v.getId()) {
case R.id.button1:
vf.showNext();
break;
case R.id.button2:
vf.showNext();
break;
case R.id.button3:
vf.showNext();
break;
default:
break;
}
}
//这一步是必不可少的,主要就是要他的返回值的,因为我进过测试如果不用触摸事件的返回值,那么我的手势的onfling()就不会被调用
@Override
public boolean onTouchEvent(MotionEvent event) {
return gd.onTouchEvent(event);//要的就是这个返回值
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
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;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if (e1.getX()>e2.getX()) {
Toast.makeText(ViewFlipperActivity.this, "你是向←滑动的", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(ViewFlipperActivity.this, "你是向→滑动的", Toast.LENGTH_SHORT).show();
}
return false;
}
}