新手试验品
想做一个视频列表,可以获取本地所有视频,并可以进行删除。之前用过自定义BaseAdapter,数据为静态的,不能及时更新系统数据,查了资料才知道应该用CursorAdapter。
以下实现的视频列表,会显示视频的缩略图、标题、时长和大小;单击Item会启动另一个Activity进行播放,长按Item会出现CheckBox和编辑功能布局,并有一个反选功能。
视频的缩略图加载使用缓存和延迟加载,是改造别人的,原地址为:http://blog.csdn.net/binyao02123202/article/details/8175345
(视频列表只是自己试验品的一个部分,没有独立出来检测是否可以运行)
package com.example.video;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.RelativeLayout.LayoutParams;
public class VideoActivity extends Activity {
private Cursor videoCursor;
private ListView videoListView;
private VideoAdapter videoAdapter;
private RelativeLayout layout_edit;
private Button cancel, delete, reverse;
private boolean edit_visiable = false;
private RemoteImageHelper imageHelper = new RemoteImageHelper();
private List<Long> selectedVideos = new ArrayList<Long>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video_listview);
initViews();
}
private void initViews() {
initListView();
initListEmptyView();
layout_edit = (RelativeLayout) findViewById(R.id.layout_edit);
cancel = (Button) findViewById(R.id.video_edit_cancel);
delete = (Button) findViewById(R.id.video_edit_delete);
reverse = (Button) findViewById(R.id.video_edit_reverse);
setClickListeners();
}
@SuppressWarnings("static-access")
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (edit_visiable && event.KEYCODE_BACK == keyCode) {
hideEditLayout();
return true;
}
return super.onKeyDown(keyCode, event);
}
private void initListView() {
final Uri sourceUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String[] projection = { MediaStore.Video.Media._ID,
MediaStore.Video.Media.DATA, MediaStore.Video.Media.TITLE,
MediaStore.Video.Media.DISPLAY_NAME,
MediaStore.Video.Media.SIZE, MediaStore.Video.Media.DURATION };
String orderBy = MediaStore.Video.Media.TITLE;
videoCursor = getContentResolver().query(sourceUri, projection, null,
null, orderBy);
String[] from = { MediaStore.Video.Media.TITLE,
MediaStore.Video.Media.DURATION, MediaStore.Video.Media.SIZE };
int[] to = { R.id.video_title, R.id.video_duration, R.id.video_size };
videoListView = (ListView) findViewById(R.id.video_listview);
videoAdapter = new VideoAdapter(this, R.layout.video_list_item,
videoCursor, from, to);
videoListView.setAdapter(videoAdapter);
videoListView.setScrollBarStyle(1);
}
private void initListEmptyView() {
TextView emptyView = new TextView(this);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
params.addRule(RelativeLayout.CENTER_VERTICAL);
emptyView.setLayoutParams(params);
emptyView.setText("没有视频资源");
emptyView.setVisibility(View.GONE);
((ViewGroup) videoListView.getParent()).addView(emptyView);
videoListView.setEmptyView(emptyView);
}
private void setClickListeners() {
videoListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (edit_visiable) {
ViewItemHolder holder = (ViewItemHolder) view.getTag();
holder.cb.toggle();
if (holder.cb.isChecked()) {
selectedVideos.add(holder.id);
} else {
if (selectedVideos.contains(holder.id)) {
selectedVideos.remove(holder.id);
}
}
} else {
// play the video
// .....
ViewItemHolder holder = (ViewItemHolder) view.getTag();
Intent intent = new Intent();
intent.setAction("com.example.VIDEOPLAY");
intent.putExtra("VideoUri", holder.Url);
VideoActivity.this.startActivity(intent);
}
}
});
videoListView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
if (edit_visiable) {
hideEditLayout();
} else {
showEditLayout();
}
return true;
}
});
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
hideEditLayout();
}
});
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < selectedVideos.size(); i++) {
String where = android.provider.MediaStore.Video.Media._ID
+ "='" + selectedVideos.get(i) + "'";
getContentResolver()
.delete(android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
where, null);
}
hideEditLayout();
}
});
reverse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < videoAdapter.getCount(); i++) {
long itemId = videoAdapter.getItemId(i);
if (selectedVideos.contains(itemId)) {
selectedVideos.remove(itemId);
} else {
selectedVideos.add(itemId);
}
}
videoAdapter.notifyDataSetInvalidated();
Toast.makeText(VideoActivity.this,
"已选中" + selectedVideos.size() + "项", Toast.LENGTH_LONG)
.show();
}
});
}
private void showEditLayout() {
RelativeLayout.LayoutParams params = (LayoutParams) videoListView
.getLayoutParams();
params.bottomMargin = 100;
videoListView.setLayoutParams(params);
layout_edit.setVisibility(View.VISIBLE);
edit_visiable = true;
selectedVideos.clear();
videoAdapter.notifyDataSetInvalidated();
}
private void hideEditLayout() {
RelativeLayout.LayoutParams params = (LayoutParams) videoListView
.getLayoutParams();
params.bottomMargin = 0;
videoListView.setLayoutParams(params);
layout_edit.setVisibility(View.INVISIBLE);
edit_visiable = false;
selectedVideos.clear();
videoAdapter.notifyDataSetInvalidated();
}
class VideoAdapter extends SimpleCursorAdapter {
// private Cursor c;
// private int layout;
// private final LayoutInflater inflater;
private ViewItemHolder holder;
@SuppressWarnings("deprecation")
public VideoAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
// this.c = c;
// this.layout = layout;
// this.inflater = LayoutInflater.from(context);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return super.newView(context, cursor, parent);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
holder = (ViewItemHolder) view.getTag();
if (holder == null) {
holder = new ViewItemHolder();
holder.imageView = (ImageView) view
.findViewById(R.id.video_thumbnail);
holder.titleView = (TextView) view
.findViewById(R.id.video_title);
holder.durationView = (TextView) view
.findViewById(R.id.video_duration);
holder.sizeView = (TextView) view.findViewById(R.id.video_size);
holder.cb = (CheckBox) view.findViewById(R.id.video_checkbox);
view.setTag(holder);
}
holder.imageIndex = cursor
.getColumnIndexOrThrow(MediaStore.Video.Media._ID);
holder.titleIndex = cursor
.getColumnIndexOrThrow(MediaStore.Video.Media.TITLE);
holder.durationIndex = cursor
.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION);
holder.sizeIndex = cursor
.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE);
holder.UrlIndex = cursor
.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
String title = cursor.getString(holder.titleIndex);
long duration = cursor.getLong(holder.durationIndex);
long size = cursor.getLong(holder.sizeIndex);
holder.titleView.setText(FormatHelper.formatTitle(title, 20));
holder.durationView.setText(FormatHelper.formatDuration(duration));
holder.sizeView.setText(FormatHelper.formatSize(size));
holder.Url = cursor.getString(holder.UrlIndex);
holder.id = cursor.getInt(holder.imageIndex);
imageHelper.loadBitmap(getContentResolver(), holder.imageView,
holder.id);
if (edit_visiable) {
holder.cb.setVisibility(View.VISIBLE);
holder.sizeView.setVisibility(View.GONE);
holder.cb.setChecked(selectedVideos.contains(holder.id));
} else {
holder.cb.setVisibility(View.GONE);
holder.sizeView.setVisibility(View.VISIBLE);
}
}
}
class ViewItemHolder {
public ViewItemHolder() {
}
public ViewItemHolder(ImageView img, TextView title, TextView duration,
TextView size, CheckBox ceb) {
imageView = img;
titleView = title;
durationView = duration;
sizeView = size;
cb = ceb;
}
ImageView imageView;
TextView titleView, durationView, sizeView;
CheckBox cb;
String Url;
long id;
int imageIndex;
int titleIndex;
int durationIndex;
int sizeIndex;
int UrlIndex;
}
}
缩略图的直接获取可以这样:
private Bitmap getThumbnail(ContentResolver contentResolver, long id) {
Bitmap thumbnail = null;
thumbnail = MediaStore.Video.Thumbnails.getThumbnail(contentResolver,
id, MediaStore.Video.Thumbnails.MINI_KIND, null);
return thumbnail;
}
列表布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF" >
<RelativeLayout
android:id="@+id/video_act_title"
android:layout_width="fill_parent"
android:layout_height="60dip"
android:background="#FA9638"
android:orientation="horizontal" >
<TextView
android:id="@+id/video_tv"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:gravity="center"
android:text="视频列表"
android:textColor="#FFFFFF"
android:textSize="@dimen/bookname_size" />
</RelativeLayout>
<ListView
android:id="@+id/video_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/video_act_title" >
</ListView>
<RelativeLayout
android:id="@+id/layout_edit"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#EEEEEE"
android:visibility="gone" >
<Button
android:id="@+id/video_edit_cancel"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="#00000000"
android:text="取消" />
<Button
android:id="@+id/video_edit_reverse"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="反选"
android:background="#00000000" />
<Button
android:id="@+id/video_edit_delete"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:background="#00000000"
android:text="删除" />
</RelativeLayout>
</RelativeLayout>
ListItem布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp" >
<RelativeLayout
android:id="@+id/video_item_detail"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/video_thumbnail"
android:layout_width="150dp"
android:layout_height="90dp"
android:scaleType="center"
android:adjustViewBounds="true"
android:background="@drawable/bg_border1"
android:src="@drawable/load_music" />
<TextView
android:id="@+id/video_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/video_thumbnail"
android:layout_marginLeft="10dp"
android:layout_marginTop="3dp"
android:layout_toRightOf="@id/video_thumbnail"
android:text="title" />
<TextView
android:id="@+id/video_duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/video_thumbnail"
android:layout_alignLeft="@id/video_title"
android:layout_marginBottom="3dp"
android:text="duration" />
<TextView
android:id="@+id/video_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/video_thumbnail"
android:layout_alignParentRight="true"
android:layout_marginBottom="3dp"
android:text="size" />
</RelativeLayout>
<CheckBox
android:id="@+id/video_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:visibility="gone" />
</RelativeLayout>
public class RemoteImageHelper {
private LruCache<Long, Bitmap> bitmapCache;
private void initLruCache() {
if (bitmapCache == null) {
int maxMemory = (int) Runtime.getRuntime().maxMemory();
int cacheSize = maxMemory / 8;
bitmapCache = new LruCache<Long, Bitmap>(cacheSize) {
@Override
protected int sizeOf(Long key, Bitmap value) {
return value.getByteCount();
}
};
}
}
public void loadBitmap(ContentResolver pContentResolver,
final ImageView imageView, final long id) {
loadBitmap(pContentResolver, imageView, id, true);
}
public void loadBitmap(final ContentResolver pContentResolver,
final ImageView imageView, final long id, boolean useCache) {
initLruCache();
Bitmap thumb = bitmapCache.get(id);
if (useCache && thumb != null) {
imageView.setImageBitmap(thumb);
return;
}
// You may want to show a "Loading" image here
imageView.setImageResource(R.drawable.image_indicator);
LoadBitmapTask loadBitmapTask = new LoadBitmapTask();
loadBitmapTask.execute(new LoadBitmapParams(pContentResolver, id,
imageView));
}
private Bitmap getThumbnail(ContentResolver contentResolver, long id) {
Bitmap thumbnail = null;
thumbnail = MediaStore.Video.Thumbnails.getThumbnail(contentResolver,
id, MediaStore.Video.Thumbnails.MINI_KIND, null);
return thumbnail;
}
class LoadBitmapTask extends AsyncTask<LoadBitmapParams, Void, Bitmap> {
private ImageView imageView;
@Override
protected Bitmap doInBackground(LoadBitmapParams... params) {
imageView = params[0].imageView;
Bitmap thumb = getThumbnail(params[0].contentResolver, params[0].id);
if (thumb != null) {
bitmapCache.put(params[0].id, thumb);
}
return thumb;
}
@Override
protected void onPostExecute(Bitmap result) {
if (result != null) {
imageView.setImageBitmap(result);
} else {
imageView.setImageResource(R.drawable.image_fail);
}
}
}
class LoadBitmapParams {
ContentResolver contentResolver;
long id;
ImageView imageView;
public LoadBitmapParams() {
}
public LoadBitmapParams(ContentResolver pContentResolver, long pId,
ImageView pImageView) {
contentResolver = pContentResolver;
id = pId;
imageView = pImageView;
}
}
}