自定义个 Android 的视频选择相册demo 用自定义页面适配器的方法获取
时间: 2024-03-02 08:51:52 浏览: 60
适配页面的demo
好的,我可以给你提供一个简单的Android视频选择相册demo的实现。下面是一个实现的步骤:
1. 在build.gradle文件中添加以下依赖项:
```
dependencies {
// ...
implementation 'com.github.bumptech.glide:glide:4.11.0'
implementation 'com.github.bumptech.glide:okhttp3-integration:4.11.0'
implementation 'com.zhihu.android:matisse:0.5.3-beta3'
}
```
2. 创建一个自定义布局文件video_item.xml来显示视频缩略图和标题。例如:
```
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/video_thumbnail"
android:layout_width="80dp"
android:layout_height="80dp"
android:scaleType="centerCrop"/>
<TextView
android:id="@+id/video_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="16dp"
android:textSize="16sp"/>
</LinearLayout>
```
3. 创建一个自定义适配器类VideoAdapter,用于显示视频缩略图和标题。例如:
```
public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.ViewHolder> {
private final List<Video> mVideos;
public VideoAdapter(List<Video> videos) {
mVideos = videos;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.video_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Video video = mVideos.get(position);
Glide.with(holder.itemView.getContext())
.load(video.getThumbnail())
.into(holder.mThumbnailView);
holder.mTitleView.setText(video.getTitle());
}
@Override
public int getItemCount() {
return mVideos.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public final ImageView mThumbnailView;
public final TextView mTitleView;
public ViewHolder(View view) {
super(view);
mThumbnailView = (ImageView) view.findViewById(R.id.video_thumbnail);
mTitleView = (TextView) view.findViewById(R.id.video_title);
}
}
}
```
4. 在Activity或Fragment中,使用以下代码来启动视频选择器:
```
Matisse.from(this)
.choose(MimeType.ofVideo())
.countable(true)
.maxSelectable(9)
.gridExpectedSize(getResources().getDimensionPixelSize(R.dimen.grid_expected_size))
.restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
.thumbnailScale(0.85f)
.imageEngine(new GlideEngine())
.forResult(REQUEST_CODE_CHOOSE);
```
5. 在onActivityResult()方法中,解析视频选择器返回的数据,并将其传递给VideoAdapter来显示。
```
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_CHOOSE && resultCode == RESULT_OK) {
List<Uri> uris = Matisse.obtainResult(data);
List<Video> videos = new ArrayList<>();
for (Uri uri : uris) {
videos.add(new Video(uri.toString()));
}
mVideoAdapter = new VideoAdapter(videos);
mRecyclerView.setAdapter(mVideoAdapter);
}
}
```
6. 最后,定义一个Video类来保存视频的URI和标题。
```
public class Video {
private final String mUri;
private String mTitle;
public Video(String uri) {
mUri = uri;
}
public String getUri() {
return mUri;
}
public String getTitle() {
return mTitle;
}
public void setTitle(String title) {
mTitle = title;
}
public String getThumbnail() {
// 假设使用Glide从URI中获取视频缩略图
return Glide.with(App.getInstance())
.asBitmap()
.load(mUri)
.submit()
.get();
}
}
```
以上就是一个简单的视频选择相册demo的实现,希望能对你有所帮助。
阅读全文