捏缩放和viewpager不能同时工作

时间:2017-09-25 04:04:51

标签: java android android-viewpager android-imageview pinchzoom

我是Android应用程序开发的新手,我创建了一个ImageGallery应用程序。我正在尝试为图库中的图像实现捏合和缩放。我正在使用https://github.com/chrisbanes/PhotoView来实现缩放和捏合。现在问题是,它无法正常使用视图寻呼机。如果我从代码中删除photoView.setVisibility(View.GONE);,我可以缩放图片,但是viewpager停止工作,我无法刷图像。如果我添加photoView.setVisibility(View.GONE);,我可以滑动图片,但无法缩放。我怎样才能解决这个问题 ?

FullImageActivity.java

public class FullImageActivity extends AppCompatActivity {
    ImageView images;
    int position;
    int folderPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_full_image);
        Intent i = getIntent();
        PhotoView photoView = (PhotoView) findViewById(R.id.photo_view);
        photoView.setVisibility(View.GONE);

        // Selected image id
        position = i.getExtras().getInt("id");
        folderPosition = i.getExtras().getInt("folderPosition");
        Bundle extras = getIntent().getExtras();
        String value = extras.getString("abc");

        Glide.with(FullImageActivity.this)
                .load(value)
                .skipMemoryCache(false)
                .into(photoView);

        ViewPager mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mViewPager.setAdapter(new TouchImageAdapter(this,al_images, folderPosition));
        mViewPager.setCurrentItem(position);
    }
}

activity_full_image.xml

<?xml version="1.0" encoding="utf-8"?>

     <LinearLayout
         xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/jazzy_pager"
         android:layout_width="match_parent"
         android:layout_height="match_parent">

    <com.github.chrisbanes.photoview.PhotoView
        android:id="@+id/photo_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:scaleType="centerCrop"/>


     <android.support.v4.view.ViewPager
         android:id="@+id/viewpager"
         android:layout_width="match_parent"
         android:layout_height="match_parent" />

</LinearLayout>

TouchImageAdapter.java

class TouchImageAdapter extends PagerAdapter {
    Context context;
    String filename;
    ArrayList<Model_images> al_menu = new ArrayList<>();
    int position,int_position;

    public TouchImageAdapter(Context context,ArrayList<Model_images> al_menu, int position){
        this.al_menu = al_menu;
        this.context = context;
        this.int_position = position;
    }

    @Override
    public int getCount() {
        return al_menu.get(int_position).getAl_imagepath().size();
    }

    @Override
    public View instantiateItem(ViewGroup container, int position) {

        ImageView img = new ImageView(container.getContext());
        ViewGroup.LayoutParams lp= new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        img.setLayoutParams(lp);
        img.setImageDrawable(getImageFromSdCard(filename,position));
        container.addView(img, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        return img;
    }

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

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

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


    public Drawable getImageFromSdCard(String imageName,int position) {
        Drawable d = null;
        try {
            String path = al_menu.get(int_position).getAl_imagepath().get(position)
                    + "/";
            Bitmap bitmap = BitmapFactory.decodeFile(path);
            d = new BitmapDrawable(context.getResources(),bitmap);
        } catch (IllegalArgumentException e) {
            // TODO: handle exception
        }

        return d;


    }
}

1 个答案:

答案 0 :(得分:0)

如果要使用ViewPager滑动图像并放大图像,则需要使用viewpager进行一些自定义。其他很少的东西是你必须保持viewpager和imageview的触摸。检查how you can create viewpager with zoom-in image?。我希望它对你有所帮助,你会得到你的解决方案