Android ImageLoader,如何在ViewPager中将图像设置为WallPaper

时间:2014-12-09 15:08:50

标签: java android view universal-image-loader wallpaper

您好我正在使用nostra13 / Android-Universal-Image-Loader库。

我想要做的是在ViewPager中加载图像时,它会将其设置为壁纸。

我遇到的问题。当我加载图像2.图像1设置为壁纸或有时是一个ramdom图像。

这是我的代码:

...

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "onCreate");

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.custom_fr_image_pager, container, false);
        ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager);
        Log.i(TAG, "onCreateView");



        imageUrls = null;


        pager.setAdapter(new ImageAdapter());
        pager.setCurrentItem(getArguments().getInt(Constants.Extra.IMAGE_POSITION, 0));


        return rootView;
    }

//------------------------------------------------------------------------------------------------------
// ImagePagerAdapter
//------------------------------------------------------------------------------------------------------
private class ImageAdapter extends PagerAdapter {

    private LayoutInflater inflater;

    ImageAdapter() {
        inflater = LayoutInflater.from(getActivity());
    }

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

    @Override
    public int getCount() {
        return imageUrls.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) imageLayout.findViewById(R.id.image);



        final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);

        ImageLoader.getInstance().displayImage(imageUrls[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) {

                vc = new VarController(AppApplication.getAppContext());

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

                Log.e(TAG, "onLoadingFailed error => " + message);
                Utils.createUserLogParseOb(getActivity(), TAG, "onLoadingFailed", message, "", vc.readCountryPref(), vc.readIMEIPref());
                Utils.makeToast(getActivity(), getString(R.string.sorry_there_was_an_error), false);


                spinner.setVisibility(View.GONE);
            }

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


                WallpaperManager wallManager = WallpaperManager.getInstance(AppApplication.getAppContext());
                try {
                    //wallManager.setBitmap(loadedImage);
                    wallManager.setBitmap(loadedImage);

                    Toast.makeText(getActivity(), "Wallpaper Set Successfully!!", Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    Toast.makeText(getActivity(), "Setting WallPaper Failed!!", Toast.LENGTH_SHORT).show();
                }



            }
        });

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

0 个答案:

没有答案