对于很多应用而言,一个少不了的功能就是相册的查看,但是有没有一个封装好了的相册处理页面呢?接下来就来介绍我的相册查看器。
基于ViewPager的实现,是一个单独的Activity,只需传入存放图片的集合就行,这里通过intent的传递数据:pictureUrls = (ArrayList<String>) b.getSerializable("info");然后就是照片的位置,默认是第零个位置:currentPosition=intent.getIntExtra("position",0);接下来就是添加到viewpager当中了。直接上代码,粘贴复制到自己项目修改一下下就可以使用了。
import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.support.v4.view.PagerAdapter;import android.support.v4.view.ViewPager;import android.view.View;import android.view.ViewGroup;import android.widget.ImageView;import android.widget.TextView;import com.bumptech.glide.Glide;import java.util.ArrayList;public class ViewPagerActivity extends Activity { PRivate ViewPager mPager; private ArrayList<String> pictureUrls = new ArrayList<>(); private TextView text_button; private int currentPosition=0; @Override protected void onCreate(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<String>) 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(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { text_button.setText((position+1)+"/"+pictureUrls.size()); } @Override public void onPageScrollStateChanged(int state) { } }); mPager.setAdapter(new PagerAdapter() { @Override public int getCount() { return pictureUrls.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public Object instantiateItem(ViewGroup container, int position) { PhotoView view = new PhotoView(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(new View.OnClickListener() { @Override public void onClick(View v) { ViewPagerActivity.this.finish(); } }); return view; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } }); mPager.setCurrentItem(currentPosition); }}期间用了glide来添加图片,这个自行调整,如不需要也可以换用别的。照片查看当中可以左右滑动,也有下标,点击照片退回原位置。xml布局:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000" > <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="40dp" android:layout_marginBottom="40dp"/> <TextView 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"/> <TextView 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"/></RelativeLayout>
新闻热点
疑难解答