gpt4 book ai didi

android - ViewPager 在 Imageview 上先于 VideoView 显示 Mediacontrols

转载 作者:行者123 更新时间:2023-12-02 10:33:47 24 4
gpt4 key购买 nike

图库功能:

我正在开发一个类似于图库的功能,用户可以通过滑动来查看他们上传的图像或视频。

问题:

只要 imageview 之后有视频,videoview 的媒体控件就会在 imageview 上显示大约 2 秒

Image showing Mediacontrols

代码:

item_pager.xml:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@color/black_overlay"
android:gravity="center"
android:orientation="vertical">

<ImageView
android:id="@+id/images_pager"
android:layout_width="match_parent"
android:layout_height="250dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:visibility="visible"
app:srcCompat="@drawable/cctv_icon" />

<VideoView
android:id="@+id/videoview"
android:layout_width="match_parent"
android:layout_height="250dp"
android:adjustViewBounds="true"
android:visibility="gone" />
</LinearLayout>

</layout>

PagerAdapter 代码:

public class ItemViewpager extends PagerAdapter implements MediaPlayer.OnPreparedListener, 
View.OnClickListener, MediaPlayer.OnCompletionListener, View.OnFocusChangeListener {
private List<MediaInfo> mediaInfos;
private LayoutInflater layoutInflater;
private MediaController mediaController;
private HashMap<Integer, MediaController> mediaMap;
private VideoView videoView;
private ImageView imageView;
private int currPos = 0;

public ItemViewpager(List<MediaInfo> mediaInfos) {
this.mediaInfos = mediaInfos;
mediaMap = new HashMap<>(mediaInfos.size());
}

@Override
public int getCount() {
return mediaInfos.size();
}

@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == ((LinearLayout) object);
}

@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
layoutInflater=
(LayoutInflater)container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = layoutInflater.inflate(R.layout.item_pager, container, false);

imageView = itemView.findViewById(R.id.images_pager);
videoView = itemView.findViewById(R.id.videoview);
videoView.setOnPreparedListener(this);
videoView.setOnCompletionListener(this);
videoView.setOnFocusChangeListener(this);

boolean video = mediaInfos.get(position).video;
String uri = mediaInfos.get(position).uri;

mediaController = new MediaController(container.getContext());
mediaMap.put(position, mediaController);
if (video) {
imageView.setVisibility(View.GONE);
videoView.setVisibility(View.VISIBLE);
videoView.setVideoURI(Uri.parse(uri));
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.requestFocus();
videoView.seekTo(1);
} else {
imageView.setVisibility(View.VISIBLE);
videoView.setVisibility(View.GONE);
Picasso.get().load(uri).fit().placeholder(R.mipmap.ic_launcher).into(imageView);
}
if (mediaMap.size() > 0)
mediaMap.get(position).hide();

container.addView(itemView);

return itemView;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}

@Override
public void onPrepared(MediaPlayer mediaPlayer) {

}

public void onPageSelectionListener(int pos) {
currPos = pos;
boolean video = mediaInfos.get(currPos).video;

/*if (mediaMap.size() > 0) {
if (video)
mediaMap.get(currPos).show(0);
else
mediaMap.get(currPos).hide();
}*/
}


@Override
public void onClick(View view) {
}

@Override
public void onCompletion(MediaPlayer mediaPlayer) {
boolean video = mediaInfos.get(currPos).video;

/*if (!video)
videoView.stopPlayback();
else
mediaMap.get(currPos).show();*/
}

@Override
public void onFocusChange(View view, boolean focus) {
}

}

提前致谢:

最佳答案

试试这个:

if (video) {
imageView.setVisibility(View.GONE);
videoView.setVisibility(View.VISIBLE);
videoView.setVideoURI(Uri.parse(uri));
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.requestFocus();
videoView.seekTo(1);
} else {
imageView.setVisibility(View.VISIBLE);
videoView.setVisibility(View.GONE);
//add below line, hide mediaController
videoView.setMediaController(null);
Picasso.get().load(uri).fit().placeholder(R.mipmap.ic_launcher).into(imageView);
}

此代码隐藏 mediaController:

videoView.setMediaController(null);

关于android - ViewPager 在 Imageview 上先于 VideoView 显示 Mediacontrols,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58476498/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com