项目DEMO地址:http://download.csdn.net/detail/wxk105/9562369
效果图
点击滑动效果图
点击放大效果图
mainActivity
项目简单只在必要处做声明,完整demo见结尾链接,看不懂请加Q164454216,验证:博客
public class MainActivity extends AppCompatActivity {
private List list;//用于接收后台图片地址集合
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();//填充数据
initView();//初始化控件
}
private void initData() {
for (int i=0;inew ImageInfo(urls[i],200,200);
list.add(imageInfo);
}
}
private void initView() {
GridView gv_pic = (GridView) findViewById(R.id.gv_pic);
gv_pic.setAdapter(new GvAdapter(this));
gv_pic.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
list=new ArrayList();
//点击位置及对象传入dialog
PicShowDialog dialog=new PicShowDialog(MainActivity.this,list,position);
dialog.show();
}
});
}
}
PicShowDialog用于图片展示dialog
public class PicShowDialog extends Dialog {
private Context context;
private View view;
private List imageInfos;//接收传入集合
private MyViewPager vp;
private List views = new ArrayList();//图片上圆点集合
private LayoutAnimationController lac;
private LinearLayout ll_point;
private ViewPagerAdapter pageAdapter;
private int position;//点击位置
private LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams(10, 10);
// 图片缓存 默认 等
private DisplayImageOptions optionsImag = new DisplayImageOptions.Builder()
.showImageForEmptyUri(R.mipmap.zanwutupian)
.showImageOnFail(R.mipmap.zanwutupian).cacheInMemory(true).cacheOnDisk(true)
.considerExifParams(true).imageScaleType(ImageScaleType.EXACTLY)
.bitmapConfig(Bitmap.Config.RGB_565).build();
public PicShowDialog(Context context, int themeResId) {
super(context, themeResId);
this.context = context;
}
public PicShowDialog(Context context, List imageInfos, int position) {
this(context, R.style.Pic_Dialog);
this.imageInfos = imageInfos;
this.position = position;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_dialog_pic);
getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
// getWindow().setLayout(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
vp = (MyViewPager) findViewById(R.id.vp);
ll_point = (LinearLayout) findViewById(R.id.ll_point);
// init();
initMyPageAdapter();
// vp.setAdapter(new ViewPagerAdapter());
vp.setCurrentItem(position);
//滑动监听设置图片滑动时,相应点跟着变化
vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int position) {
if (views.size() != 0 && views.get(position) != null) {
for (int i = 0; i < views.size(); i++) {
if (i == position) {
views.get(i).setBackgroundResource(R.drawable.point_focus2);
} else {
views.get(i).setBackgroundResource(R.drawable.point_normal2);
}
}
}
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
}
/***
* 初始化viewpager适配器
*/
private void initMyPageAdapter() {
initPoint();
if (pageAdapter == null) {
pageAdapter = new ViewPagerAdapter();
if (vp != null) {
vp.setAdapter(pageAdapter);
}
} else {
pageAdapter.notifyDataSetChanged();
}
}
//初始化圆点
private void initPoint() {
views.clear();
ll_point.removeAllViews();
if (imageInfos.size()==1){
ll_point.setVisibility(View.GONE);
}else {
for (int i = 0; i < imageInfos.size(); i++) {
View view = new View(context);
paramsL.setMargins(dip2px(context, 5), dip2px(context, 2), 0, dip2px(context, 5));
view.setLayoutParams(paramsL);
if (i == position) {
view.setBackgroundResource(R.drawable.point_focus2);
} else {
view.setBackgroundResource(R.drawable.point_normal2);
}
views.add(view);
ll_point.addView(view);
}
}
}
//viewpager适配器
private class ViewPagerAdapter extends PagerAdapter {
@Override
public int getCount() {
return imageInfos.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view =View.inflate(context, R.layout.item_pic_show, null);
PhotoView photoView = (PhotoView) view.findViewById(R.id.pic_pv);
//这里使用Imageloader加载框架,加载网络图片,图片地址给的是网络图片。 ImageLoader.getInstance().displayImage(imageInfos.get(position).getUrl(),photoView,optionsImag);
photoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
@Override
public void onPhotoTap(View view, float x, float y) {
dismiss();
}
});
((ViewPager) container).addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((View) object);
}
}
private int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
}
GridView适配器GvAdapter
public class GvAdapter extends BaseAdapter {
private Context context;
private List list;
// 图片缓存 默认 等
private DisplayImageOptions optionsImag = new DisplayImageOptions.Builder()
.showImageForEmptyUri(R.mipmap.zanwutupian)
.showImageOnFail(R.mipmap.zanwutupian).cacheInMemory(true).cacheOnDisk(true)
.considerExifParams(true).imageScaleType(ImageScaleType.EXACTLY)
.bitmapConfig(Bitmap.Config.RGB_565).build();
public GvAdapter(Context context, List list){
this.context=context;
this.list=list;
}
@Override
public int getCount() {
return list==null?0:list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView=View.inflate(context, R.layout.item_image_view, null);
ImageView iv_image= (ImageView) convertView.findViewById(R.id.iv_image);
ImageLoader.getInstance().displayImage(list.get(position).getUrl(),iv_image,optionsImag);
return convertView;
}
}
项目DEMO地址:http://download.csdn.net/detail/wxk105/9562369可进群下载:573239233