2010.11.29——— Gallery和ImageSwitcher实现图片和视频的画廊展示

2010.11.29——— Gallery和ImageSwitcher实现图片和视频的画廊展示


[color=red]1、展示图片[/color]

需求:
使用画廊来展示图片 并且图片是网络上的图片


[b]A:首先 layout.xml[/b]


 <?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">

<ImageSwitcher android:id="@+id/switcher"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
/>

<Gallery android:id="@+id/gallery"
android:background="#55000000"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"

android:gravity="center_vertical"
android:spacing="16dp"
/>

</RelativeLayout>



[b]B:Activity[/b]


 package com.huitu.project;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnTouchListener;
import android.view.animation.AnimationUtils;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewSwitcher;

import com.huitu.pojo.AD_TPXX;
import com.huitu.service.PicService;
import com.huitu.util.ImageUtil;
import com.huitu.util.JSONUtil;

public class PicListActivity extends Activity implements
AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory {

private List<ImageView> images = new ArrayList<ImageView>();
private List<String> descs = new ArrayList<String>();
private ImageSwitcher mSwitcher;


@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.piclist);

Intent intent = this.getIntent();
int id = intent.getIntExtra("id", 0);
System.out.println(id);

try {
String json = PicService.query(id);
if(!json.trim().equals("noValue")){
List<AD_TPXX> list = JSONUtil.parseJSON_Pic_list(json);
for(AD_TPXX bean : list){
ImageView iv = new ImageView(PicListActivity.this);
String path = "http://10.169.53.126:8080/CPJW_2"+bean.getPath();
path = path.replace("\\", "/");
System.out.println(path);
byte[] data = ImageUtil.getImage(path);
System.out.println(data.length);
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
iv.setImageBitmap(bitmap);
images.add(iv);
descs.add(bean.getDescr());
}
}else{
Toast.makeText(this, R.string.error_nopic, 0).show();
}

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
mSwitcher.setFactory(this);
mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));

Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemSelectedListener(this);



}

public void onItemSelected(AdapterView parent, View v, int position, long id) {
mSwitcher.setImageDrawable(images.get(position).getDrawable());
Toast.makeText(this, "图片描述:"+descs.get(position), 0).show();
}

public void onNothingSelected(AdapterView parent) {
}

public View makeView() {
ImageView i = new ImageView(this);
i.setBackgroundColor(0xFF000000);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
return i;
}

private class ImageAdapter extends BaseAdapter{
private Context mContext;
//定义Context
public ImageAdapter(Context c) {
//声明 ImageAdapter
mContext = c;
}
public int getCount() { //获取图片的个数
return images.size();
}
public Object getItem(int position) {
//获取图片在库中的位置
return position;
}
public long getItemId(int position) {
//获取图片在库中的位置
return position;
}
public View getView(int position, View convertView,
ViewGroup parent) {
ImageView i = images.get(position);
i.setAdjustViewBounds(true);
i.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
i.setBackgroundResource(R.drawable.picture_frame);
//设置比例类型
return i;
}

}

}


大致流程:

1、
extends Activity implements
AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory

2、根据网络图片路径地址 转换为字节流,然后 转换为位图文件

Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
iv.setImageBitmap(bitmap);

生成ImageView对象 并放到list里面去

3、设置(ImageSwitcher) 的一些动画效果
mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
mSwitcher.setFactory(this);
mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));

4、为Gallery自定义一个Adapter 并且
g.setAdapter(new ImageAdapter(this));

5、onItemSelected方法

mSwitcher.setImageDrawable(images.get(position).getDrawable());
Toast.makeText(this, "图片描述:"+descs.get(position), 0).show();

当选中一个图片是 在ImageSwitcher显示出来 并且吐司对应的图片信息



[color=red]2、展示视频[/color]

视频的画廊展示 其实和图片是一样的 不同就是 不需要ImageSwitcher 换成一个VideoView,
然后 当onItemSelected时 就在VideoView播放对应的视频 就可以了

但是 有个难点 就是 [color=red]如何获得视频的预览画面 这个倒是耽误我了很长时间[/color]


其实 很简单:


Bitmap  b = ThumbnailUtils.createVideoThumbnail(path,Video.Thumbnails.MICRO_KIND);
ImageView iv = new ImageView(this);


这样 就可以获得视频的缩略图了 呵呵


[b]A:layout.xml[/b]


 <?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">

<VideoView android:id="@+id/vv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
/>

<Gallery android:id="@+id/ggg"
android:background="#55000000"
android:layout_width="fill_parent"
android:layout_height="120dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"

android:gravity="center_vertical"
android:spacing="16dp"
/>

</RelativeLayout>




[b]B: Activity [/b]


package com.huitu.project;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;
import android.os.Bundle;
import android.provider.MediaStore.Video;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
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.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

import com.huitu.pojo.AD_SPXX;
import com.huitu.service.VideoService;
import com.huitu.util.ImageUtil;
import com.huitu.util.JSONUtil;

public class VideoListActivity extends Activity {
private List<String> videos = new ArrayList<String>();
private List<ImageView> images = new ArrayList<ImageView>();
private List<String> descs = new ArrayList<String>();
private MediaController control;
private VideoView video;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

setContentView(R.layout.videolist);

control = new MediaController(this);
video = (VideoView)findViewById(R.id.vv);

Intent intent = this.getIntent();
int id = intent.getIntExtra("id", 0);
System.out.println(id);

try {
String json = VideoService.query(id);
if(!json.trim().equals("noValue")){
List<AD_SPXX> list = JSONUtil.parseJSON_Video_list(json);
for(AD_SPXX bean : list){
String path = "http://10.169.53.126:8080/CPJW_2"+bean.getPath();
path = path.replace("\\", "/");
System.out.println(path);
videos.add(path);
descs.add(bean.getDescr());
Bitmap b = ThumbnailUtils.createVideoThumbnail(path,Video.Thumbnails.MICRO_KIND);
ImageView iv = new ImageView(this);
iv.setImageBitmap(b);
images.add(iv);
}
}else{
Toast.makeText(this, R.string.error_novideo, 0).show();
}

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

video.setVideoPath(videos.get(0));
control.setMediaPlayer(video);
video.setMediaController(control);
video.setBackgroundResource(0);
video.requestFocus();
video.start();

VideoAdapter adapter = new VideoAdapter(this);
System.out.println(adapter.getCount());

Gallery g = (Gallery) findViewById(R.id.ggg);

//定义 Gallery 控件
g.setAdapter(adapter);
//设置 Gallery 控件的图片源
g.setOnItemClickListener(new OnItemClickListener() {
//点击监听事件
public void onItemClick(AdapterView parent, View v, int position, long id) {//点击事件
if(video.isPlaying()){
video.stopPlayback();
}
video.setVideoPath(videos.get(position));
video.requestFocus();
video.start();
Toast.makeText(VideoListActivity.this, "视频描述:"+descs.get(position),0).show(); //Toast显示图片位置
}
});
g.setOnTouchListener(new OnTouchListener(){

public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}

});
}


private class VideoAdapter extends BaseAdapter{
private Context mContext;
int mGalleryItemBackground;
//定义Context
public VideoAdapter(Context c) {
//声明 ImageAdapter
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
public int getCount() { //获取图片的个数
return videos.size();
}
public Object getItem(int position) {
//获取图片在库中的位置
return position;
}
public long getItemId(int position) {
//获取图片在库中的位置
return position;
}
public View getView(int position, View convertView,
ViewGroup parent) {
ImageView i = images.get(position);
//给ImageView设置资源
i.setLayoutParams(new Gallery.LayoutParams(250, 250));
//设置布局 图片200×200显示
i.setBackgroundResource(mGalleryItemBackground);
//i.requestFocus();
return i;
}

}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值