Android 图片浏览功能简单实现(画廊效果实现,支持放大缩小)

前言

APP日常小功能整理,别小瞧小功能的实现和积累,万一哪天使用到了,直接Ctrl+C和Ctrl+V轻松实现是不是节约了时间呢?同事也是日常Andro基础知识的复习点,哈哈哈!

首先上一波gif动态图看看效果过过瘾


示例代码使用的是viewpager,以下是viewpager详细介绍的地址可以参考着学习一下viewpager使用

http://blog.csdn.net/wangjinyu501/article/details/8169924

示例demo主逻辑代码:

package com.example.androidviewpagerphotopreview;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class MainActivity extends Activity implements OnPageChangeListener {

	/**
	 * 用于管理图片的滑动
	 */
	private ViewPager viewPager;

	/**
	 * 显示当前图片的页数
	 */
	private TextView pageText;
	private int[] image = { R.drawable.one, R.drawable.two, R.drawable.three, R.drawable.four, R.drawable.five, R.drawable.six };
	private List> list = new ArrayList>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		pageText = (TextView) findViewById(R.id.page_text);
		viewPager = (ViewPager) findViewById(R.id.view_pager);
		for (int i = 0; i < image.length; i++) {
			Map map = new HashMap();
			map.put("image", image[i]);
			list.add(map);
		}
		ViewPagerAdapter adapter = new ViewPagerAdapter(MainActivity.this,list);
		viewPager.setAdapter(adapter);
		viewPager.setCurrentItem(0);
		viewPager.setOnPageChangeListener(this);
		viewPager.setEnabled(false);
		// 设定当前的页数和总页数
		pageText.setText(1 + "/" + image.length);
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {

	}

	@Override
	public void onPageSelected(int arg0) {
		// 每当页数发生改变时重新设定一遍当前的页数和总页数
		pageText.setText((arg0+1) + "/" + image.length);
	}

	/**
	 * ViewPager的适配器
	 * 
	 * @author guolin
	 */
	class ViewPagerAdapter extends PagerAdapter {
		List> list;
		private Context context ;
		public ViewPagerAdapter(Context context,List> lists) {
			this.context = context;
			this.list=lists;
		}
		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			Bitmap bitmap = BitmapFactory.decodeResource(getResources(), list.get(position).get("image"));
			if (bitmap == null) {
				bitmap = BitmapFactory.decodeResource(getResources(),
						R.drawable.empty_photo);
			}
			View view = LayoutInflater.from(MainActivity.this).inflate(
					R.layout.zoom_image_layout, null);
			ZoomImageView zoomImageView = (ZoomImageView) view
					.findViewById(R.id.zoom_image_view);
			zoomImageView.setImageBitmap(bitmap);
			container.addView(view);
			return view;
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			View view = (View) object;
			container.removeView(view);
		}

	}
}

文章参考地址来自郭霖大神博客:

http://blog.csdn.net/guolin_blog/article/details/12646775

代码中读取的是drawable资源文件,换取本地读取详细厉害的你们是没问题的

demo下载地址:

http://download.csdn.net/detail/qq_31546677/9916961

有新的升级功能欢迎提出!



你可能感兴趣的:(Android 图片浏览功能简单实现(画廊效果实现,支持放大缩小))