简体   繁体   English

如何将整个图像作为壁纸贴在屏幕上

[英]how to fit the whole image on screen as wallpaper

I am developing an app which picks an image from the gallery and then sets that image as the wallpaper. 我正在开发一个应用程序,从图库中选择一个图像,然后将该图像设置为壁纸。 But here problem is that only part of image is set as wallpaper not the whole image, but I want to set the whole image as the wallpaper. 但这里的问题是只有部分图像被设置为壁纸而不是整个图像,但我想将整个图像设置为壁纸。 can you please tell me how that can be done ??? 你能告诉我怎么办?

Here is my code... 这是我的代码......

public class Scaleimage extends Activity {
    /** Called when the activity is first created. */

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String FileName;
        File file = new File("/sdcard/pictures");   
        File[] imageFiles = file.listFiles( );
        if(imageFiles.length > 0 ) {
            FileName = imageFiles[0].getName();
        final WallpaperManager wallpaperManager = WallpaperManager.getInstance(getBaseContext());   
        Bitmap myBitmap =  BitmapFactory.decodeFile("/sdcard/pictures" + "/" + FileName); 

        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        int height = displayMetrics.heightPixels;
        int width = displayMetrics.widthPixels << 1;
        myBitmap = Bitmap.createScaledBitmap(myBitmap,width, height, true);
        try {
            wallpaperManager.setBitmap( myBitmap);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
    }
}

Set wallpaper size to your image size: 将壁纸大小设置为您的图像大小:

WallpaperManager wm = (WallpaperManager) getSystemService(WALLPAPER_SERVICE);
wm.setBitmap(bitmap);
wm.suggestDesiredDimensions(w, h);

and remember to add permissions: 并记得添加权限:

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

Pass width height to wallpaper manager like this : 将宽度高度传递给壁纸管理器如下:

final WallpaperManager wallpaperManager = (WallpaperManager)getSystemService(
                Context.WALLPAPER_SERVICE);    

Bitmap myBitmap = Bitmap.createScaledBitmap(Const.cropped_bitmap, wallpaperManager.getDesiredMinimumWidth(), wallpaperManager.getDesiredMinimumHeight(), true);
wallpaperManager.suggestDesiredDimensions(wallpaperManager.getDesiredMinimumWidth(), wallpaperManager.getDesiredMinimumHeight());

try {

    wallpaperManager.setBitmap(myBitmap);
    Toast.makeText(CropImageActivity.this, CropImageActivity.this.getString(R.string.wallpaper_has_been_set), 0).show();
} catch (IOException e) {
    e.printStackTrace();
    Toast.makeText(CropImageActivity.this, "Wallpaper not set", 0).show();
}

Do not forget to add permission : 不要忘记添加权限:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM