使用 Universal Image Loader 在 vi​​ewpager 上启用缩放?

Enabling zoom on viewpager using Universal Image Loader?

我在 android.I 开发了一个图库应用程序,我正在从服务器获取图像并在 viewpager.I 上显示 已为此 purpose.My viewpager 作品使用通用图像加载器 fine.But 现在我想添加缩放功能,例如 zoomin/zoom out/pinch zoom。因此出于目的,我已将视图寻呼机的图像替换为 TouchImageView 但如果我使用 TouchImageView 那么如果进度条在隐藏进度条后显示图像加载那么图像不会 appear.I 不知道为什么如果我使用 TouchImageView 很开心。请告诉我出路?

imageview 寻呼机代码 activity

package info.androidhive.slidingmenu;

import info.androidhive.slidingmenu.R;
import info.androidhive.slidingmenu.adapter.ImageAdapter;
import info.androidhive.slidingmenu.adapter.ImagePagerAdapter;
import info.androidhive.slidingmenu.util.TouchImageView;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;

public class ImageViewPager extends Activity {
    // Declare Variable
    int position;
    Button delete, gimmy, done;
    ViewPager viewpager;
    ImageAdapter imageAdapter;
    ImagePagerAdapter pageradapter;
    TouchImageView imageView;
    TextView text;
    Runnable r;


    @SuppressLint("NewApi")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set title for the ViewPager
        setContentView(R.layout.view_pager);
        ActionBar bar = getActionBar();
        bar.setDisplayHomeAsUpEnabled(true);
        bar.setHomeButtonEnabled(true);
        setTitle("ViewPager");
        Intent p = getIntent();
        position = p.getExtras().getInt("id");
        pageradapter = new ImagePagerAdapter(ImageViewPager.this);
        viewpager = (ViewPager) findViewById(R.id.pager);
        viewpager.setAdapter(pageradapter);
        viewpager.setCurrentItem(position);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub

        switch (item.getItemId()) {
        case android.R.id.home:

            finish();
            overridePendingTransition(R.anim.slide_enter, R.anim.slide_exit);
            return true;

        default:
            return super.onOptionsItemSelected(item);

        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent p = new Intent(getApplicationContext(), SlideImages.class);
            // p.putExtra("id", pos+1);
            startActivity(p);
        }

        return super.onKeyDown(keyCode, event);
    }
}

view_pager.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000" />

viewpager 适配器代码

package info.androidhive.slidingmenu.adapter;

import info.androidhive.slidingmenu.app.AppController;
import info.androidhive.slidingmenu.bean.Wallpaper;
import info.androidhive.slidingmenu.util.TouchImageView;

import java.util.List;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

import info.androidhive.slidingmenu.R;
import uk.co.senab.photoview.PhotoViewAttacher;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.Toast;

public class ImagePagerAdapter extends PagerAdapter {
    LayoutInflater inflater;
    PhotoViewAttacher attacher;
    private DisplayImageOptions options;
    private  List<Wallpaper>  IMAGES_LIST=AppController.getInstance().getPrefManger().getAllImages();


        public ImagePagerAdapter(Context context) 
        {
                inflater = LayoutInflater.from(context);

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

    @Override
    public Object instantiateItem(ViewGroup container, int position)
    {
        View imageLayout = inflater.inflate(R.layout.item_pager_image, container, false);
        assert imageLayout != null;
        TouchImageView imageView = (TouchImageView) imageLayout.findViewById(R.id.image);
        final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);

        ImageLoader.getInstance().displayImage(IMAGES_LIST.get(position).getUrl(), imageView, 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(view.getContext(), message, Toast.LENGTH_SHORT).show();

                spinner.setVisibility(View.GONE);
            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                spinner.setVisibility(View.GONE);
                Log.i("RAE", "Loading Completed");


            }
        });

        container.addView(imageLayout, 0);
        return imageLayout;
    }

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

    @Override
    public int getCount() {
        return IMAGES_LIST.size();
    }


    @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;
    }
}

item_pager_image.xml

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

    <info.androidhive.slidingmenu.util.TouchImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:adjustViewBounds="true"
        android:contentDescription="@string/app_name" />

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

</FrameLayout>

Chris Banes 制作的 repository 中的任何可缩放图像都有一个很好的解决方案。它适用于任何可滚动容器,包括视图寻呼机。

试试这个

  • onLoadingStarted()中,添加view.setVisibility(View.GONE)
  • onLoadingComplete()中,添加view.setVisibility(View.VISIBLE)

这是上述解决方案的代码:

imageLoader.displayImage(imageUri.toString(), 
                     mPictureView,
                     options,
                     new SimpleImageLoadingListener()
{
@Override
public void onLoadingStarted(String imageUri, View view) 
{
    spinner.setVisibility(View.VISIBLE);
    view.setVisibility(View.GONE);
}

@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) 
{
    spinner.setVisibility(View.GONE);
}

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