我正在使用 viewpager 并从 url 加载图像。我想实现一个按钮来将 viewpager 图像设置为手机壁纸。请帮助我,因为我是菜鸟。
public class ImageDetails extends Activity {
private ViewPager pager;
private DisplayImageOptions options;
protected ImageLoader imageLoader = ImageLoader.getInstance();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ac_image_pager);
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
Button buttonSetWallpaper = (Button)findViewById(R.id.downloads);
ImageView imagePreview = (ImageView)findViewById(R.id.image);
imagePreview.setImageResource();
buttonSetWallpaper.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setResource(R.drawable.wallpaper);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
//getting serialized data from MainActivity
Bundle bundle = getIntent().getExtras();
final List<String> imageUrls = bundle.getStringArrayList(ImageExtractor.IMAGES);
final List<String> downloads = bundle.getStringArrayList(ImageExtractor.DOWNLOADS);
final List<String> favs = bundle.getStringArrayList(ImageExtractor.FAV);
int pagerPosition = bundle.getInt(ImageExtractor.IMAGE_POSITION, 0);
//Setting DisplayImageOptions for UIL
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.stub_image) //setting image to show on loading
.showImageForEmptyUri(R.drawable.image_for_empty_url)
.showImageOnFail(R.drawable.image_for_empty_url)
.cacheInMemory(true)
.cacheOnDisk(true)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
//initializing viewpager
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(new ImagePagerAdapter(imageUrls,downloads,favs));
pager.setCurrentItem(pagerPosition);
}
@Override
protected void onStop() {
super.onStop();
}
//Adapter for Pager
private class ImagePagerAdapter extends PagerAdapter {
private List<String> images;
private LayoutInflater inflater;
private List<String> downloads;
private List<String> favs;
ImagePagerAdapter(List<String> images,List<String> d,List<String> f) {
this.images = images;
this.downloads=d;
this.favs=f;
inflater = LayoutInflater.from(ImageDetails.this);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public int getCount() {
return images.size();
}
@Override
public Object instantiateItem(ViewGroup view, final int position) {
final View imageLayout = inflater.inflate(R.layout.item_pager_image, view, false);
final TouchImageView imageView = (TouchImageView) imageLayout.findViewById(R.id.image);
final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);
TextView down = (TextView) imageLayout.findViewById(R.id.downloads);
TextView fav = (TextView) imageLayout.findViewById(R.id.Fav);
down.setText(downloads.get(position) + " downloads");
fav.setText(favs.get(position) + " liked");
//displaying image using UIL
imageLoader.displayImage(images.get(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(ImageDetails.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;
}
}
}