2014-03-31 63 views
9

我在一個可繪製文件夾中有一組圖像。我有一個按鈕在設備屏幕上將圖像設置爲壁紙。但是,當我將這個圖像設置爲牆紙時,無論是放大還是裁剪。我希望圖像應該適合屏幕尺寸。我已經看到了很多鏈接,但沒有鏈接對我有用。這是我到目前爲止所嘗試的代碼。牆紙在設備屏幕上不適合

代碼 -

Bitmap bmap2 = BitmapFactory.decodeStream(getResources().openRawResource(mThumb[position])); 
DisplayMetrics metrics = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(metrics); 
int height = metrics.heightPixels; 
int width = metrics.widthPixels; 
Bitmap bitmap = Bitmap.createScaledBitmap(bmap2, width, height, true); 
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this); 
try { 
    wallpaperManager.setBitmap(bitmap); 
    } catch (IOException e) { 
    e.printStackTrace(); 
} 

我還添加以下在manifest-

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

我也面臨着放大壁紙的同樣的問題。你找到這個解決方案,請幫助我解決這個[問題](http://stackoverflow.com/questions/32201161/wallpaper-not-properly-fit-on-samsung-devices).... –

回答

23

你好這是工作與繪製的圖像我檢查了它..

   DisplayMetrics metrics = new DisplayMetrics(); 
       getWindowManager().getDefaultDisplay().getMetrics(metrics); 
       int height = metrics.heightPixels; 
       int width = metrics.widthPixels; 
       Bitmap tempbitMap = BitmapFactory.decodeResource(getResources(), R.drawable.img); 
       Bitmap bitmap = Bitmap.createScaledBitmap(tempbitMap,width,height, true); 
       WallpaperManager wallpaperManager = WallpaperManager.getInstance(MainActivity.this); 
       wallpaperManager.setWallpaperOffsetSteps(1, 1); 
       wallpaperManager.suggestDesiredDimensions(width, height); 
       try { 
        wallpaperManager.setBitmap(bitmap); 
        } catch (IOException e) { 
        e.printStackTrace(); 
       } 

還別說清單中的這些權限。 xml ..

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

這是截圖..

enter image description here

對於屏幕商店的復位擬合壁紙圖像路徑中共享的喜好和使用結束啓動接收器,然後復位在屏幕上相同的壁紙....

的廣播接收機是..

import java.io.IOException; 

import android.app.WallpaperManager; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.util.DisplayMetrics; 
import android.util.Log; 
import android.view.WindowManager; 

public class BootReceiver extends BroadcastReceiver { 
private static final String TAG="BootReceiver"; 

@Override public void onReceive(Context context,Intent intent){ 
    try{ 
      DisplayMetrics metrics = new DisplayMetrics(); 
      WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
      windowManager.getDefaultDisplay().getMetrics(metrics); 
      int height = metrics.heightPixels; 
      int width = metrics.widthPixels; 
      Bitmap tempbitMap = BitmapFactory.decodeResource(context.getResources(), R.drawable.img); 
      Bitmap bitmap = Bitmap.createScaledBitmap(tempbitMap,width,height, true); 
      WallpaperManager wallpaperManager = WallpaperManager.getInstance(context); 
      wallpaperManager.setWallpaperOffsetSteps(1, 1); 
      wallpaperManager.suggestDesiredDimensions(width, height); 
      try { 
       wallpaperManager.setBitmap(bitmap); 
       } catch (IOException e) { 
       e.printStackTrace(); 
      } 
    }catch(Exception e){ 
     Log.e(TAG,e.toString()); 
    } 
} 
} 

添加在的Manifest.xml

 <receiver 
      android:name=".BootReceiver" 
      android:enabled="true" 
      android:exported="true" 
      android:label="BootReceiver" > 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" > 
       </action> 
      </intent-filter> 
     </receiver> 
+0

嗨@John R如果你對我的代碼感到滿意,那麼接受我的答案..其他方面請讓我知道... – PankajSharma

+0

你這樣做了,所以late.bounties已過期.. :(我鬆了100聲望s @ John R – PankajSharma

+0

我在你的代碼中發現了一個難題,如果我重新啓動我的設備或者在玩遊戲之後回到主屏幕大小的壁紙增加,那麼如何阻止這個? –

0

嘗試此行代碼:

public void changeWallpaper(String path) { 
     FileInputStream is; 
     BufferedInputStream bis; 
     WallpaperManager wallpaperManager; 
     Drawable wallpaperDrawable; 
     File sdcard = Environment.getExternalStorageDirectory(); 
     try { 
      is = new FileInputStream(new File(path)); 
      bis = new BufferedInputStream(is); 
      Bitmap bitmap = BitmapFactory.decodeStream(bis); 
      Bitmap useThisBitmap = Bitmap.createBitmap(bitmap); 
      wallpaperManager = WallpaperManager.getInstance(getActivity()); 
      wallpaperDrawable = wallpaperManager.getDrawable(); 
      wallpaperManager.setBitmap(useThisBitmap); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

在本例中,我從device SD使用的圖像卡...

這是完全爲我工作..

+0

我有圖像在drawable中如何做到這一點? –

3

試試這個代碼

Bitmap bmap2 =BitmapFactory.decodeStream(getResources().openRawResource(mThumb[position])); 
    DisplayMetrics metrics = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(metrics); 
    int height = metrics.heightPixels; 
    int width = metrics.widthPixels; 
    Bitmap bitmap = Bitmap.createScaledBitmap(bmap2, width, height, true); 
    WallpaperManager wallpaperManager = WallpaperManager.getInstance(this); 
    wallpaperManager.setWallpaperOffsetSteps(1, 1); 
    wallpaperManager.suggestDesiredDimensions(width, height); 
    try { 
     wallpaperManager.setBitmap(bitmap); 
     } catch (IOException e) { 
     e.printStackTrace(); 
    } 
+0

我有這個代碼的一個問題。 –

+0

您面臨什麼問題 – RQube

+0

重新啓動設備時或在我回到主屏幕時玩遊戲後。圖像變焦。 –

1
這些行後

如果你有圖片的URL,然後使用:

WallpaperManager wpm = WallpaperManager.getInstance(context); 
InputStream ins = new URL("absolute/path/of/image").openStream(); 
wpm.setStream(ins); 

如果你有像然後使用URI

WallpaperManager wpm = WallpaperManager.getInstance(context); 
wpm.setResource(Uri.of.image); 

在您的清單文件:

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

,不要忘記將這個代碼在AsyncTask

0

注意:也許不適合你。

你需要這個:

WallpaperManager wallpaperManager = WallpaperManager.getInstance(this); 
      try { 
       wallpaperManager.setBitmap(bitmap); 

       wallpaperManager.suggestDesiredDimensions(width, height); 
       Toast.makeText(this, "Wallpaper Set", Toast.LENGTH_SHORT).show(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

enter link description here