android图片自动轮播,
时间: 2023-07-22 08:07:49 浏览: 123
在 Android 中实现图片自动轮播可以使用 ViewPager 和 Timer 结合的方式。下面是一个简单的示例代码:
1. 创建布局文件
在布局文件中使用 ViewPager 和 CircleIndicator,CircleIndicator 是一个指示器,用于显示当前选中的页。
```xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="200dp" />
<me.relex.circleindicator.CircleIndicator
android:id="@+id/indicator"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="16dp" />
</RelativeLayout>
```
2. 创建适配器
创建一个 PagerAdapter,用于显示图片,这里以本地图片为例,也可以从网络加载图片。
```java
public class MyPagerAdapter extends PagerAdapter {
private int[] mImageIds;
public MyPagerAdapter(int[] imageIds) {
mImageIds = imageIds;
}
@Override
public int getCount() {
return mImageIds.length;
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
ImageView imageView = new ImageView(container.getContext());
imageView.setImageResource(mImageIds[position]);
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
}
```
3. 设置适配器和指示器
在 Activity 中设置 ViewPager 的适配器和指示器。
```java
public class MainActivity extends AppCompatActivity {
private ViewPager mViewPager;
private CircleIndicator mIndicator;
private int[] mImageIds = new int[]{
R.drawable.image1,
R.drawable.image2,
R.drawable.image3,
R.drawable.image4
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = findViewById(R.id.view_pager);
mIndicator = findViewById(R.id.indicator);
MyPagerAdapter adapter = new MyPagerAdapter(mImageIds);
mViewPager.setAdapter(adapter);
mIndicator.setViewPager(mViewPager);
}
}
```
4. 实现自动轮播
使用 Timer 和 TimerTask 来实现自动轮播。
```java
public class MainActivity extends AppCompatActivity {
private ViewPager mViewPager;
private CircleIndicator mIndicator;
private int[] mImageIds = new int[]{
R.drawable.image1,
R.drawable.image2,
R.drawable.image3,
R.drawable.image4
};
private int mCurrentPage = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = findViewById(R.id.view_pager);
mIndicator = findViewById(R.id.indicator);
MyPagerAdapter adapter = new MyPagerAdapter(mImageIds);
mViewPager.setAdapter(adapter);
mIndicator.setViewPager(mViewPager);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (mCurrentPage == mImageIds.length - 1) {
mCurrentPage = 0;
} else {
mCurrentPage++;
}
mViewPager.setCurrentItem(mCurrentPage);
}
});
}
}, 2000, 2000);
}
}
```
这样就可以实现图片的自动轮播了。
阅读全文