ViewPager + PhotoView-->图片查看器,类似今日头条图片查看

新手,只有实现...


这是类似于今日头条中很多图片的时候的图片查看效果,先上效果图 :


    由于图片大小限制,不能上传太长时间的图片,大致就是以上效果

 三步走战略 :

1.  添加依赖  :  (两个都要加,具体添加可以查看 PhoneView链接)

  project下 :

allprojects {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

app 下 : 

compile 'com.github.chrisbanes:PhotoView:2.0.0'
compile 'com.squareup.picasso:picasso:2.5.2'   // Picasso

下面是github上的链接 : (可以自己研究,另外你可以自己以PhotoView为关键词再搜搜看看其他框架)


 PhoneView链接


看到另外一个也可以 ---> 链接 : 另一个PhotoView


2.布局文件:

xml version="1.0" encoding="utf-8"?>
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


            android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>


3. 具体代码

 PhotoViewPager 代码 :  ★★★★

package ce.phoneview;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

public class PhotoViewPager extends ViewPager {
    public PhotoViewPager(Context context) {
        super(context);
    }

    public PhotoViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        try {
            return super.onInterceptTouchEvent(ev);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            return false;
        }
    }
}

MainActivity 代码 :  主界面代码 ★★★★

package ce.phoneview;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;

import com.github.chrisbanes.photoview.PhotoView;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private String mPath1 = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505712009881&di=6fbf9a376801f6d210594b4ea0828263&imgtype=0&src=http%3A%2F%2Fimage.tianjimedia.com%2FuploadImages%2F2016%2F336%2F03%2FI2F84A86VB5N.jpg";
    private String mPath2 = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505712308596&di=31486b1a0d3a13b44c2a1bff4877d02b&imgtype=jpg&src=http%3A%2F%2Fc.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2F14ce36d3d539b60020610daae350352ac65cb77b.jpg";
    private String mPath3 = "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3824947408,1425426099&fm=11&gp=0.jpg";
    private PhotoViewPager mViewPager;
    private ArrayList mArrayList = new ArrayList<>();
    private ArrayList mImageViewLists = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mViewPager = (PhotoViewPager) findViewById(R.id.viewpager);
        initData();
        MyAdapter myAdapter = new MyAdapter();
        mViewPager.setAdapter(myAdapter);
    }

    private void initData() {
        mArrayList.add(mPath1);
        mArrayList.add(mPath2);
        mArrayList.add(mPath3);
        // 添加图片
        for (int i = 0; i < mArrayList.size(); i++) {
            PhotoView imageview = new PhotoView(this);
            mImageViewLists.add(imageview);
        }
    }

    class MyAdapter extends PagerAdapter {

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

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            PhotoView imageview = mImageViewLists.get(position);
            Picasso.with(MainActivity.this).load(mArrayList.get(position)).into(imageview);
            container.addView(imageview);
            return imageview;
        }

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


你可能感兴趣的:(图片查看,ViewPager,PhotoView)