如何“将当前的毕加索加载的图像设置为墙纸”

时间:2019-05-18 14:07:49

标签: android

我正在研究毕加索并将图像加载到ImageView中。我应该在按钮代码上写些什么才能将其设置为Android壁纸?

我成功地从可绘制文件夹设置了wallaper。

public class ViewPagerAdapter extends PagerAdapter {
    private Context context;
    private String[] imageUrls;

    ViewPagerAdapter(Context context, String[] imageUrls) {
        this.context = context;
        this.imageUrls = imageUrls;
    }

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

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        ImageView imageView = new ImageView(context);
        Picasso.get()
                .load(imageUrls[position])
                .fit()
                .centerCrop()
                .into(imageView);
        container.addView(imageView);

        return imageView;
    }

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

1 个答案:

答案 0 :(得分:1)

首先,您要像这样从 Picasso 获得位图图像:

Bitmap mybitmap ;

Picasso.get().load(imageUrl).into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
     mybitmap = bitmap;
}

@Override
public void onBitmapFailed(Drawable errorDrawable) { }

@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {}
});

或者您可以像这样从Imageview获取位图:

imageView.buildDrawingCache();
Bitmap mybitmap= imageView.getDrawingCache();

,然后您可以使用WallpaperManager类设置墙纸。例如:

WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
wallpaperManager.setBitmap(mybitmap);