View Pager + ImageView +Pinch Zoom + Rotation [英] View Pager + ImageView +Pinch Zoom + Rotation

查看:14
本文介绍了View Pager + ImageView +Pinch Zoom + Rotation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Imageview 上实现 Pinch Zoom,在 View Pager 中类似于 Default Android Gallery.我在 GitHub 上找到了多个来源,但缩放和滑动仅适用于第一张图片.

I want to implement Pinch Zoom on Imageview, with in View Pager similar to Default Android Gallery. I have found multiple source over GitHub, But the zoom and sliding just work for only first image.

我尝试过的:

1.) TouchImageView

2.) PhotoView

3.) Android 触控图库

以上所有链接都适用于单个图像视图.但是,当涉及到 View pager 中的图像时,它们会出现一些故障,并且仅适用于 View Pager 中的第一张图像.当我们在视图寻呼机中滚动到第 3 个第 4 个图像时,如果图像被缩放,则拖动功能无法按预期工作.

All the above links works fine for single image view. But when it comes to Images in View pager, They have some glitches and only works fine for first image in the View Pager. When we scroll over to 3rd 4th image in view pager, Dragging functionality not working as expected if the image is zoomed.

如果有人知道这样做的好图书馆,请给我他们的链接.

Please if any one knows any good library for doing this, then provide me the link for them.

推荐答案

示例代码已推送到TouchImageView的主分支.这是 链接到示例活动ExtendedViewPager 的链接.

EDIT 2: Example code has been pushed to the master branch of TouchImageView. Here is a link to the example activity and a link to the ExtendedViewPager.

添加了将示例链接适配到 TouchImageView 的代码.注意:您将需要最新的代码,该代码当前位于 dev 分支中.将来,这将包含在 v1.2.0 中.如果 TouchImageView 覆盖 canScrollHorizo​​ntally,你知道你有最新的代码.

added code adapting the example link to TouchImageView. Note: you will need the latest code, which is currently in the dev branch. In the future, this will be included in v1.2.0. You know you have the latest code if TouchImageView overrides canScrollHorizontally.

第 1 步:扩展 ViewPager 并覆盖 canScroll 以调用 canScrollHorizo​​ntallyFroyo.

Step 1: Extend ViewPager and override canScroll to call canScrollHorizontallyFroyo.

public class ExtendedViewPager extends ViewPager {

public ExtendedViewPager(Context context) {
    super(context);
}

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

@Override
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
    if (v instanceof TouchImageView) {
        return ((TouchImageView) v).canScrollHorizontallyFroyo(-dx);
    } else {
        return super.canScroll(v, checkV, dx, x, y);
    }
}

}

第二步:修改TouchImageView,添加canScrollHorizo​​ntallyFroyo:

Step 2: Modify TouchImageView by adding canScrollHorizontallyFroyo:

public boolean canScrollHorizontallyFroyo(int direction) {
    return canScrollHorizontally(direction);
}

第 3 步:您的活动

public class TouchImageViewActivity extends Activity {

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ExtendedViewPager mViewPager = (ExtendedViewPager) findViewById(R.id.view_pager);
        setContentView(mViewPager);
        mViewPager.setAdapter(new TouchImageAdapter());
    }

    static class TouchImageAdapter extends PagerAdapter {

            private static int[] images = { R.drawable.img1, R.drawable.img2, R.drawable.img3 };

            @Override
            public int getCount() {
                    return images.length;
            }

            @Override
            public View instantiateItem(ViewGroup container, int position) {
                    TouchImageView img = new TouchImageView(container.getContext());
                    img.setImageResource(images[position]);
                    container.addView(img, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
                    return img;
            }

            @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;
            }

    }
}

第四步: main.xml

<com.example.touch.ExtendedViewPager 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/view_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<小时>

TouchImageView 实际上是我的项目.我目前在 a fixdev 分支 用于与 ViewPagers 集成,它将在即将发布的版本中推送到 master.不幸的是,此修复仅适用于 API 14 及更高版本,因为蜂窝和更早版本不调用 canScrollHorizo​​ntally.如果您需要支持较旧的 API,则需要在 ViewPager 中实施解决方法.这是一个例子.


TouchImageView is actually my project. I currently have a fix in the dev branch for integration with ViewPagers, which will be pushed to master in an upcoming release. Unfortunately, this fix is only applicable for API 14 and greater since honeycomb and earlier do not call canScrollHorizontally. If you need to support older APIs, then you will need to implement a workaround in your ViewPager. Here is an example.

这篇关于View Pager + ImageView +Pinch Zoom + Rotation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆