天天看點

截屏/截取螢幕指定區域

/**
 * 截屏工具.
 */
public class CutScreenUtile {

    /**
     * 截取全屏
     * @param aty
     * @return
     */
    public static Bitmap cutScreen(Activity aty){
        return cutScreen(aty,0,0,0,0);
    }

    /**
     * 指定位置截取螢幕
     * @param aty
     * @param x
     * @param y
     * @param width
     * @param height
     * @return
     */
    public static Bitmap cutScreen(Activity aty,int x,int y,int width,int height){
        View view = aty.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap bitmap = null;
        if(width != 0 && height != 0){
            bitmap = Bitmap.createBitmap(view.getDrawingCache(),x,y,width,height);
        }else {
            bitmap = view.getDrawingCache();
        }
//        view.setDrawingCacheEnabled(false);
        return bitmap;
    }
}