2016-04-06 3 views
0

Je développe une application de fond d'écran dans Android et je trouve une bonne façon de définir le fond d'écran déroulant pour mon application. Maintenant, mon code peut définir le fond d'écran à partir de bitmap, mais il a été recadré pour s'adapter à une page et est resté seulement sur une seule page (j'ai 5 pages dans l'écran d'accueil). Cela signifie que le contenu de chaque page peut faire défiler le fond d'écran, mais le fond d'écran ne défilait pas.android Fond d'écran défilable sur le téléphone de l'écran

Je souhaite définir un fond d'écran déroulant. J'ai essayé quelques codes d'internet mais ils n'ont pas aidé. Est-ce que vous avez une idée?

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(); 
       } 
      } 
     }); 

Et je l'utilise à partir de ce code, mais ne fonctionne pas:

//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)); 

Répondre

0

Post est vieux, mais quand même ... Vous pouvez essayer quelque chose de similaire à ce

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(); 
    } 
}