android – 壁纸不适合设备屏幕

前端之家收集整理的这篇文章主要介绍了android – 壁纸不适合设备屏幕前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_301_1@我在一个可绘制的文件夹中有一组图像.我有一个按钮设置图像为设备屏幕上的壁纸.但是当我将这个图像设置为壁纸时,它可以缩放或裁剪.我想要的图像应该适合屏幕尺寸.我已经看到很多关于SO的链接,但是没有链接对我有用.这是我正在尝试的代码.

码-

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();
}

我还在清单中添加了以下行:

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

解决方法

你好这个工作与可绘制的图像我已经检查了..
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,true);
                WallpaperManager wallpaperManager = WallpaperManager.getInstance(MainActivity.this); 
                wallpaperManager.setWallpaperOffsetSteps(1,1);
                wallpaperManager.suggestDesiredDimensions(width,height);
                try {
                  wallpaperManager.setBitmap(bitmap);
                  } catch (IOException e) {
                  e.printStackTrace();
                }

还要在Manifest.xml中提到这些权限..

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

这是截图..

对于重置适合屏幕壁纸将图像路径存储在共享首选项中,并使用启动完成的接收器,然后在屏幕上重置相同的壁纸….

广播接收机是..

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,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>
原文链接:/android/312203.html

猜你在找的Android相关文章