如何使用图像视图和PagerAdapter滑动缩放图像

时间:2013-04-10 18:40:15

标签: java android

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

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

enter image description here

我尝试在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;
    }

}

}

1 个答案:

答案 0 :(得分: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);
}

并且在第636行的TouchImageView中添加onSwipeEvent(事件)如下:

        ......
            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,其中图像被缩放+。它适合我。