android 以编程方式在屏幕 phone 上滚动壁纸
android Scrollable wallpaper on screen's phone programmatically
我正在 Android 开发一个壁纸应用程序,我正在寻找一种正确的方法来为我的应用程序设置可滚动的壁纸。现在,我的代码可以从位图中设置墙纸,但它被裁剪以适合一页并且只停留在一页上(我在主屏幕上有 5 页)。这意味着每个页面中的内容都可以通过墙纸滚动,但墙纸不能滚动。
我想设置一个可滚动的墙纸。我尝试了一些来自互联网的代码,但它们没有帮助。你们有什么想法吗?
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();
}
}
});
我使用这段代码,但没有用:
//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 是旧的但无论如何......你可以尝试类似的东西
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 开发一个壁纸应用程序,我正在寻找一种正确的方法来为我的应用程序设置可滚动的壁纸。现在,我的代码可以从位图中设置墙纸,但它被裁剪以适合一页并且只停留在一页上(我在主屏幕上有 5 页)。这意味着每个页面中的内容都可以通过墙纸滚动,但墙纸不能滚动。
我想设置一个可滚动的墙纸。我尝试了一些来自互联网的代码,但它们没有帮助。你们有什么想法吗?
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();
}
}
});
我使用这段代码,但没有用:
//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 是旧的但无论如何......你可以尝试类似的东西
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();
}
}