如何将图片URL设置为壁纸(Glide Library)

时间:2017-05-03 21:19:34

标签: android imageview android-glide

请任何帮助,我正在尝试将图像设置为壁纸我通过她的网址获取此图像 我正在使用 Glide Library 我尝试使用此代码Set image as wallpaper from url但是对我来说不起作用请求解决此问题的任何帮助?

file.java:

public class Pop extends Activity {
int width,height;
String url;
LinearLayout llsetwallpapers;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pop);
    llsetwallpapers = (LinearLayout)findViewById(R.id.llSetWallpaper);
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    this.width = dm.widthPixels;
    this.height = dm.heightPixels;
    getWindow().setLayout((int) (((double) this.width) * 0.9d), (int) (((double) this.height) * 0.75d));
    getIntent().getSerializableExtra("WallpaperURL");
    this.url = (String)getIntent().getSerializableExtra("WallpaperURL");
 Glide.with(getApplicationContext()).load(this.url).into((ImageView)findViewById(R.id.imageSelectTo));
    llsetwallpapers.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // set as wallpapers

        }
    });
}

}

xml:

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/linealL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#65000000">

<ImageView
    android:id="@+id/imageSelectTo"
    android:layout_alignParentStart="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentTop="true"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true" />
<!-- Set as wallpaper button -->
<LinearLayout
    android:id="@+id/llSetWallpaper"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="10dp"
    android:layout_marginStart="10dp"
    android:background="@drawable/btn_rounded_corner"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:layout_marginLeft="10dp"
    android:layout_alignParentLeft="true">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:text="@string/SetAsWallpaper"
        android:textColor="@color/white"
        android:textSize="18sp" />
</LinearLayout>

<!-- Download wallpaper button -->

<LinearLayout
    android:id="@+id/llDownloadWallpaper"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_marginBottom="10dp"
    android:layout_marginEnd="10dp"
    android:background="@drawable/btn_rounded_corner"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:layout_alignParentRight="true"
    android:layout_marginRight="10dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:text="@string/Download"
        android:textColor="@color/white"
        android:textSize="18sp" />
</LinearLayout>

2 个答案:

答案 0 :(得分:0)

private ImageView userImageView;

userImageView = (ImageView) findViewById(R.id.user_image);

Glide.with(getApplicationContext()).load(yourImageUrl).asBitmap()
                    .error(yourDefaultImageUrl or from R.id.drawableDefaultImage).centerCrop().into(userImageView);

答案 1 :(得分:0)

尝试使用此代码,将网址转换为位图,然后根据屏幕大小设置壁纸:

声明变量

ImageView mImageView;
WallpaperManager wallpaperManager;
Bitmap bitmap1, bitmap2;
DisplayMetrics displayMetrics; //to find screen dimensions
int width, height; //to find screen dimensions
BitmapDrawable bitmapDrawable;
Button downloadButton;
String url="your wallpaper url";

初始化您的观点

mImageView = (ImageView) findViewById(R.id.imageView);
downloadButton= (Button) findViewById(R.id.Button);

在onCreate

Glide.with(this).load(spacePhoto.getUrl()).asBitmap().listener(new RequestListener<String, Bitmap>() {
        @Override
        public boolean onException(Exception e, String model, Target<Bitmap> target, boolean isFirstResource) {
            return false;
        }

        @Override
        public boolean onResourceReady(Bitmap resource, String model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {
            mImageView.setImageBitmap(resource);
            //Code to set bitmap as wallpaper according to screen size
            fabOptions.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            wallpaperManager  = WallpaperManager.getInstance(getApplication());
                            bitmapDrawable = (BitmapDrawable) mImageView.getDrawable();
                            bitmap1 = bitmapDrawable.getBitmap();
                            GetScreenWidthHeight();
                            bitmap2 = Bitmap.createScaledBitmap(bitmap1, width, height, false);
                            wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
                            try {
                                wallpaperManager.setBitmap(bitmap2);
                                wallpaperManager.suggestDesiredDimensions(width, height);

                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }).start();
                    Snackbar snackbar = Snackbar
                            .make(v, "Wallpaper Set", Snackbar.LENGTH_SHORT);
                    snackbar.show();

                }
            });
            return false;
        }
    }).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(mImageView);

将此代码写在onCreate

之外
public void GetScreenWidthHeight() {
    displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    width = displayMetrics.widthPixels;
    height = displayMetrics.heightPixels;
}