我正在尝试创建一个可以从给定 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;
}
}
}