实现将一个存放图片的集合赋值到Photoview中,并用Viewpager展示出来
MainActivity类
public class MainActivity extends AppCompatActivity { private List<String> url=new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); TextView textView = (TextView) findViewById(R.id.tvIntent); url.add("http://7xi8d6.com1.z0.glb.clouddn.com/2017-05-05-18251898_1013302395468665_8734429858911748096_n.jpg"); url.add("http://7xi8d6.com1.z0.glb.clouddn.com/2017-05-04-18299181_1306649979420798_1108869403736276992_n.jpg"); url.add("http://ww1.sinaimg.cn/large/61e74233ly1feuogwvg27j20p00zkqe7.jpg"); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //跳转到大图页面 new MyDialog(MainActivity.this,url).show(); } }); }
自定义的一个viewpager类(为了避免viewpager与photoview的滑动冲突)
public class MyViewpager extends ViewPager { public MyViewpager(Context context) { super(context); } public MyViewpager(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { //为了避免viewpager与photoview的滑动冲突 try { return super.onInterceptTouchEvent(ev); } catch (Exception e) { e.printStackTrace(); return false; } } }
Dialog类
public class MyDialog extends Dialog { private List<String> url; private Activity context; public MyDialog(@NonNull Activity context, List<String> url) { //调用系统构造方法,给dialog设置style样式 super(context,R.style.dialog_theme); this.url=url; this.context=context; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.dialog_layout); //拿屏幕的宽高,只有activity有getWindowManager这个方法,所有我们要强转成activity, // dialog是挂载到activity上的,直接getContext就是他的activity WindowManager windowManager = context.getWindowManager(); DisplayMetrics displayMetrics = new DisplayMetrics(); windowManager.getDefaultDisplay().getMetrics(displayMetrics); int widthPixels = displayMetrics.widthPixels; int heightPixels = displayMetrics.heightPixels; //设置diaolog为全屏 Window window = getWindow(); WindowManager.LayoutParams attributes = window.getAttributes(); attributes.x=0; attributes.y=0; attributes.width=widthPixels; attributes.height=heightPixels; window.setAttributes(attributes); //找到自己定义的viewpager控件 MyViewpager myViewpager = (MyViewpager) findViewById(R.id.myViewpager); final TextView tvTitle = (TextView) findViewById(R.id.tvTitle); MyViewpagerAdapter myViewpagerAdapter = new MyViewpagerAdapter(getContext(),url); myViewpager.setAdapter(myViewpagerAdapter); tvTitle.setText("1/"+url.size()); myViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { tvTitle.setText(position+1+"/"+url.size()); } @Override public void onPageScrollStateChanged(int state) { } }); } }
style样式类
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <style name="dialog_theme" parent="@android:style/Theme.Dialog"> <!-- Customize your theme here. --> <item name="android:windowBackground">@color/transparent</item> <item name="android:background">@color/black</item> <item name="android:backgroundDimEnabled">true</item> <item name="android:windowIsTranslucent">true</item> </style> </resources>
colors样式类
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark">#303F9F</color> <color name="colorAccent">#FF4081</color> <color name="black">#000000</color> <color name="transparent">#00000000</color> </resources>
Application类(该类需在清单文件的application标签下声明一下)
<application android:name=".app.MyApplication"></application>
该类为了配置Imageloader的全局配置
imageloader依赖了
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); initImageLoader(this); } public static void initImageLoader(Context context){ String path = Environment.getExternalStorageDirectory() + "/" + "Image"; File file = new File(path); //File cacheDir= StorageUtils.getOwnCacheDirectory(context,"universalimageloader/Cache"); ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(context) .memoryCacheExtraOptions(100, 100)//配置内存缓存图片的尺寸 .memoryCacheSize(2 * 1024 * 1024)//配置内存缓存的大小 例如 : 2* 1024 * 1024 = 2MB .threadPoolSize(3)//配置加载图片的线程数 .threadPriority(1000)//配置线程的优先级 .diskCache(new UnlimitedDiskCache(file))//UnlimitedDiskCache 限制这个图片的缓存路径 .diskCacheSize(50 * 1024 * 1024)//在sdcard缓存50MB .diskCacheFileNameGenerator(new Md5FileNameGenerator())//MD5这种方式生成缓存文件的名字 .diskCacheFileCount(20)//配置sdcard缓存文件的数量 .writeDebugLogs() //打印加载图片的log日志,跟据自己的需求配置 .build();//配置构建完成 ImageLoader.getInstance().init(configuration); } }
MyViewpagerAdapter类(设置布局显示)
其中PhotoView依赖了
compile 'com.github.chrisbanes.photoview:library:+'
public class MyViewpagerAdapter extends PagerAdapter { private Context context; private List<String> url; public MyViewpagerAdapter(Context context, List<String> url){ this.context=context; this.url=url; } @Override public int getCount() { return url.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view==object; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } @Override public Object instantiateItem(ViewGroup container, int position) { PhotoView photoView = new PhotoView(context); ImageLoader.getInstance().displayImage(url.get(position),photoView); container.addView(photoView); return photoView; } }