截取屏幕一部分view+二维码(图片)+文字(文字竖排显示,自动换行),合成分享图片(Bitmap),调用系统分享将bitmap图片分享

效果图

根据自己想要的效果在画布上绘制即可。
效果图
1.先将要截取的屏幕部分shareListview(RelativeLayout)生成位图加载在画布上,画布宽高是截取部分的宽高

//截取诗词图片
        int width = shareListview.getWidth();
        int height = shareListview.getHeight();
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        shareListview.draw(canvas);

2.添加二维码,由于二维码图片较大,不能直接使用,bitmap不能直接设置大小,需要按比例缩小,将其放在画布右下角,left坐标即为整体的宽-二维码缩放后的宽,top为整体的高-二维码缩放后的高

 //添加二维码
        Bitmap share = BitmapFactory.decodeResource(this.getResources(), R.drawable.share_ma);
        //重新设置bitmap大小
        Bitmap newShare = newBitmap(share,height/4,height/4);
        canvas.drawBitmap(newShare, width-newShare.getWidth(), height-newShare.getHeight(), null);
    public static Bitmap newBitmap(Bitmap bitmap, int newWidth, int newHeight) {
        Bitmap newbm = null;
        if (bitmap!=null){
            // 获得图片的宽高
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            // 计算缩放比例
            float scaleWidth = ((float) newWidth) / width;
            float scaleHeight = ((float) newHeight) / height;
            // 取得想要缩放的matrix参数
            Matrix matrix = new Matrix();
            matrix.postScale(scaleWidth, scaleHeight);
            // 得到新的图片
            newbm = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
        }
        return newbm;
    }

3.绘制文字,因为需要添加竖排文字,所以要自动换行,因为Canvas.drawText不会换行,StaticLayout是android中处理文字的一个工具类,StaticLayout 处理了文字换行的问题,将outerwidth设置成字体大小,则每行只显示一个字符,将其绘制在左边,x坐标为0,y坐标为整体高的1/3

StaticLayout的参数说明:

CharSequence source 文本内容。

int bufstart, int bufend, 开始位置和结束位置。

TextPaint paint 文本画笔对象。

int outerwidth 布局宽度,超出宽度换行显示。

Alignment align 对齐方式,默认是Alignment.ALIGN_LEFT。

TextDirectionHeuristic textDir 文本显示方向。

float spacingmult 行间距倍数,默认是1。

float spacingadd 行距增加值,默认是0。

boolean includepad 文本顶部和底部是否留白。

TextUtils.TruncateAt ellipsize 文本省略方式,有 START、MIDDLE、 END、MARQUEE 四种省略方式(其实还有一个 END_SMALL,但是 Google 并未开放出来)。

int ellipsizedWidth 省略宽度。

int maxLines 最大行数。
 //添加文字
        String s1= "来自诗来诗往·古诗分享";
        TextPaint textPaint = new TextPaint();
        textPaint.setAntiAlias(true);
        textPaint.setTextSize(32.0F);
        textPaint.setColor(Color.BLACK);
        StaticLayout sl= new StaticLayout(s1, textPaint, 32, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
        canvas.translate(0, height/3);
        sl.draw(canvas);

4.保存回收

 		canvas.save();// 保存
        canvas.restore();// 存储
        //回收
        share.recycle();
        newShare.recycle();

5.整体代码

    //生成分享的图片
    private Bitmap getImg() {
        //截取诗词图片
        int width = shareListview.getWidth();
        int height = shareListview.getHeight();
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        shareListview.draw(canvas);

        //添加二维码
        Bitmap share = BitmapFactory.decodeResource(this.getResources(), R.drawable.share_ma);
        //重新设置bitmap大小
        Bitmap newShare = newBitmap(share,height/4,height/4);
        canvas.drawBitmap(newShare, width-newShare.getWidth(), height-newShare.getHeight(), null);

        //添加文字
        String s1= "来自诗来诗往·古诗分享";
        TextPaint textPaint = new TextPaint();
        textPaint.setAntiAlias(true);
        textPaint.setTextSize(32.0F);
        textPaint.setColor(Color.BLACK);
        StaticLayout sl= new StaticLayout(s1, textPaint, 32, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
        canvas.translate(0, height/3);
        sl.draw(canvas);

        canvas.save();// 保存
        canvas.restore();// 存储
        //回收
        share.recycle();
        newShare.recycle();

        return bitmap;
    }

    public static Bitmap newBitmap(Bitmap bitmap, int newWidth, int newHeight) {
        Bitmap newbm = null;
        if (bitmap!=null){
            // 获得图片的宽高
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            // 计算缩放比例
            float scaleWidth = ((float) newWidth) / width;
            float scaleHeight = ((float) newHeight) / height;
            // 取得想要缩放的matrix参数
            Matrix matrix = new Matrix();
            matrix.postScale(scaleWidth, scaleHeight);
            // 得到新的图片
            newbm = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
        }
        return newbm;
    }

6.调用系统分享

	Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), getImg(), null,null));
    Intent imageIntent = new Intent(Intent.ACTION_SEND);
    imageIntent.setType("image/jpeg");
    imageIntent.putExtra(Intent.EXTRA_STREAM, uri);
    startActivity(Intent.createChooser(imageIntent, "诗来诗往"));
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值