[英]android Scrollable wallpaper on screen's phone programmatically
我正在使用Android開發壁紙應用程序,並且找到了為我的應用程序設置可滾動壁紙的正確方法。 現在,我的代碼可以從位圖設置牆紙,但將其裁剪為僅適合一頁,而僅停留在一頁上(我在主屏幕上有5頁)。 這意味着每個頁面中的內容都可以滾動顯示牆紙,但是牆紙不能滾動。
我想設置一個可滾動的牆紙。 我嘗試了一些來自Internet的代碼,但它們沒有幫助。 你們有什么主意嗎?
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));
帖子很舊,但是無論如何...您可以嘗試類似的操作
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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.