Android 实现图片,视频在同一个item轮播,先展示图片,图片展示1秒暗淡消失,然后播放视频 Java完整代码加布局
时间: 2024-05-08 17:22:32 浏览: 46
安卓实现图片的轮播
以下是一个简单的实现,使用了ViewPager来实现图片和视频的轮播。
1. 布局文件:activity_main.xml
```xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
```
2. 页面布局文件:item_page.xml
```xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
<VideoView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
</RelativeLayout>
```
3. Java代码:MainActivity.java
```java
public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
private int[] images = {R.drawable.image1, R.drawable.image2, R.drawable.image3};
private int[] videos = {R.raw.video1, R.raw.video2, R.raw.video3};
private List<View> pages = new ArrayList<>();
private int currentPage = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = findViewById(R.id.view_pager);
for (int i = 0; i < images.length; i++) {
View page = LayoutInflater.from(this).inflate(R.layout.item_page, null);
ImageView imageView = page.findViewById(R.id.image_view);
VideoView videoView = page.findViewById(R.id.video_view);
imageView.setImageResource(images[i]);
videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + videos[i]));
pages.add(page);
}
viewPager.setAdapter(new PagerAdapter() {
@Override
public int getCount() {
return pages.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
View page = pages.get(position);
container.addView(page);
return page;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
});
viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
currentPage = position;
playPage();
}
});
playPage();
}
private void playPage() {
View page = pages.get(currentPage);
ImageView imageView = page.findViewById(R.id.image_view);
final VideoView videoView = page.findViewById(R.id.video_view);
imageView.animate().alpha(0f).setDuration(1000).withEndAction(new Runnable() {
@Override
public void run() {
videoView.setVisibility(View.VISIBLE);
videoView.start();
}
});
}
}
```
在MainActivity中,我们初始化了ViewPager,并且为每个页面添加了一个ImageView和一个VideoView。我们使用PagerAdapter来管理页面,当页面被选中时,我们调用playPage()方法来播放页面。在playPage()方法中,我们使用alpha动画来让ImageView逐渐消失,然后将VideoView设置为可见并开始播放视频。
阅读全文