android - setting wallpaper from resource -
i have simple problem wallpapermanager , cannot find answer on site. have simple code picture 1280x800 (the display of tablet). when run code, center of image wallpaper, if whole picture zoomed in. why that? thanks!
package com.daniel.wallpaperporsche; import java.io.ioexception; import com.daniel.wallpaper.r; import android.os.bundle; import android.app.activity; import android.app.wallpapermanager; import android.view.menu; import android.widget.toast; public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); wallpapermanager mywallpapermanager = wallpapermanager.getinstance(this); try { mywallpapermanager.setresource(r.drawable.porsche_911_1280x800_72dpi); toast.maketext(getbasecontext(), "success set wallpaper", toast.length_short).show(); } catch (ioexception e) { toast.maketext(getbasecontext(), "error set wallpaper", toast.length_short).show(); } super.ondestroy(); } }
the best way i've found set wallpaper without zooming issues (which seem arbitrary, you're seeing when resolution sizes same) bitmapfactory. you'll use wallpapermanager.setbitmap instead of wallpapermanager.setresource.
string path = "path/to/desired/wallpaper/file"; final bitmapfactory.options options = new bitmapfactory.options(); //the injustdecodebounds option tells decoder return null (no bitmap), //include size of image, letting query size. options.injustdecodebounds = true; bitmapfactory.decodefile(path_, options); // calculate insamplesize options.insamplesize = calculateinsamplesize(options, width, height); // decode bitmap insamplesize set options.injustdecodebounds = false; bitmap decodedsamplebitmap = bitmapfactory.decodefile(path_, options); //wm = wallpapermanager wm.suggestdesireddimensions(decodedsamplebitmap.getwidth(), decodedsamplebitmap.getheight()); wm.setbitmap(decodedsamplebitmap);
calculateinsamplesize method google provides implementation of in android documentation; goal load large bitmaps efficiently. can reading on here: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
unfortunately, implementation crap , wrong. i'd recommend this.
public static int calculateinsamplesize( bitmapfactory.options options, int reqwidth, int reqheight) { // raw height , width of image int imageheight = options.outheight; int imagewidth = options.outwidth; int insamplesize = 1; int multiplyheight = 1; int multiplywidth = 1; while (imageheight >= reqheight) { multiplyheight++; imageheight = imageheight/2; } while (imagewidth >= reqwidth) { multiplywidth++; imagewidth = imagewidth/2; } if (multiplyheight > multiplywidth) return multiplyheight; else return multiplywidth; }
this method more efficient loading large files, prevents outofmemoryexception, , should avoid strange zooming you're seeing. if not, let me know , i'll take second @ i've done.
Comments
Post a Comment