screenshot With Wallpaper - Android Phone

Android examples for Phone:Screen

Description

screenshot With Wallpaper

Demo Code

/*/*  ww  w . j  a  v a  2 s  .  c  o  m*/
 * Copyright (c) 2014 Zhang Hai <Dreaming.in.Code.ZH@Gmail.com>
 * All Rights Reserved.
 */
//package com.java2s;
import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.view.View;

public class Main {
    public static Bitmap screenshotWithWallpaper(Context context,
            View view, Rect clipRect) {

        Bitmap bitmap = Bitmap.createBitmap(clipRect.width(),
                clipRect.height(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);

        // Translate for clipRect.
        canvas.translate(-clipRect.left, -clipRect.top);

        Drawable wallpaper = WallpaperManager.getInstance(context)
                .getFastDrawable();
        // Center wallpaper on screen, as in launcher.
        DisplayMetrics displayMetrics = view.getResources()
                .getDisplayMetrics();
        wallpaper.setBounds(0, 0, displayMetrics.widthPixels,
                displayMetrics.heightPixels);
        // Translate the canvas to draw wallpaper on the correct location.
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        canvas.save();
        canvas.translate(-location[0], -location[1]);
        wallpaper.draw(canvas);
        canvas.restore();

        view.draw(canvas);

        return bitmap;
    }

    public static Bitmap screenshotWithWallpaper(Activity activity) {
        //View activityView = activity.findViewById(android.R.id.content).getRootView();
        View activityView = activity.getWindow().getDecorView();
        Rect frame = new Rect();
        activityView.getWindowVisibleDisplayFrame(frame);
        return screenshotWithWallpaper(activity, activityView, frame);
    }
}

Related Tutorials