gpt4 book ai didi

android - 使用 ViewPager 实现捏缩放

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:53:23 24 4
gpt4 key购买 nike

我正在使用 ViewPager 从远程服务器检索图像。一切正常,但问题是我不知道如何对其实现缩放功能。我的代码在下面

public class ImagePagerActivity extends BaseActivity {

private ViewPager pager;

private DisplayImageOptions options;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.ac_image_pager);
Bundle bundle = getIntent().getExtras();
String[] imageUrls = bundle.getStringArray(Extra.IMAGES);
int pagerPosition = bundle.getInt(Extra.IMAGE_POSITION, 0);

options = new DisplayImageOptions.Builder()
.showImageForEmptyUri(R.drawable.image_for_empty_url)
.cacheOnDisc()
.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
.build();

pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(new ImagePagerAdapter(imageUrls));
pager.setCurrentItem(pagerPosition);

}

protected void onStop() {
imageLoader.stop();
super.onStop();
}

private class ImagePagerAdapter extends PagerAdapter {

private String[] images;
private LayoutInflater inflater;

ImagePagerAdapter(String[] images) {
this.images = images;
inflater = getLayoutInflater();
}

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

public void finishUpdate(View container) {
}

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

public Object instantiateItem(View view, int position) {
final View imageLayout = inflater.inflate(R.layout.item_pager_image, null);
final ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);

imageLoader.displayImage(images[position], imageView, options, new ImageLoadingListener() {

public void onLoadingStarted() {
spinner.setVisibility(View.VISIBLE);
}


public void onLoadingFailed(FailReason failReason) {
String message = null;
switch (failReason) {
case IO_ERROR:
message = "Input/Output error";
break;
case OUT_OF_MEMORY:
message = "Out Of Memory error";
break;
case UNKNOWN:
message = "Unknown error";
break;
}
Toast.makeText(ImagePagerActivity.this, message, Toast.LENGTH_SHORT).show();

spinner.setVisibility(View.GONE);
imageView.setImageResource(android.R.drawable.ic_delete);
}


public void onLoadingComplete(Bitmap loadedImage) {
spinner.setVisibility(View.GONE);
Animation anim = AnimationUtils.loadAnimation(ImagePagerActivity.this, R.anim.fade_in);
imageView.setAnimation(anim);
anim.start();
}

public void onLoadingCancelled() {
// Do nothing
}
});

((ViewPager) view).addView(imageLayout, 0);
return imageLayout;
}

public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}

public void restoreState(Parcelable state, ClassLoader loader) {
}
public Parcelable saveState() {
return null;
}

public void startUpdate(View container) {
}
}
}

最佳答案

如果您的 viewpager 使用 imageView 那么您可以使用 touchImageView

https://github.com/MikeOrtiz/TouchImageView/blob/master/src/com/example/touch/TouchImageView.java

只需用这个自定义 View 替换您当前的 ImageView 。祝你好运

关于android - 使用 ViewPager 实现捏缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12880262/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com