我正在开发一个应用程序,其中有一个选项可以在每个屏幕中设置为壁纸。(每个屏幕都有不同的图像。)问题是当我按下设置为壁纸选项时,图像不适合屏幕尺寸。以下是我的代码片段:
wbutton.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(),
"Wallpaper set", Toast.LENGTH_LONG).show();
// TODO Auto-generated method stub
WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setResource(R.drawable.two);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
请建议修改代码,以便我可以解决问题。
答案 0 :(得分:0)
请尝试这种方式,希望这有助于您解决问题。
wbutton.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(),"Wallpaper set", Toast.LENGTH_LONG).show();
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setResource(R.drawable.two);
myWallpaperManager.suggestDesiredDimensions(w, h);
} catch (IOException e) {
e.printStackTrace();
}
}
});
请记住添加权限:
<uses-permission android:name="android.permission.SET_WALLPAPER_HINTS"/>
<uses-permission android:name="android.permission.SET_WALLPAPER"/>