Gallery实现缩略图图片浏览

       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;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unifie(缩略图浏览)是一款缩略图查看软件,体积小巧,占用电脑资源少,但能提供给您最快的查看图片缩略图速度。 XP图像缩略图无法显示的解决方法: 可能是装了ACD的原因,文件夹的图片不能预览了,在网上看到一个解决的方法,很不错:) 症状:在XP或者其他操作系统里,在新装的系统里,图片文件在文件夹里显示缩略图的时候,你能看到图片的预览图像。但是由于一些原因出现后,这种预览图不再出现,给我们使用图像文件的时候带来了不少的麻烦。 原因:一般情况下,当我们安装了某些看图或者图像处理类软件(如ACDSee或者Photoshop)之后,这些软件会更改文件关联,让自己这成为开启某种图像文件格式的主程序。如果用户没用卸载程序而是强行删除这些软件,那么这些程序在Windows注册表中的文件关联还会保留,但由于该程序已被删除,所以Windows将无法打开原先可以支持的图片格式。 解决方法: 在“开始→运行”中输入“regsvr32 shimgvw.dll ”(启用图像预览); 然后运行“regsvr32 shmedia.dll” (启用影像预览)。 操作成功后会弹出窗口提示“……中的……成功”,按确定即可。 如果要取消预览,比如取消视频预览,运行“regsvr32 /u shmedia.dll”即可。 PS:我试过了,保证好用,有遇到此类问题的朋友不妨一试。 Unifie截图:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值