加载到视图中时,从图像URL设置壁纸

时间:2013-09-27 08:20:16

标签: android url wallpaper

我已将我的应用设置为从外部源/链接加载大量图像/壁纸,然后将这些图像加载到视图中,以便用户可以滚动它们。

我想这样做,一旦用户点击他们在视图中的图像,它就会被设置为他们的壁纸。

这是我的班级:

public class Question extends FragmentActivity implements OnClickListener {
private static final String IMAGE_CACHE_DIR = "images";
public static final String EXTRA_IMAGE = "extra_image";

private ImagePagerAdapter mAdapter;
private ImageFetcher mImageFetcher;
private ViewPager mPager;

@TargetApi(11)
@Override
public void onCreate(Bundle savedInstanceState) {
    if (BuildConfig.DEBUG) {
        Utils.enableStrictMode();
    }
    super.onCreate(savedInstanceState);
    setContentView(R.layout.image_detail_pager);

    AdView ad = (AdView) findViewById(R.id.ad);
    ad.loadAd(new AdRequest());

    final DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    final int height = displayMetrics.heightPixels;
    final int width = displayMetrics.widthPixels;

    final int longest = (height > width ? height : width) / 2;

    ImageCache.ImageCacheParams cacheParams = new ImageCache.ImageCacheParams(
            this, IMAGE_CACHE_DIR);
    cacheParams.setMemCacheSizePercent(0.25f);

    mImageFetcher = new ImageFetcher(this, longest);
    mImageFetcher.addImageCache(getSupportFragmentManager(), cacheParams);
    mImageFetcher.setImageFadeIn(false);

    mAdapter = new ImagePagerAdapter(getSupportFragmentManager(),
            Images.imageUrlsQuestion.length);
    mPager = (ViewPager) findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);
    mPager.setPageMargin((int) getResources().getDimension(
            R.dimen.image_detail_pager_margin));
    mPager.setOffscreenPageLimit(2);

    getWindow().addFlags(LayoutParams.FLAG_FULLSCREEN);

    if (Utils.hasHoneycomb()) {
        final ActionBar actionBar = getActionBar();

        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayHomeAsUpEnabled(true);

        mPager.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(int vis) {
                if ((vis & View.SYSTEM_UI_FLAG_LOW_PROFILE) != 0) {
                    actionBar.hide();
                } else {
                    actionBar.show();
                }
            }
        });

        mPager.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
        actionBar.hide();
    }

    final int extraCurrentItem = getIntent().getIntExtra(EXTRA_IMAGE, -1);
    if (extraCurrentItem != -1) {
        mPager.setCurrentItem(extraCurrentItem);
    }
}

@Override
public void onResume() {
    super.onResume();
    mImageFetcher.setExitTasksEarly(false);
}

@Override
protected void onPause() {
    super.onPause();
    mImageFetcher.setExitTasksEarly(true);
    mImageFetcher.flushCache();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    mImageFetcher.closeCache();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        return true;
    case R.id.clear_cache:
        mImageFetcher.clearCache();
        Toast.makeText(this, R.string.clear_cache_complete_toast,
                Toast.LENGTH_SHORT).show();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu, menu);
    return true;
}

public ImageFetcher getImageFetcher() {
    return mImageFetcher;
}

private class ImagePagerAdapter extends FragmentStatePagerAdapter {
    private final int mSize;

    public ImagePagerAdapter(FragmentManager fm, int size) {
        super(fm);
        mSize = size;
    }

    @Override
    public int getCount() {
        return mSize;
    }

    @Override
    public Fragment getItem(int position) {
        return ImageDetailFragment
                .newInstance(Images.imageUrlsQuestion[position]);
    }
}

@TargetApi(11)
@Override
public void onClick(View v) {
    final int vis = mPager.getSystemUiVisibility();
    if ((vis & View.SYSTEM_UI_FLAG_LOW_PROFILE) != 0) {
        mPager.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
    } else {
        mPager.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
    }
}
}

1 个答案:

答案 0 :(得分:0)

使用WallpaperManager here查看设置壁纸 并且不要忘记将以下权限添加到您的清单中:

     <uses-permission android:name="android.permission.SET_WALLPAPER"/>