1. 程式人生 > >Android 圖片檢視器實現 PhotoView + ViewPager

Android 圖片檢視器實現 PhotoView + ViewPager

今天看了今日頭條APP的圖片檢視的效果,就自己實現了一下。

效果如下


首先引入兩個庫

一個是圖片縮放的功能:PhotoView框架

compile 'com.github.chrisbanes:PhotoView:1.2.6'

一個介面滑動退出效果(比如自己滑動空白區域,就退出介面,而不用按手機返回鍵。我這裡實現上下滑動退出介面)Slider框架

compile 'com.komi.slider:slider:0.4.0'

專案目錄如下:


佈局檔案activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.gps.MainActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="120dp">

        <ImageView
            android:id="@+id/photo_1"
            android:layout_width="0dp"
            android:layout_height="100dp"
            android:layout_weight="1"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:background="@mipmap/photo_1"/>

        <ImageView
            android:id="@+id/photo_2"
            android:layout_width="0dp"
            android:layout_height="100dp"
            android:layout_weight="1"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:background="@mipmap/photo_2"/>

        <ImageView
            android:id="@+id/photo_3"
            android:layout_width="0dp"
            android:layout_height="100dp"
            android:layout_weight="1"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:background="@mipmap/photo_3"/>
    </LinearLayout>

</RelativeLayout>

activity_photoview.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.support.v4.view.ViewPager
            android:id="@+id/ViewPager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#000000"/>

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#ffffff"
            android:text="1/3"
            android:textSize="25dp"
            android:layout_marginTop="15dp"
            android:layout_marginLeft="15dp"/>
</RelativeLayout>

PhotoViewActivity

package com.example.gps;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
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.komi.slider.SliderConfig;
import com.komi.slider.SliderUtils;
import com.komi.slider.position.SliderPosition;
import java.util.ArrayList;
import uk.co.senab.photoview.PhotoViewAttacher;


public class PhotoViewActivity extends FragmentActivity {
    private ViewPager viewPager;
    private PagerAdapter pagerAdapter;
    private SliderConfig mConfig;
    private ArrayList<ImageView> mList;
    private TextView textView;
    private int imageIds[]={R.mipmap.photo_1,R.mipmap.photo_2,R.mipmap.photo_3};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photoview);
        initSilder();
        initView();
        initShowphoto();
    }

    private void initShowphoto() {
        //Bundle bundle = getIntent().getExtras();
        Intent intent = getIntent();
        int i = intent.getIntExtra("showphoto",0);
        setSelect(i);//通過傳值獲取點選圖片位置,從而顯示當前圖片
    }
    //實現介面上下滑動退出介面效果
    private void initSilder() {
        mConfig = new SliderConfig.Builder()
                .secondaryColor(Color.TRANSPARENT)
                .position(SliderPosition.VERTICAL)  //設定上下滑動
                .edge(false)  //是否允許有滑動邊界值,預設是有的true
                .build();
        SliderUtils.attachActivity(this, mConfig);
    }

    private void initView() {
            viewPager=(ViewPager)findViewById(R.id.ViewPager);
            textView=(TextView)findViewById(R.id.text);

            mList = new ArrayList<ImageView>();

           pagerAdapter = new PagerAdapter() {

               // 獲取要滑動的控制元件的數量,在這裡我們以滑動的廣告欄為例,那麼這裡就應該是展示的廣告圖片的ImageView數量
               @Override
               public int getCount() {
                   return imageIds.length;

                   //return Integer.MAX_VALUE;    返回一個比較大的值,目的是為了實現無限輪播
               }

               // 來判斷顯示的是否是同一張圖片,這裡我們將兩個引數相比較返回即可
               @Override
               public boolean isViewFromObject(View view, Object object) {
                   return view == object;
               }

               // 當要顯示的圖片可以進行快取的時候,會呼叫這個方法進行顯示圖片的初始化,
               // 我們將要顯示的ImageView加入到ViewGroup中,然後作為返回值返回即可
               @Override
               public Object instantiateItem(ViewGroup container, int position) {
                   ImageView imageView = new ImageView(PhotoViewActivity.this);
                   imageView.setImageResource(imageIds[position]);

                   //使圖片具有放縮功能
                   PhotoViewAttacher mAttacher=new PhotoViewAttacher(imageView);
                   mAttacher.update();

                   container.addView(imageView);
                   mList.add(imageView);
                   return imageView;
               }

               //PagerAdapter只快取三張要顯示的圖片,如果滑動的圖片超出了快取的範圍,就會呼叫這個方法,將圖片銷燬
               @Override
               public void destroyItem(ViewGroup container, int position, Object object) {
                   container.removeView(mList.get(position));
               }


           };

            viewPager.setAdapter(pagerAdapter);

            //設定滑動監聽事件
            viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                //滑動到第幾張圖片的呼叫的方法,position當前顯示圖片位置
                @Override
                public void onPageSelected(int position) {
                    switch (position){
                        case 0:
                            setSelect(0);
                            textView.setText("1/3");
                            break;
                        case 1:
                            setSelect(1);
                            textView.setText("2/3");
                            break;
                        case 2:
                            textView.setText("3/3");
                            setSelect(2);
                            break;
                    }
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });

    }

    //啟動檢視圖片時,顯示你點選圖片
    public void setSelect(int i) {
        switch (i){
            case 0:
                viewPager.setCurrentItem(0);
                break;
            case 1:
                viewPager.setCurrentItem(1);
                break;
            case 2:
                viewPager.setCurrentItem(2);
                break;
        }
    }

}

MainActivity

package com.example.gps;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private ImageView imageView_01,imageView_02,imageView_03;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

    }


    private void initView() {
        imageView_01=(ImageView)findViewById(R.id.photo_1);
        imageView_02=(ImageView)findViewById(R.id.photo_2);
        imageView_03=(ImageView)findViewById(R.id.photo_3);

        imageView_01.setOnClickListener(this);
        imageView_02.setOnClickListener(this);
        imageView_03.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(MainActivity.this,PhotoViewActivity.class);
        switch (v.getId()){
            case R.id.photo_1:
                intent.putExtra("showphoto",0);
                startActivity(intent);
                break;
            case R.id.photo_2:
                intent.putExtra("showphoto",1);
                startActivity(intent);
                break;
            case R.id.photo_3:
                intent.putExtra("showphoto",2);
                startActivity(intent);
                break;
        }
    }
}