簡體   English   中英

Android-設置壁紙我的全圖

[英]Android- Set wallpaper my full image

如何設置圖像牆紙全圖背景屏幕。 我的密碼

Bitmap bmap = BitmapFactory.decodeResource(getResources(),
                    R.drawable.splash);

            DisplayMetrics outMetrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
            int w = outMetrics.widthPixels;
            int h = outMetrics.heightPixels;
            Bitmap wallpaper=Bitmap.createScaledBitmap(bmap, w, h, true);

                            WallpaperManager m = WallpaperManager.getInstance(getApplicationContext());
            try {
                m.setBitmap(wallpaper);
            } catch (IOException e) {
                e.printStackTrace();
            }

圖片

圖片

我需要圖像全套牆紙

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(MainActivity.this);
                        wallpaperDrawable = wallpaperManager.getDrawable();
                        wallpaperManager.setBitmap(useThisBitmap);
                } catch (Exception e) {
                        e.printStackTrace();
                }
    }

上面的代碼對我有用,並且不要忘記添加

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

在AndroidManifest.xml文件中

您還可以將可繪制對象轉換為位圖

BitmapFactory.decodeResource(context.getResources(), R.drawable.icon_name);

您可以通過針對結果的開始活動啟動“裁剪意圖”,然后在結果中檢索它,然后使用牆紙管理器類。 像這樣 :

Uri imgUri=Uri.parse("android.resource://your.package.name/"+R.drawable.image); 
Intent intent = new Intent("com.android.camera.action.CROP");  
intent.setDataAndType(imgUri, "image/*");  
intent.putExtra("crop", "true");  
intent.putExtra("aspectX", 1);  
intent.putExtra("aspectY", 1);  
intent.putExtra("outputX", 80);  
intent.putExtra("outputY", 80);  
intent.putExtra("return-data", true);
startActivityForResult(intent, REQUEST_CODE_CROP_PHOTO);

並在onResult函數中使用牆紙管理器

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM