ViewFlipper,好像一个容器!!!,你可以把许多的View放在这个容器中,让它展示给用户,虽然它每次只展示一个,话不多说直接上原码!
1、创建布局文件并且在布局里加上ViewFlipper==ImageView
<LinearLayout 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"
android:orientation="vertical"
>
<ViewFlipper
android:id="@+id/vf"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/lauout1"<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromXDelta="0"
android:toXDelta="100%p"
/>
android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/image1" android:src="@drawable/abc" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout><LinearLayout android:id="@+id/lauout2" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/image2" android:src="@drawable/championsskin_15003" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout><LinearLayout android:id="@+id/lauout3" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/image3" android:src="@drawable/championsskin_15004" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout></ViewFlipper>
</LinearLayout> 2、创建anim的xml文件好四个特效
in_leftright.xml文件、in_rightleft.xml文件、out_leftright.xml文件、out_rightleft.xml文件。例如in_leftright.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromXDelta="0"
android:toXDelta="100%p"
/>
1000表示一秒,0表示进来的横坐标,100表示出去的横坐标
3、代码部分创建ViewFlipper
public class MainActivity extends Activity implements OnTouchListener {
private ViewFlipper vf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vf=(ViewFlipper) findViewById(R.id.vf);
vf.setOnTouchListener(this);
}
4、实现触屏事件并添加图片例如
public class MainActivity extends Activity implements OnTouchListener {
private ViewFlipper vf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vf=(ViewFlipper) findViewById(R.id.vf);
vf.setOnTouchListener(this);
}