将图片从URL设置为壁纸 - Glide Library -

时间:2017-05-04 08:56:12

标签: android imageview android-glide

我正在尝试使用 Glide库创建一个壁纸应用程序,在我的应用程序中我有4个类别,每个类别我有一系列图像显示它们的URL如下图所示:< / p>

enter image description here

当我点击图片时会以这种格式显示:

enter image description here

我遇到问题设置为壁纸我尝试了一切但不适合我,这是我的 file.java 负责提取图片和显示它

public class Pop extends Activity {
int width,height;
String url;
LinearLayout llDownloadWallpaper,llsetwallpapers;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pop);
    llDownloadWallpaper = (LinearLayout)findViewById(R.id.llDownloadWallpaper);
    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));
    llDownloadWallpaper.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // download image
        }
    });
    final ImageView userImageView = (ImageView) findViewById(R.id.imageSelectTo);
    llsetwallpapers.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // set as wallpapers
           /* try {
                WallpaperManager.getInstance(getApplicationContext()).setBitmap(Glide.with(getApplicationContext()).load(url).asBitmap().into(width,height).get());
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }*/
          }
        });
     }
  }

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>
</RelativeLayout>

2 个答案:

答案 0 :(得分:3)

在代码中进行此更改。

FUN

答案 1 :(得分:1)

我没有离开给你确切的答案,但我会给你一些想法

首先点击获取点击图片的网址,然后将该图片设置为背景图片,就像这样。

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        WallpaperManager wpm = WallpaperManager.getInstance(context);
        InputStream ins = null;
        try {
            ins = new URL("http://cdn.wonderfulengineering.com/wp-content/uploads/2014/07/inspiring-creative-design-mobile-phone-wallpapers.jpg").openStream();
            wpm.setStream(ins);
        } catch (IOException e) {
            e.printStackTrace();
        }

另外不要忘记添加清单文件的权限

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