1

我正在尝试创建一个可以从给定 URL 加载图像的图像查看器。下面的代码实现了用户界面。

我的意图是让用户缩放图像并使用 Swipe 事件移动到下一个图像。但问题是当我缩放然后滑动时,它不会显示剩余部分,而是移动到下一个图像。

在此处输入图像描述

我尝试在 TouchImageView 的(https://github.com/MikeOrtiz/TouchImageView)onTouchListener中使用 requestDisallowInterceptTouchEvent。在此之后,剩余部分可以显示,但现在我无法转到下一页。我想知道如何实现这一点,因为事件只能转到 TouchView 或 PageAdapter

public class PageActivity extends Activity {
private int numPages = 33;
private TouchImageView[] imageViews = new TouchImageView[numPages];

private String URL = "http://www.smbc-comics.com/comics/200905";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ViewPager viewPager = new ViewPager(this);

    for (int i = 0; i < numPages; i++) {
        imageViews[i] = new TouchImageView(this);
        imageViews[i].setBackgroundResource(R.drawable.banke);
        imageViews[i].setMaxZoom(4f);
    }

    setContentView(viewPager);
    ImagePagerAdapter adapter = new ImagePagerAdapter();
    viewPager.setAdapter(adapter);
    viewPager.setOffscreenPageLimit(2);
}

@SuppressWarnings("unused")
private class ImagePagerAdapter extends PagerAdapter {
    @Override
    public int getCount() {
        return numPages;
    }

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

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Context context = PageActivity.this;

        String pageURL = URL;

        if (imageViews[position].getDrawable() == null) {
          ImageFetcher  imagefetcher = new ImageFetcher();
            imagefetcher.execute(
                    pageURL + String.format("%02d", position+1) + ".gif",
                    String.valueOf(position));
        }


        ((ViewPager) container).addView(imageViews[position], 0);
        return imageViews[position];
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {

        ((ViewPager) container).removeView((TouchImageView) object);
        imageViews[position].setImageDrawable(null);

    }

}
public class ImageFetcher extends AsyncTask<String, Integer, Drawable> {
    int fillthisPos;

    public Drawable doInBackground(String... urls) {
        try {
            InputStream is = (InputStream) new URL(urls[0]).getContent();
            fillthisPos = Integer.parseInt(urls[1]);
            Drawable d = Drawable.createFromStream(is, "src name");
            return d;
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    protected void onPostExecute(Drawable result) {
        super.onPostExecute(result);
        imageViews[fillthisPos].setImageDrawable(result);
        result = null;
    }

}

}

4

1 回答 1

2

您可以在 TouchImageView 类中添加以下代码:

public boolean isZoomed () {
    return (normalizedScale > minScale);
}

private void onSwipeEvent(MotionEvent event) {
    boolean zoomed = this.isZoomed();

    if (!zoomed && (swipeLen > 0)) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            swipeStartPos = (int) event.getRawX();
        }
        else if (event.getAction() == MotionEvent.ACTION_MOVE) {
            int distance = ((int) event.getRawX()) - swipeStartPos;
            int swipeVector = SWIPE_RIGHT;
            if (distance < 0) swipeVector = SWIPE_LEFT;

            if (Math.abs(distance) > swipeLen) {
                onSwipeHandler.onSwipe(swipeVector);

                swipeStartPos = (int) event.getRawX();
                this.setScaleX(1f);
            }
            else {
                int swipeStDist = swipeLen - Math.round(((swipeLen / 100) * 50));
                if (Math.abs(distance) > swipeStDist) {
                    this.setScaleX(0.98f);
                }
            }
        }
        else if (event.getAction() == MotionEvent.ACTION_UP) {
            swipeStartPos = (int) event.getRawX();
            this.setScaleX(1f);
        }
    }
}

public static int SWIPE_LEFT = 0;
public static int SWIPE_RIGHT = 1;
    public int swipeStartPos = 0;
public onSwipeListener onSwipeHandler = new onSwipeListener() {
    public void onSwipe(int vector) {}
};
public static int swipeLen = 0;

public void setOnSwipeListener(onSwipeListener c, int swipeLength) {
    onSwipeHandler = c;
    swipeLen = swipeLength;
}
public interface onSwipeListener {
    public void onSwipe(int vector);
}

并且还在 TouchImageView 的第 636 行下面添加 onSwipeEvent(event) ,如下所示:

        ......
            setImageMatrix(matrix);
            onSwipeEvent(event);
            //
            // indicate event was handled
            //
            return true;
        ...........

在您的代码之后,您可以添加滑动事件侦听器,如下所示:

imageView.setOnSwipeListener(new TouchImageView.onSwipeListener() {
    @Override
    public void onSwipe(int vector) {
        if (vector == TouchImageView.SWIPE_LEFT) {
            Log.d("swipe", "swipe left!");
        }
        else if (vector == TouchImageView.SWIPE_RIGHT) {
            Log.d("swipe", "swipe right!");
        }
    }
}, 200);  //length of swiping - 200 dip

此 onSwipeListener 忽略图像缩放的 onswipe +。它对我有用。

于 2014-03-28T13:53:19.517 回答