您的位置:首页 > 其它

Gallery实现缩略图图片浏览

2016-04-20 20:46 274 查看
       Gallery介绍

            我们有时候在手机上或PC上面看到动态的图片,可以通过鼠标或者手指触摸移动它,产生动态的图片滚动效果,还可以根据你的点击或者触摸触发其他时间的响应。同样的,在Android中也可提供这种实现,这就是通过Gallery在UI上实现缩略图浏览器。

      其中用到适配器使用继承BaseAdapter的方法来实现所需的适配器。

      BaseAdapter中的重要方法

      public int getCount()     返回已定义的数据源的总数量

      public Object getItem(int position)

      public long getItemId(int position)

       告诉适配器取得目前容器中的数据ID和对象

     public View getView(int position,View convertView,ViewGroup parent)  

         取得目前欲显示的图像View,传入数组ID值使之读取与成像

     当点击缩略图时显示大图片,图片展示使用ImageSwitcher,ImageSwitcher和ImageView类似都是用来展示图片的,但ImageSwitcher相当与承载图

片,展示图片时会比ImageView更炫,有自定义动画效果。

      在使用ImageSwitcher时要实现ViewFactory中的makeView()的抽象方法加载一个指定特征的图片。

   具体实现代码如下:

package com.example.mhy.demo;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

/**
* Created by mhy on 2016/4/20.
*/
public class GalleryAdapter extends BaseAdapter {

private Context context;
private int[] res;

public GalleryAdapter(Context context, int[] res){

this.context = context;
this.res = res;

}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}

@Override
public Object getItem(int position) {
return res[position];
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

ImageView image = new ImageView(context);
image.setBackgroundResource(res[position%res.length]);
image.setLayoutParams(new Gallery.LayoutParams(200, 150));
image.setScaleType(ScaleType.FIT_XY);

return image;
}
}

package com.example.mhy.demo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.ViewSwitcher.ViewFactory;

public class MainActivity extends AppCompatActivity implements OnItemSelectedListener,ViewFactory {

private int[]res = {R.mipmap.item1,R.mipmap.item2,R.mipmap.item3,R.mipmap.item4,
R.mipmap.item5,R.mipmap.item6,R.mipmap.item7,R.mipmap.item8,
R.mipmap.item9,R.mipmap.item10,R.mipmap.item11,R.mipmap.item12};
private Gallery mGallery;
private GalleryAdapter mAdapter;
private ImageSwitcher mImageSwitcher;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mGallery = (Gallery) findViewById(R.id.gallery);
mImageSwitcher = (ImageSwitcher)findViewById(R.id.imageSwitcher);
// gallery加载适配器
mAdapter = new GalleryAdapter(this,res);
mGallery.setAdapter(mAdapter);
mGallery.setOnItemSelectedListener(this);
mImageSwitcher.setFactory(this);
mImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
mImageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));

}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

mImageSwitcher.setBackgroundResource(res[position%res.length]);

}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}

@Override
public View makeView() {
ImageView image = new ImageView(this);
image.setScaleType(ScaleType.CENTER);
return image;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: