对于很多应用而言,一个少不了的功能就是相册的查看,但是有没有一个封装好了的相册处理页面呢?接下来就来介绍我的相册查看器。
基于ViewPager的实现,是一个单独的Activity,只需传入存放图片的集合就行,这里通过intent的传递数据:pictureUrls = (ArrayList) b.getSerializable("info");然后就是照片的位置,默认是第零个位置:currentPosition=intent.getIntExtra("position",0);接下来就是添加到viewpager当中了。直接上代码,粘贴复制到自己项目修改一下下就可以使用了。
importandroid.app.Activity;
importandroid.content.Intent;
importandroid.os.Bundle;
importandroid.support.v4.view.PagerAdapter;
importandroid.support.v4.view.ViewPager;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.widget.ImageView;
importandroid.widget.TextView;
importcom.bumptech.glide.Glide;
importjava.util.ArrayList;
publicclassViewPagerActivityextendsActivity {
privateViewPager mPager;
privateArrayList pictureUrls =newArrayList<>();
privateTextView text_button;
privateintcurrentPosition=0;
@Override
protectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager);
text_button = (TextView) findViewById(R.id.text_button);
Intent intent = getIntent();
if(!intent.equals(null)) {
Bundle b = intent.getExtras();
if(b !=null) {
if(b.containsKey("info")) {
pictureUrls = (ArrayList) b.getSerializable("info");
}
}
}
if(intent.hasExtra("position")){
currentPosition=intent.getIntExtra("position",0);
}
text_button.setText(1+"/"+pictureUrls.size());
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setPageMargin((int) (getResources().getDisplayMetrics().density *15));
mPager.addOnPageChangeListener(newViewPager.OnPageChangeListener() {
@Override
publicvoidonPageScrolled(intposition,floatpositionOffset,intpositionOffsetPixels) {
}
@Override
publicvoidonPageSelected(intposition) {
text_button.setText((position+1)+"/"+pictureUrls.size());
}
@Override
publicvoidonPageScrollStateChanged(intstate) {
}
});
mPager.setAdapter(newPagerAdapter() {
@Override
publicintgetCount() {
returnpictureUrls.size();
}
@Override
publicbooleanisViewFromObject(View view, Object object) {
returnview == object;
}
@Override
publicObject instantiateItem(ViewGroup container,intposition) {
PhotoView view =newPhotoView(ViewPagerActivity.this);
view.enable();
view.setScaleType(ImageView.ScaleType.FIT_CENTER);
// text_button.setText(position+"/"+pictureUrls.size());
Glide.with(ViewPagerActivity.this).load(pictureUrls.get(position)).into(view);
// view.setImageResource(pictureUrls[position]);
container.addView(view);
view.setOnClickListener(newView.OnClickListener() {
@Override
publicvoidonClick(View v) {
ViewPagerActivity.this.finish();
}
});
returnview;
}
@Override
publicvoiddestroyItem(ViewGroup container,intposition, Object object) {
container.removeView((View) object);
}
});
mPager.setCurrentItem(currentPosition);
}
}
期间用了glide来添加图片,这个自行调整,如不需要也可以换用别的。照片查看当中可以左右滑动,也有下标,点击照片退回原位置。
xml布局:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
>
android:id="@+id/pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_marginBottom="40dp"/>
android:id="@+id/text_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1/10"
android:textColor="#fff"
android:textSize="14sp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="15dp"/>
android:id="@+id/delete_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="15dp"
android:layout_alignParentRight="true"
android:paddingLeft="40dp"
android:paddingTop="5dp"
android:paddingRight="30dp"
android:text="删除"
android:textSize="14sp"
android:textColor="#ffffff"
android:visibility="gone"/>
csdn项目地址:http://blog.csdn.net/greatdaocaoren/article/details/54890825