从URL设置图像作为壁纸 - Glide Library - [英] Set image From URL as wallpaper - Glide Library -

查看:598
本文介绍了从URL设置图像作为壁纸 - Glide Library - 的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i'm trying to create an wallpapers application using Glide library , in my app i have 4 categories and each category i have range of images displayed it by their URLs like this picture :

当我点击图片时出现此格式:

and when i click on a picture Appears in this format :

我有的问题设置为壁纸我尝试了一切,但对我不起作用,这是我的 file.java 负责获取图片并显示它。

i have problem with Set as wallpaper i tried everything but doesn't work for me , this is my file.java responsible for fetching a picture and showing it.

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>

请帮助我!

推荐答案

在代码中进行此更改。

llsetwallpapers.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      Glide.with(this)
    .load(path)
    .asBitmap()
    .into(new SimpleTarget<Bitmap>() {
        @Override
        public void onResourceReady(Bitmap resource, GlideAnimation<? super 
                  Bitmap> glideAnimation) {


 WallpaperManager.getInstance(getApplicationContext()).setBitmap(resource);

        }
    });

    });
 }

这篇关于从URL设置图像作为壁纸 - Glide Library - 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆