对于Android Studio的轮播图的实现
由于本人经常忘记一些代码,现开始写博客进行基础的代码记录,为以后的编写提供一定的帮助。
1.在XML界面建立一个ViewFlipper,其中添加ImageView(要轮播多少图片就添加多少个ImageView)。
<ViewFlipper
android:id="@+id/flipper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inAnimation="@anim/left_in"
android:outAnimation="@anim/right_out">
<ImageView
android:id="@+id/imageView7"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/g1" />
<ImageView
android:id="@+id/imageView8"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/g2" />
</ViewFlipper>
这里本人就添加了两个ImageView,其中ImageView的src属性为要展示图片的路径。图中本人就添加了drawable下的g1,g2两张图片;ViewFlipper的inAnimation和outAnimation属性为轮播的效果。
2.创建一个anim文件夹在res文件夹下,anim文件夹中创建left_in.xml和right_out.xml(这里的xml文件表示轮播效果为左进右出)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="2000"
android:fromXDelta="-100%p"
android:toXDelta="0" />
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="2000"
android:fromXDelta="0"
android:toXDelta="100%p"/>
</set>
duration属性为轮播速度。
3.在Activity中声明Flipper,然后加入flipper.startFlipper();启动轮播。
ViewFlipper flipper = findViewById(R.id.flipper);
flipper.startFlipping();
完成轮播图!