问题描述
我正在用Android开发壁纸应用程序,并且找到了为我的应用程序设置可滚动壁纸的正确方法.现在,我的代码可以从位图设置墙纸,但将其裁剪为仅适合一页,而仅停留在一页上(我在主屏幕上有5页).这意味着每个页面中的内容都可以在墙纸中滚动,但墙纸无法滚动.
I am developing a wallpaper application in Android and i am finding a right way to set scrollable wallpaper for my app. Now, my code can set wallpaper from bitmap but it was cropped to fit with one page and just stayed only on one page (i have 5 pages in home screen). That means the content in each page can scroll through the wallpaper but the wallpaper was not scroll.
我想设置一个可滚动的墙纸.我尝试了一些来自Internet的代码,但它们没有帮助.你们有什么主意吗?
I want to set a scrollable wallpaper. I tried some codes from internet but they did not help. Do you guys have any idea?
setImage_Wallpaper.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
File file = imageLoader.getDiscCache().get(urldisplay);
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(mContext);
try {
myWallpaperManager.setBitmap(bitmap);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
我从这段代码中使用,但是不起作用:
And I use from this code but don't work :
//get screen height
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
screenHeight = size.y;
wallPaperBitmap= ... //your bitmap resource
//adjust the aspect ratio of the Image
//this is the main part
int width = wallPaperBitmap.getWidth();
width = (width * screenHeight) / wallPaperBitmap.getHeight();
//set the wallpaper
//this may not be the most efficent way but it worked for me
wallpaperManager.setBitmap(Bitmap.createScaledBitmap(wallPaperBitmap, width, height, true));
推荐答案
帖子很旧,但是无论如何...您可以尝试与此类似的东西
Post is old but anyway... You can try something similar to this
public static void setWallpaper(Context context) {
int wallpaperRId = getWallpaperImageRid(context);
if (wallpaperRId == 0) {
return;
}
Bitmap tempBmp = BitmapFactory.decodeResource(context.getResources(), wallpaperRId);
// get size
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
int area = width * height / 1000;
width *= 2;
float scale = width / (float) tempBmp.getWidth();
height = (int) (scale * tempBmp.getHeight());
Bitmap bitmap = Bitmap.createScaledBitmap(tempBmp,width,height, true);
WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
wallpaperManager.setWallpaperOffsetSteps(1, 1);
wallpaperManager.suggestDesiredDimensions(width, height);
try {
wallpaperManager.setBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
这篇关于以编程方式在屏幕手机上的android Scrollable壁纸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!