0

我有一个使用 ViewFlipper 为我的应用程序构建的图像轮播,但存储在设备上的图像质量很差。我正在使用以下内容:

String fileLoc = mediaData.get("fileLoc");
ImageView imageItem = new ImageView(getApplicationContext());
InputStream imageStream = null;
try {
     File file = new File(fileLoc);
     imageStream = new FileInputStream(file);
     imageItem.setImageBitmap(Configurator.decodeSampledBitmapFromResource(getResources(), fileLoc, 100, 100));
} catch...

内存是一个问题,因为可能显示任意数量的图像。

我确实找到了这个:

资源到位图的转换导致质量很差

但我看不出如何将其转化为我的情况。

根据@Ultimo_m,以下是需要发生的事情的详细信息:

  1. 用户会看到一个屏幕,该屏幕具有三个部分,基于先前选择的选项:pdf 文档、图像和视频。
  2. 用户从显示的集合中选择一个图像
  3. 用户被带到显示所选项目的屏幕
  4. 从右向左滑动显示该集中的下一张图片
  5. 从左向右滑动可显示集合中的上一张图片

现在我正在使用 ViewFlipper:

  1. 我通过 SharedPreference 传递每个图像位置的数组
  2. 我也传了当前图片的关键位置
  3. 我遍历数组以构建 ViewFlipper
  4. 我使用当前键在脚蹼中设置当前视图

使用从@Ultimo_m 中选择的库,我如何告诉库要显示的当前图像以及下一个和上一个图像是什么?

- - - 编辑 - - -

从上一个屏幕点击时,我有通用图像加载器处理当前图像。我在文档中找不到如何处理滑动事件并告诉库接下来要显示什么。图书馆的 github 上有一个正在进行的滑动事件的屏幕截图,但我该如何利用它呢?

- - - 编辑 - - -

应用程序崩溃。在 .处抛出一个 NullPointer imageLoadView.setAdapter(new ImagePagerAdapter(imageArraySet));。我已经按照您的指定制作了 xml 文件。我确实对您的代码进行了调整,以从SharedPref我已经记录了我正在传递的数组(imageArraySet)中获取我的数组,并且数组是正确的。我在代码之后粘贴了完整的错误日志:

public class Viewer extends baseActivity {

    clientDB clientDB = new ClientDB(this);

    public static final String PREFS_NAME = "myPrefs";
    SharedPreferences storedInfo;

    String chosenImg;

    ViewPager imageLoadView;

    DisplayImageOptions options;

    String[] imageArraySet;

    //LoadImageUtil mLoadImageUtil;
    private static final String STATE_POSITION = "STATE_POSITION";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewer);

        Bundle bundle = getIntent().getExtras();
        assert bundle != null;

        storedInfo = getSharedPreferences(PREFS_NAME,0);
        String mediaID = storedInfo.getString("imgId", null); //ids of all passed images
        String chosenImg = storedInfo.getString("chosenImg", null); //id of current image

        String[] imageArray = mediaID.split(",");
        imageArraySet = clientDB.getMediaDataSet(imageArray);
        int pagerPosition = 0;

        imageLoadView = (ViewPager) findViewById(R.id.imageLoadView);
        imageLoadView.setAdapter(new ImagePagerAdapter(imageArraySet));
        imageLoadView.setCurrentItem(pagerPosition);


    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putInt(STATE_POSITION, imageLoadView.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) {
            container.removeView((View) object);
        }

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

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

            imageLoader.displayImage(images[position], 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(ImagePagerActivity.this, message, Toast.LENGTH_SHORT).show();

                    spinner.setVisibility(View.GONE);
                }

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

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



}

错误日志:

6-12 16:41:30.455: W/dalvikvm(19960): threadid=1: thread exiting with uncaught exception (group=0x41fdfe10)
06-12 16:41:30.455: E/AndroidRuntime(19960): FATAL EXCEPTION: main
06-12 16:41:30.455: E/AndroidRuntime(19960): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.client.clientconfigurator/com.client.clientconfigurator.Viewer}: java.lang.NullPointerException
06-12 16:41:30.455: E/AndroidRuntime(19960):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
06-12 16:41:30.455: E/AndroidRuntime(19960):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
06-12 16:41:30.455: E/AndroidRuntime(19960):    at android.app.ActivityThread.access$700(ActivityThread.java:150)
06-12 16:41:30.455: E/AndroidRuntime(19960):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
06-12 16:41:30.455: E/AndroidRuntime(19960):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-12 16:41:30.455: E/AndroidRuntime(19960):    at android.os.Looper.loop(Looper.java:176)
06-12 16:41:30.455: E/AndroidRuntime(19960):    at android.app.ActivityThread.main(ActivityThread.java:5279)
06-12 16:41:30.455: E/AndroidRuntime(19960):    at java.lang.reflect.Method.invokeNative(Native Method)
06-12 16:41:30.455: E/AndroidRuntime(19960):    at java.lang.reflect.Method.invoke(Method.java:511)
06-12 16:41:30.455: E/AndroidRuntime(19960):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
06-12 16:41:30.455: E/AndroidRuntime(19960):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
06-12 16:41:30.455: E/AndroidRuntime(19960):    at dalvik.system.NativeStart.main(Native Method)
06-12 16:41:30.455: E/AndroidRuntime(19960): Caused by: java.lang.NullPointerException
06-12 16:41:30.455: E/AndroidRuntime(19960):    at com.client.clientconfigurator.Viewer.onCreate(Viewer.java:92)
06-12 16:41:30.455: E/AndroidRuntime(19960):    at android.app.Activity.performCreate(Activity.java:5267)
4

1 回答 1

1

我建议你使用Universal Image Loader,它使用异步图像加载、缓存和显示,你可以在那里找到如何实现它的示例。完成后,您不必担心图像的数量。

在这里,我将给你一个如何使用它的例子:

首先创建这个 LoadImageUtil.class

public class LoadImageUtil {

    protected ImageLoader imageLoader = ImageLoader.getInstance();
    DisplayImageOptions options;
    public boolean memoryManage(int item) {// use this to clear cache 
        switch (item) {
            case 0:
                imageLoader.clearMemoryCache();
                return true;
            case 1:
                imageLoader.clearDiscCache();
                return true;
            default:
                return false;
        }
    }

    public LoadImageUtil(Context mContext) {
        initUILSettings(mContext);
    }

    public void initUILSettings(Context mContext){

        imageLoader.init(ImageLoaderConfiguration.createDefault(mContext));

        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.calice)
                .showImageForEmptyUri(R.drawable.calice)
                .showImageOnFail(R.drawable.calice)
                .cacheInMemory(true)
                .cacheOnDisc(true)
                .considerExifParams(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();

    }

    public void loadBitmapToImageView(ImageView myImageView, String ImagePath) {

        imageLoader.displayImage(ImagePath, myImageView, options, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                super.onLoadingComplete(imageUri, view, loadedImage);

            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
               // Log.e("onLoadingFailed : " + imageUri, "failReason: " + failReason);

            }

            @Override
            public void onLoadingCancelled(String imageUri, View view) {
                super.onLoadingCancelled(imageUri, view);                
            }
        });
    }
}

从您要加载图像的班级中的第二个执行以下操作:

LoadImageUtil mLoadImageUtil = new LoadImageUtil(getApplicationContext());

第三把它放在你想加载图像的地方:

mLoadImageUtil.loadBitmapToImageView(YourImageView, ImagePathOrUrl);

这里有你应该在加载图像的方法中传递的 imagesPath 示例

String imageUri = "http://8.8.8.8/image.png"; // from Web
String imageUri = "file:///mnt/sdcard/image.png"; // from SD card
String imageUri = "content://media/external/audio/albumart/13"; // from content provider
String imageUri = "assets://image.png"; // from assets
String imageUri = "drawable://" + R.drawable.image; // from drawables (only images, non-9patch)

编辑:

在我看来,最好使用 ViewPager 来解决您的问题,下面我将根据我告诉您的库向您展示一些如何做到这一点的代码:

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

    private static final String STATE_POSITION = "STATE_POSITION";


    ViewPager pager;
     LoadImageUtil mLoadImageUtil;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_image_pager);

             // in this example the data are passed to this activity using bundle
        Bundle bundle = getIntent().getExtras();
        assert bundle != null;

            // imageUrls is the array that contains the images URL to load
        String[] imageUrls = bundle.getStringArray(Extra.IMAGES);

        int pagerPosition = bundle.getInt(Extra.IMAGE_POSITION, 0);

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

             mLoadImageUtil = new LoadImageUtil(getApplicationContext());


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

    @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) {
            container.removeView((View) object);
        }
           // the view pager will have the same number of pages as the length of the Strings array
        @Override
        public int getCount() {
            return images.length;
        }

        @Override
        public Object instantiateItem(ViewGroup view, int position) {
            View imageLayout = inflater.inflate(R.layout.item_pager_image, view, false);
            assert imageLayout != null;
            ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
               // here is the part where image is load for each pager
               mLoadImageUtil.loadBitmapToImageView(imageView, images[position]);

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

以下是xml文件:

ac_image_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="fill_parent"
    android:layout_height="fill_parent" />

item_pager_image.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/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:adjustViewBounds="true"
        android:contentDescription="@string/descr_image" />

</FrameLayout>
于 2014-06-06T21:26:16.437 回答