作者:Im边夫人 | 来源:互联网 | 2023-09-13 17:34
主显示布局以及代码:
activity_main.xml:
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:id="@+id/gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<VideoView
android:id&#61;"&#64;&#43;id/vv"
android:layout_below&#61;"&#64;id/gallery"
android:layout_width&#61;"match_parent"
android:layout_height&#61;"match_parent"/>
主Activity的java代码&#xff1a;
package com.sxt.day07_09;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore.Video;
import android.provider.MediaStore.Video.Thumbnails;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.VideoView;
public class MainActivity extends Activity {
ArrayList> mVideos;
Gallery mGallery;//横向显示列表
VideoAdapter mAdapter;//适配器
VideoView mVideoView;//播放视屏的ui
&#64;Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getVideoInfo();
initView();
setListener();
}
private void setListener() {
mGallery.setOnItemClickListener(new OnItemClickListener() {
&#64;Override
public void onItemClick(AdapterView> parent, View view,
int position, long id) {
HashMap video&#61;mVideos.get(position);
String path&#61;video.get(Video.Media.DATA).toString();
mVideoView.setVideoPath(path);
mVideoView.start();
}
});
}
private void initView() {
mVideoView&#61;(VideoView) findViewById(R.id.vv);
mGallery&#61;(Gallery) findViewById(R.id.gallery);
mAdapter&#61;new VideoAdapter(mVideos, this);
mGallery.setAdapter(mAdapter);
}
private void getVideoInfo() {
ContentResolver resolver &#61; getContentResolver();
String[] projection&#61;{
Video.Media._ID,
Video.Media.DATA,
Video.Media.TITLE
};
mVideos&#61;new ArrayList>();
Cursor c &#61; resolver.query(Video.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);
while(c.moveToNext()){
HashMap video&#61;new HashMap();
video.put(Video.Media._ID, c.getInt(c.getColumnIndex(Video.Media._ID)));
video.put(Video.Media.DATA, c.getString(c.getColumnIndex(Video.Media.DATA)));
video.put(Video.Media.TITLE, c.getString(c.getColumnIndex(Video.Media.TITLE)));
mVideos.add(video);
}
}
class VideoAdapter extends BaseAdapter{
ArrayList> videos;
Context context;
public VideoAdapter(ArrayList> videos,
Context context) {
super();
this.videos &#61; videos;
this.context &#61; context;
}
&#64;Override
public int getCount() {
return videos.size();
}
&#64;Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
&#64;Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
&#64;Override
public View getView(int position, View layout, ViewGroup parent) {
layout&#61;View.inflate(context, R.layout.item_video, null);
ImageView ivThumb&#61;(ImageView) layout.findViewById(R.id.ivThumb);
TextView tvTitle&#61;(TextView) layout.findViewById(R.id.tvTitle);
HashMap video&#61;videos.get(position);
tvTitle.setText(video.get(Video.Media.TITLE).toString());
int id&#61;(Integer) video.get(Video.Media._ID);
Bitmap bm&#61;Thumbnails.getThumbnail(getContentResolver(), id, Thumbnails.MICRO_KIND, null);
ivThumb.setImageBitmap(bm);
return layout;
}
}
}
适配的布局&#xff1a;item_video.xml
android:layout_width&#61;"wrap_content"
android:layout_height&#61;"wrap_content"
android:orientation&#61;"vertical" >
android:id&#61;"&#64;&#43;id/ivThumb"
android:layout_width&#61;"80dp"
android:layout_height&#61;"80dp"
android:src&#61;"&#64;drawable/ic_launcher"/>
android:id&#61;"&#64;&#43;id/tvTitle"
android:layout_width&#61;"80dp"
android:layout_height&#61;"wrap_content"
android:text&#61;"安卓"
android:gravity&#61;"center_horizontal"/>