作者实现的基础上,加上了文字的变换
-
public class MainActivity extends Activity
-
{
-
private ViewFlipper viewFlipper;
-
private Button autoplay;
-
@Override
-
public void onCreate(Bundle savedInstanceState)
-
{
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.main);
-
viewFlipper = (ViewFlipper) findViewById(R.id.details);
-
autoplay = (Button) findViewById(R.id.autoplay);
-
}
-
public void prev(View source)
-
{
-
viewFlipper.setInAnimation(this , R.anim.slide_in_right);
-
viewFlipper.setOutAnimation(this , R.anim.slide_out_left);
-
// 显示上一个组件
-
viewFlipper.showPrevious();
-
// 停止自动播放
-
viewFlipper.stopFlipping();
-
autoplay.setText("自动播放");
-
}
-
public void next(View source)
-
{
-
viewFlipper.setInAnimation(this , android.R.anim.slide_in_left);
-
viewFlipper.setOutAnimation(this , android.R.anim.slide_out_right);
-
// 显示下一个组件
-
viewFlipper.showNext();
-
// 停止自动播放
-
viewFlipper.stopFlipping();
-
autoplay.setText("自动播放");
-
}
-
public void auto(View source)
-
{
-
viewFlipper.setInAnimation(this , android.R.anim.slide_in_left);
-
viewFlipper.setOutAnimation(this , android.R.anim.slide_out_right);
-
String s=autoplay.getText().toString();
-
if (s.equals("自动播放")) {
-
// 开始自动播放
-
viewFlipper.startFlipping();
-
autoplay.setText("停止自动");
-
} else {
-
viewFlipper.stopFlipping();
-
autoplay.setText("自动播放");
-
}
-
-
}
-
}
|
XML文件
-
<?xml version="1.0" encoding="utf-8"?>
-
<RelativeLayout
-
xmlns:android="http://schemas.android.com/apk/res/android"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent">
-
<ViewFlipper
-
android:id="@+id/details"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
android:flipInterval="1000">
-
<ImageView
-
android:src="@drawable/java"
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content">
-
</ImageView>
-
<ImageView
-
android:src="@drawable/android"
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content">
-
</ImageView>
-
<ImageView
-
android:src="@drawable/javaee"
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content">
-
</ImageView>
-
</ViewFlipper>
-
<Button
-
android:text="<"
-
android:onClick="prev"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:layout_alignParentBottom="true"
-
android:layout_alignParentLeft="true"/>
-
<Button
-
android:id="@+id/autoplay"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:layout_alignParentBottom="true"
-
android:layout_centerInParent="true"
-
android:onClick="auto"
-
android:text="自动播放"/>
-
<Button
-
android:text=">"
-
android:onClick="next"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:layout_alignParentBottom="true"
-
android:layout_alignParentRight="true"/>
-
</RelativeLayout>
|
-
<?xml version="1.0" encoding="utf-8"?>
-
<set xmlns:android="http://schemas.android.com/apk/res/android">
-
<!-- 设置从右边拖进来的动画
-
android:duration指定动画持续时间 -->
-
<translate
-
android:fromXDelta="100%p"
-
android:toXDelta="0"
-
android:duration="@android:integer/config_mediumAnimTime" />
-
</set>
|
-
<?xml version="1.0" encoding="utf-8"?>
-
<set xmlns:android="http://schemas.android.com/apk/res/android">
-
<!-- 设置从左边拖出去的动画
-
android:duration指定动画持续时间 -->
-
<translate
-
android:fromXDelta="0"
-
android:toXDelta="-100%p"
-
android:duration="@android:integer/config_mediumAnimTime" />
-
</set>
|
效果
![](https://i-blog.csdnimg.cn/blog_migrate/cfb9272375524b01178ddf7933ac087d.jpeg)
转载于:https://www.cnblogs.com/wwjldm/p/6930514.html