在安卓ImageView上设置2张以上的图片,可以通过以下几种方式实现:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
<item android:drawable="@drawable/image1" android:duration="1000" />
<item android:drawable="@drawable/image2" android:duration="1000" />
<item android:drawable="@drawable/image3" android:duration="1000" />
</animation-list>
ImageView imageView = findViewById(R.id.imageView);
imageView.setBackgroundResource(R.drawable.animation);
AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
animationDrawable.start();
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
public class ImagePagerAdapter extends PagerAdapter {
private List<Integer> imageList;
public ImagePagerAdapter(List<Integer> imageList) {
this.imageList = imageList;
}
@Override
public int getCount() {
return imageList.size();
}
@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(imageList.get(position));
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((ImageView) object);
}
}
ViewPager viewPager = findViewById(R.id.viewPager);
List<Integer> imageList = new ArrayList<>();
imageList.add(R.drawable.image1);
imageList.add(R.drawable.image2);
imageList.add(R.drawable.image3);
ImagePagerAdapter adapter = new ImagePagerAdapter(imageList);
viewPager.setAdapter(adapter);
以上两种方法可以实现在安卓ImageView上设置2张以上的图片,并且可以根据具体需求选择适合的方式。
领取专属 10元无门槛券
手把手带您无忧上云