1.有一个AdapterViewFilpper控件
2.准备图片资源到drawable文件夹下面,并在MainActivity用数组存去该图片资源数组
3.为AdapterViewFilpper添加适配器
4.该控件有showPrevious,showNext,stopFlipping,startFlipping等一些方法。
showPrevious往前播放
showNext往后播放
stopFlipping停止自动播放
startFlipping开启自动播放
在xml代码中还有一个可以设置播放事件的flipInterval,代码如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<AdapterViewFlipper
android:layout_width="400dp"
android:layout_height="500dp"
android:id="@+id/flipper"
android:flipInterval="5000"
/>
<LinearLayout
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/button"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="上一个"
android:layout_marginBottom="8dp"/>
<Button
android:id="@+id/button2"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="下一个"
android:layout_marginBottom="8dp"/>
<Button
android:id="@+id/button3"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="自动播放"
android:layout_marginBottom="8dp"/>
</LinearLayout>
</LinearLayout>
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterViewFlipper;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private int[] imageIds=new int[]{R.drawable.gjiaoshi,R.drawable.guanjiaoshi0,R.drawable.guanxuesheng0,R.drawable.gxuesheng};
private AdapterViewFlipper adapterViewFlipper;
Button button,button2,button3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapterViewFlipper=findViewById(R.id.flipper);
final BaseAdapter adapter=new BaseAdapter() {
@Override
public int getCount() {
return imageIds.length;
}
@Override
public Object getItem(int i) {
return i;
}
@Override
public long getItemId(int i) {
return i;
}
/**
*主要步骤在getView方法里面
*/
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ImageView imageView;
if(view==null){
//创建一个imageView
imageView=new ImageView(MainActivity.this);
}else{
imageView= (ImageView) view;
}
imageView.setImageResource(imageIds[i]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
));
return imageView;
}
};
//添加适配器
adapterViewFlipper.setAdapter(adapter);
//为按钮设置点击事件
button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
adapterViewFlipper.showPrevious();
adapterViewFlipper.stopFlipping();
}
});
button2=findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
adapterViewFlipper.showNext();
adapterViewFlipper.stopFlipping();
}
});
button3=findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
adapterViewFlipper.startFlipping();
}
});
}
}