gpt4 book ai didi

android - Photoview 图像边界不改变

转载 作者:行者123 更新时间:2023-11-29 01:48:16 26 4
gpt4 key购买 nike

我在 ViewPager 中使用 Chris Banes 的 PhotoView 和 Universal Image Loader。一切正常,除了当图像被放大时,无论是通过双击还是手势,图像都会放大,但图像的原始尺寸不会改变。这意味着,例如,如果图像显示为 100x200,则图像可以放大,但 ImageView 仍为 100x200。我尝试在代码中调用 PhotoView 而不是 ImageView,但结果是一样的。我试过在 ViewPager XML 中调用 uk.co.senab.photoview.PhotoView 而不是 android.support.v4.view.ViewPager,但同样没有变化,图像尺寸不随图像扩展。这是我的代码:寻呼机 Activity :

    import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.myapp.tnt.TNTConstants.Extra;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

/**
* @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
*/
public class UILPager extends BaseActivity {

private static final String STATE_POSITION = "STATE_POSITION";
DisplayImageOptions options;
ViewPager pager;
PhotoViewAttacher mAttacher;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.uil_viewpager);

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

if (savedInstanceState != null) {
pagerPosition = savedInstanceState.getInt(STATE_POSITION);
}

options = new DisplayImageOptions.Builder()
.showImageForEmptyUri(R.drawable.ic_empty)
.showImageOnFail(R.drawable.ic_error)
.resetViewBeforeLoading(true).cacheOnDisc(true)
.imageScaleType(ImageScaleType.NONE)
.bitmapConfig(Bitmap.Config.RGB_565)
.displayer(new FadeInBitmapDisplayer(300)).build();

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

}// --- END onCreate

@Override
public void onSaveInstanceState(Bundle outState) {
outState.putInt(STATE_POSITION, pager.getCurrentItem());
}

private class ImagePagerAdapter extends PagerAdapter {

private String[] images;
private LayoutInflater inflater;

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

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

@Override
public void finishUpdate(View container) {
}

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

@Override
public Object instantiateItem(ViewGroup view, int position) {
View imageLayout = inflater.inflate(R.layout.uil_viewpager_item,
view, false);
ImageView phView = (ImageView) imageLayout.findViewById (R.id.uil_vpager_image);
// Attach a PhotoViewAttacher, which takes care of all of the zooming functionality.
mAttacher = new PhotoViewAttacher(phView);
final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.uil_vpager_prog);
imageLoader.displayImage(images[position], phView, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
spinner.setVisibility(View.VISIBLE);
}

@Override
public void onLoadingFailed(String imageUri, View view,
FailReason failReason) {
String message = null;
switch (failReason.getType()) {
case IO_ERROR:
message = "Input/Output error";
break;
case DECODING_ERROR:
message = "Image can't be decoded";
break;
case NETWORK_DENIED:
message = "Downloads are denied";
break;
case OUT_OF_MEMORY:
message = "Out Of Memory error";
break;
case UNKNOWN:
message = "Unknown error";
break;
}
Toast.makeText(UILPager.this, message,
Toast.LENGTH_SHORT).show ();

spinner.setVisibility(View.GONE);
}

@Override
public void onLoadingComplete(String imageUri,
View view, Bitmap loadedImage) {
spinner.setVisibility(View.GONE);
}
});

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

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

@Override
public void restoreState(Parcelable state, ClassLoader loader) {
}

@Override
public Parcelable saveState() {
return null;
}

@Override
public void startUpdate(View container) {
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.splash, menu);
return true;
}

@Override
protected void onPause() {
super.onPause();
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
UILPager.this.finish();
}
}

下面是 Pager Items 的 XML:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="1dip" >

<ImageView
android:id="@+id/uil_vpager_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:contentDescription="@string/descr_image" />

<ProgressBar
android:id="@+id/uil_vpager_prog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone" />

</FrameLayout>

有没有人遇到过这个问题,如果有,解决办法是什么?我正在使用 SDK 10,并且在我的 Android 2.3.4 手机和我的 Android 4.3 平板电脑上得到了相同的结果

最佳答案

我想通了。我做错了几件事。最主要的是:为了使 PhotoView 能够扩展到原始图像的边界之外,您不能将它放入 ImageLayout 中,您必须创建一个PhotoView(不是 ImageView)在代码中,而不是在 XML 中。所以这是我更正的寻呼机 Activity :

    package com.myapp.wcbc;

import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.view.ViewGroup.LayoutParams;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.myapp.wcbc.TNTConstants.Extra;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

import com.myapp.wcbc.HackyViewPager;
import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

/**
* @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
*/
public class UILPager extends BaseActivity {

private static final String STATE_POSITION = "STATE_POSITION";
DisplayImageOptions options;
ViewPager pager;
PhotoViewAttacher mAttacher;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.uk_vp);

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

if (savedInstanceState != null) {
pagerPosition = savedInstanceState.getInt(STATE_POSITION);
}

options = new DisplayImageOptions.Builder()
.showImageForEmptyUri(R.drawable.ic_empty)
.showImageOnFail(R.drawable.ic_error)
.resetViewBeforeLoading(true).cacheOnDisc(true)
.imageScaleType(ImageScaleType.EXACTLY)
.bitmapConfig(Bitmap.Config.RGB_565)
.displayer(new FadeInBitmapDisplayer(300)).build();

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

}// --- END onCreate

@Override
public void onSaveInstanceState(Bundle outState) {
outState.putInt(STATE_POSITION, pager.getCurrentItem());
}

private class ImagePagerAdapter extends PagerAdapter {

private String[] images;

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

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

@Override
public void finishUpdate(View container) {
}

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

@Override
public Object instantiateItem(ViewGroup container, int position) {
PhotoView phView = new PhotoView(container.getContext());
// Attach a PhotoViewAttacher, which takes care of all of the zooming functionality.
mAttacher = new PhotoViewAttacher(phView);
final ProgressBar spinner = new ProgressBar(container.getContext());
imageLoader.displayImage(images[position], phView, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
spinner.setVisibility(View.VISIBLE);
}

@Override
public void onLoadingFailed(String imageUri, View view,
FailReason failReason) {
String message = null;
switch (failReason.getType()) {
case IO_ERROR:
message = "Input/Output error";
break;
case DECODING_ERROR:
message = "Image can't be decoded";
break;
case NETWORK_DENIED:
message = "Downloads are denied";
break;
case OUT_OF_MEMORY:
message = "Out Of Memory error";
break;
case UNKNOWN:
message = "Unknown error";
break;
}
Toast.makeText(getBaseContext(), message,
Toast.LENGTH_SHORT).show ();

spinner.setVisibility(View.GONE);
}

@Override
public void onLoadingComplete(String imageUri,
View view, Bitmap loadedImage) {
spinner.setVisibility(View.GONE);
}
});

container.addView(phView, LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
return phView;
}

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

@Override
public void restoreState(Parcelable state, ClassLoader loader) {
}

@Override
public Parcelable saveState() {
return null;
}

@Override
public void startUpdate(View container) {
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.splash, menu);
return true;
}

@Override
protected void onPause() {
super.onPause();
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
UILPager.this.finish();
}
}

不过我是对的。所有需要调整的魔法都在 instantiateItem() 中。

关于android - Photoview 图像边界不改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20007199/

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