2017-05-03 3 views
-1

bitte jede Hilfe zu setzen, ich versuche Bild als Hintergrund einstellen bekomme ich dieses Bild durch ihre url ich mit Arbeit bin Glide Bibliothek ich diesen Code versucht Set image as wallpaper from url aber funktioniert bei mir bitte keine Hilfe um dieses Problem zu lösen?wie Bild-URL als Hintergrundbild (Glide Library)

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> 

Antwort

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); 
+0

danke für die Antwort, aber dieser Code funktioniert nicht für mich, ich stelle dies 'Glide.with (getApplicationContext()) .Load (URL) .asBitmap() .Fehler (R.Drawable.arrw). CenterCrop() .into ((ImageView) findViewById (R.id.imageSelectTo)); 'innerhalb von Onclick und wenn ich als Hintergrundbild-Taste drücke Nothing happens – user7959932

+0

Bist du sicher, dass du das Bild auf dem wallpaper imageView aufblähst und nicht irgendeine andere Ansicht, sondern sofort bindst und das ImageView mit einem Image aufzublasen, ist keine gute Übung. Ich denke, Sie sollten eine separate Methode zum Anhängen von Widgets haben. Bitte zeigen Sie auch Ihren XML-Code in Frage. – Athelon

+0

Stellen Sie außerdem sicher, dass Sie das Bild über die URL empfangen. – Athelon

0

Versuchen dieser Code eine URL nehmen es in Bitmap konvertiert und dann eingestellt Hintergrundbild nach Bildschirmgröße e:

Variablen deklarieren

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"; 

Initialisiere Ihre Ansichten

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

In 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); 

schreiben diesen Code außerhalb von onCreate

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