【android学习笔记】PhotoView+ViewPager+Picasso结合使用,PhotoView之启用缩放功能

主要代码如下:

imageView.enable();//启用缩放功能

若要关闭缩放功能:

imageView.disenable();

photoView+Picasso+viewPager整体代码如下:

package menu.bottombar.bottombar.Activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bm.library.PhotoView;
import com.squareup.picasso.Picasso;

import java.util.List;
import java.util.Map;

import menu.bottombar.bottombar.Config.Config;
import menu.bottombar.bottombar.R;

public class ImageBrowser extends AppCompatActivity implements ViewPager.OnPageChangeListener{
    private List> imgList;
    private String index;
    private ViewPager viewPager;
    private ImageView[] imageArr;
    private LinearLayout layout;
    int currentIndex = 0;
    private ImagePagerAdapter pagerAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_browser);
        initView();
        initIntent();
        initData();
    }

    private void initData() {

        pagerAdapter = new ImagePagerAdapter(this);
        viewPager.setAdapter(pagerAdapter);
        viewPager.setCurrentItem(currentIndex);
        viewPager.setOnPageChangeListener(this);
        this.layout = (LinearLayout) ((LinearLayout) findViewById(R.id.img));

    }

    private void initIntent() {
        Intent intent=getIntent();
        imgList= (List>) intent.getSerializableExtra("imgList");
        index=(String) intent.getStringExtra("index");

        currentIndex=Integer.parseInt(index);
        if (currentIndex < 0 || currentIndex >= imgList.size()) {
            currentIndex = 0;
        }
        Toast.makeText(ImageBrowser.this,"+"+index,Toast.LENGTH_LONG).show();
        Log.e("imgT",imgList+"");


    }
    private class ImagePagerAdapter extends PagerAdapter {

        private LayoutInflater inflater;
        private Context mContext;

        ImagePagerAdapter(Context context) {
            this.mContext = context;
            inflater = getLayoutInflater();
        }

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

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

        @Override
        public Object instantiateItem(ViewGroup view, int position) {
            View imageLayout = inflater.inflate(
                    R.layout.image_browser_item, view, false);
            PhotoView imageView = (PhotoView) imageLayout
                    .findViewById(R.id.img);
            Picasso.get()
                    .load(Config.baseUrl+"remote/upload/images/" +imgList.get(position).get("img"))
                    .placeholder(R.drawable.icon_nopic)
                    .into(imageView);
            imageView.enable();//启用缩放功能
            ((ViewPager) view).addView(imageLayout, 0);
            return imageLayout;
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

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

    private void initView() {
        viewPager=(ViewPager)findViewById(R.id.image_browser);

    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        TextView textView=(TextView)findViewById(R.id.img_poi);
        int page=position+1;
        textView.setText(page+"/"+imgList.size());
    }

    @Override
    public void onPageSelected(int position) {
        if (position < 0||imageArr==null)
            return;
        this.imageArr[position].setEnabled(false);
        this.imageArr[this.currentIndex].setEnabled(true);
        this.currentIndex = position;
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }
}

 

你可能感兴趣的:(android)