Android截屏分享

来源:互联网 发布:mac需要清理软件 编辑:程序博客网 时间:2024/06/05 03:40

最近公司有个新需求,在某页面需要截图分享.分享页面待下载App的二维码,上图





直接上代码:

这是生成截图的方法

public Bitmap getScreen() {    View dView = getWindow().getDecorView();    // 允许当前窗口保存缓存信息    dView.destroyDrawingCache();//清除缓存,重新生成    dView.setDrawingCacheEnabled(true);    dView.buildDrawingCache();    Bitmap bmp = dView.getDrawingCache();    if (bmp != null) {        try {            Rect rect = new Rect();            dView.getWindowVisibleDisplayFrame(rect);            int statusBarHeights = rect.top; // 获取状态栏高度            int titleHeight = UIUtils.dip2px(42) + statusBarHeights;//状态栏和title的高度.拼接时需要去除(42是我的title高度,需要转成px)            Display display = getWindowManager().getDefaultDisplay();            int widths = display.getWidth();            int heights = display.getHeight();            // 去掉状态栏            saveBitmap = Bitmap.createBitmap(dView.getDrawingCache(), 0, titleHeight, widths, heights - titleHeight);                       Bitmap alterBitmap = Bitmap.createBitmap(saveBitmap.getWidth(),                    saveBitmap.getHeight(), Bitmap.Config.ARGB_8888);            Canvas canvas = new Canvas(alterBitmap);            Paint paint = new Paint();            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DARKEN));            canvas.drawBitmap(saveBitmap, new Matrix(), paint);            Bitmap shareTitle = BitmapFactory.decodeResource(getResources(),                    R.mipmap.share_title_image);//将本地需要合成的图片转成bitmap            return mergeBitmap(shareTitle, saveBitmap, false);        } catch (Exception e) {            Logger.e(e.toString());            e.printStackTrace();        }    } else {        Logger.e("bmp为空");    }    return null;}


其中
mergeBitmap()方法来自百度
/** * 把两个位图覆盖合成为一个位图,上下拼接 * * @param topBitmap * @param bottomBitmap * @param isBaseMax    是否以高度大的位图为准,true则小图等比拉伸,false则大图等比压缩 * @return */public static Bitmap mergeBitmap(Bitmap topBitmap, Bitmap bottomBitmap, boolean isBaseMax) {    if (topBitmap == null || topBitmap.isRecycled()            || bottomBitmap == null || bottomBitmap.isRecycled()) {        Logger.e("topBitmap=" + topBitmap + "bottomBitmap=" + bottomBitmap);        return null;    }    int width = 0;    if (isBaseMax) {        width = topBitmap.getWidth() > bottomBitmap.getWidth() ? topBitmap.getWidth() : bottomBitmap.getWidth();    } else {        width = topBitmap.getWidth() < bottomBitmap.getWidth() ? topBitmap.getWidth() : bottomBitmap.getWidth();    }    Bitmap tempBitmapT = topBitmap;    Bitmap tempBitmapB = bottomBitmap;    if (topBitmap.getWidth() != width) {        tempBitmapT = Bitmap.createScaledBitmap(topBitmap, width, (int) (topBitmap.getHeight() * 1f / topBitmap.getWidth() * width), false);    } else if (bottomBitmap.getWidth() != width) {        tempBitmapB = Bitmap.createScaledBitmap(bottomBitmap, width, (int) (bottomBitmap.getHeight() * 1f / bottomBitmap.getWidth() * width), false);    }    int height = tempBitmapT.getHeight() + tempBitmapB.getHeight();    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);    Canvas canvas = new Canvas(bitmap);    Rect topRect = new Rect(0, 0, tempBitmapT.getWidth(), tempBitmapT.getHeight());    Rect bottomRect = new Rect(0, 0, tempBitmapB.getWidth(), tempBitmapB.getHeight());    Rect bottomRectT = new Rect(0, tempBitmapT.getHeight(), width, height);    canvas.drawBitmap(tempBitmapT, topRect, topRect, null);    canvas.drawBitmap(tempBitmapB, bottomRect, bottomRectT, null);    return bitmap;}


至此,只需要把返回的bitmap分享出去即可.还有一种需求是:需要把状态栏和title清空,替换成用户信息或者别的,

此时只需要将getScreen()方法替换为

public Bitmap getAllScreen() {      View dView = getWindow().getDecorView();      // 允许当前窗口保存缓存信息      dView.destroyDrawingCache();//清除缓存,重新生成      dView.setDrawingCacheEnabled(true);      dView.buildDrawingCache();      Bitmap bmp = dView.getDrawingCache();      if (bmp != null) {          try {              // 获取状态栏高度              Rect rect = new Rect();              dView.getWindowVisibleDisplayFrame(rect);              int statusBarHeights = rect.top;              Display display = getWindowManager().getDefaultDisplay();              int widths = display.getWidth();              int heights = display.getHeight();              // 去掉状态栏              saveBitmap = Bitmap.createBitmap(dView.getDrawingCache(), 0, 0,                      widths, heights);              Bitmap alterBitmap = Bitmap.createBitmap(saveBitmap.getWidth(),                      saveBitmap.getHeight(), Bitmap.Config.ARGB_8888);              Canvas canvas = new Canvas(alterBitmap);              Paint paint = new Paint();              Paint paintText = new Paint();              Paint paintLine = new Paint();//画线              float titleHeight = UIUtils.dip2px(42) + statusBarHeights;//状态栏和title的高度              paintLine.setColor(getResources().getColor(R.color.yellow));              paintLine.setStyle(Paint.Style.STROKE);              paintLine.setStrokeWidth(titleHeight);//用一条画线,画在状态栏和title上,清除上面原来的文字以及图片              paintText.setTextSize(UIUtils.dip2px(15));              paintText.setColor(Color.BLACK);              paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DARKEN));              canvas.drawBitmap(saveBitmap, new Matrix(), paint);//首先将整张图画在画布上              canvas.drawLine(0, titleHeight / 2, widths, titleHeight / 2, paintLine);//清除状态栏和title              canvas.drawText("哎呦,不错哦", UIUtils.dip2px(150), statusBarHeights + UIUtils.dip2px(42) / 2, paintText);//写上用户信息,              Bitmap icon = BitmapFactory.decodeResource(getResources(),                      R.mipmap.logo);              canvas.drawBitmap(icon, UIUtils.dip2px(10), statusBarHeights / 2, paint);//带上logo              return alterBitmap;          } catch (Exception e) {              Logger.e(e.toString());              e.printStackTrace();          }      } else {          Logger.e("bmp为空");      }      return null;  }


 
原创粉丝点击