我在一个似乎并不复杂的问题上花了几个小时,但尽管有关于这个主题的所有现有消息,我仍然找不到解决方案......
我正在使用viewpager,在适配器中我有一个按钮和一个imageview。我想通过使用myWallpaperManager.setBitmap(image)单击按钮时将imageview中的图片设置为墙纸。在此之前,我调整图片大小以适应屏幕尺寸。壁纸已更改,但图片保持缩放和裁剪。我想用整张图片设置壁纸。 我对getDefaultDisplay()的宽度和高度以及myWallpaperManager.getDesiredMinimumHeight / Width()的宽度和高度都有相同的值。
我尝试了很多不同的方法。我不知道还能做什么!非常感谢您的回答。
以下是代码:
Button ButWallpaper = (Button) viewLayout.findViewById(R.id.ButWallpaper);
ButWallpaper.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
Bitmap image;
Bitmap imagelarge = ((BitmapDrawable)imageView2.getDrawable()).getBitmap();
int Measuredwidth = 0;
int Measuredheight = 0;
Point size = new Point();
WindowManager w = _activity.getWindowManager();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
w.getDefaultDisplay().getSize(size);
Measuredwidth = size.x;
Measuredheight = size.y;
}else{
Display d = w.getDefaultDisplay();
Measuredwidth = d.getWidth();
Measuredheight = d.getHeight();
}
Log.i("TAG", String.valueOf(Measuredwidth));
Log.i("TAG", String.valueOf(Measuredheight));
image = Bitmap.createScaledBitmap(imagelarge, Measuredwidth, Measuredheight,true);
// TODO Auto-generated method stub
WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(_activity.getApplicationContext());
try {
myWallpaperManager.setBitmap(image);
myWallpaperManager.suggestDesiredDimensions(Measuredwidth, Measuredheight);
Log.i("TAG", String.valueOf(myWallpaperManager.getDesiredMinimumHeight()));
Log.i("TAG", String.valueOf(myWallpaperManager.getDesiredMinimumWidth()));
Toast.makeText(_activity.getApplicationContext(), "Wallpaper changed", toast.LENGTH_LONG).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}});
答案 0 :(得分:0)
看看这是否有帮助:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
Bitmap bitmap = Bitmap.createScaledBitmap(yourimagebitmap, width, height, true);
WallpaperManager.getInstance(MyActivity.this).setBitmap(bitmap);