美文网首页
Android 生成携带数据的分享图片

Android 生成携带数据的分享图片

作者: leo567 | 来源:发表于2018-06-20 17:19 被阅读251次

    1.图片

    left:图片 right:加入数据的分享图片.jpg

    2.代码

    //调用
      new SharePicUtil.Builder(this)
                    .setAvatarUrl("http://fd.topitme.com/d/23/ce/1132046888390ce23do.jpg")
                    .setDiligentValue(32)
                    .setQuestionNum(32)
                    .setVideoNum(24)
                    .setRankNum(125489)
                    .setSignNum(5)
                    .setLiveNum(3)
                    .setName("Hi,张大麻子")
                    .build()
                    .setBitmapListener(new SharePicUtil.ShareBitmapListener() {
                        @Override
                        public void shareBitmap(Bitmap bitmap) {
                            iv.setImageBitmap(bitmap);
                        }
                    });
    
    import android.app.Activity;
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Matrix;
    import android.graphics.Paint;
    import android.graphics.Point;
    import android.graphics.PorterDuff;
    import android.graphics.PorterDuffXfermode;
    import android.graphics.Rect;
    import android.net.Uri;
    import android.support.annotation.Nullable;
    import android.text.TextPaint;
    import android.util.AttributeSet;
    import android.util.DisplayMetrics;
    import android.view.Display;
    import android.view.View;
    
    import com.duia.specialarea.R;
    import com.facebook.common.executors.CallerThreadExecutor;
    import com.facebook.common.references.CloseableReference;
    import com.facebook.datasource.DataSource;
    import com.facebook.drawee.backends.pipeline.Fresco;
    import com.facebook.imagepipeline.core.ImagePipeline;
    import com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber;
    import com.facebook.imagepipeline.image.CloseableImage;
    import com.facebook.imagepipeline.request.ImageRequest;
    import com.facebook.imagepipeline.request.ImageRequestBuilder;
    
    import java.lang.reflect.Method;
    import java.math.BigDecimal;
    
    /**
     * @description: 生成分享图片
     * @author: fanrunqi
     * @date: 2018/6/20 11:39
     */
    public class SharePicUtil {
    
        private Activity a;
        private  int picWidth = 1080, picHeight = 1920;   //屏幕宽高单位像素
        private int shareResId = R.drawable.sa_share_bg;  //share 图片路径
        private Bitmap shareBitmap; //重设尺寸后的Bitmap
        private Canvas canvas;    //画板
    
        private String avatarUrl;//用户头像路径
        private Bitmap avatarBitmap;
        private String name;//用户名字
        private String rankNum;//排名数值
        private String diligentValue;//今日勤奋值
        private String signNum;//连续签到的天数
        private String videoNum;//看视频数
        private String questionNum;//做题数
        private String liveNum;//看直播数
    
        private SharePicUtil(Builder builder) {
            a = builder.activity;
            avatarUrl = builder.avatarUrl;
            name = builder.name;
            rankNum = builder.rankNum;
            diligentValue =builder.diligentValue;
            signNum = builder.signNum;
            videoNum = builder.videoNum;
            questionNum = builder.questionNum;
            liveNum = builder.liveNum;
            init();
        }
    
        /**
         * 初始化函数
         */
        private void init(){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    //获取资源图片
                    Bitmap  bitmap = BitmapFactory.decodeResource(a.getResources(),shareResId);
                    //重设图片大小
                    shareBitmap = setImgSize(bitmap,picWidth,picHeight);
                    //生成canvas
                    canvas = new Canvas(shareBitmap);
                    //获取用户头像
                    url2Bitmap();
                }
            }).start();
        }
    
        /**
         *  重设图片宽高
         */
        private Bitmap setImgSize(Bitmap bm,int newWidth,int newHeight){
            // 获得图片的宽高.
            int width = bm.getWidth();
            int height = bm.getHeight();
            // 计算缩放比例.
            float scaleWidth = ((float) newWidth) / width;
            float scaleHeight = ((float) newHeight) / height;
            // 取得想要缩放的matrix参数.
            Matrix matrix = new Matrix();
            matrix.postScale(scaleWidth, scaleHeight);
            // 得到新的图片.
            Bitmap newBm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
            return newBm;
        }
    
        /**
         * 回调接口
         */
        public ShareBitmapListener listener;
        public void setBitmapListener(ShareBitmapListener listener){
            this.listener =listener;
        }
        public interface ShareBitmapListener{
            void shareBitmap(Bitmap bitmap);
        }
    
        /**
         * Fresco获取头像的bitmap
         */
        private void url2Bitmap(){
    
            ImageRequest imageRequest = ImageRequestBuilder
                    .newBuilderWithSource(Uri.parse(avatarUrl))
                    .setProgressiveRenderingEnabled(true)
                    .build();
    
            ImagePipeline imagePipeline = Fresco.getImagePipeline();
            DataSource<CloseableReference<CloseableImage>>
                    dataSource = imagePipeline.fetchDecodedImage(imageRequest,this);
    
            dataSource.subscribe(new BaseBitmapDataSubscriber() {
    
                @Override
                public void onNewResultImpl(@Nullable Bitmap bitmap) {
                    //重设大小为48dp
                    int pix = DensityUtil.dip2px(a,40f);
                    avatarBitmap = getCircleBitmap(setImgSize(bitmap,pix,pix));
                    //画数据信息
                    drawInfo();
                }
    
                @Override
                public void onFailureImpl(DataSource dataSource) {
    
                }
            }, CallerThreadExecutor.getInstance());
    
        }
    
        /**
         * 获取圆形图片
         * @param bitmap
         * @return Bitmap
         * @author caizhiming
         */
        private Bitmap getCircleBitmap(Bitmap bitmap) {
            Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                    bitmap.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(output);
    
            final int color = 0xff424242;
    
            final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setAntiAlias(true);
            canvas.drawARGB(0, 0, 0, 0);
            paint.setColor(color);
            int x = bitmap.getWidth();
    
            canvas.drawCircle(x / 2, x / 2, x / 2, paint);
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
            canvas.drawBitmap(bitmap, rect, rect, paint);
            return output;
    
        }
        /**
         * 在图片上画文字
         */
        private void drawInfo(){
            Paint mBitPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            mBitPaint.setFilterBitmap(true);
            mBitPaint.setDither(true);
            //名字
            Paint mPaint = new TextPaint();
            mPaint.setColor(Color.parseColor("#778aff"));
            mPaint.setAntiAlias(true);
            mPaint.setTextAlign(Paint.Align.LEFT);
            mPaint.setTextSize(DensityUtil.sp2px(a,18));
            canvas.drawText(name,(float)(picWidth*(185/360.0)),(float)(picHeight*(90/640.0)), mPaint);
    
            //绘制头像
            canvas.drawBitmap(avatarBitmap, (float)(picWidth*(279/360.0)), (float)(picHeight*(65/640.0)), mBitPaint);
    
            //勤奋值排名
            mPaint.setColor(Color.parseColor("#798afe"));
            mPaint.setTextAlign(Paint.Align.CENTER);
            mPaint.setTextSize(DensityUtil.sp2px(a,42));
            canvas.drawText(rankNum,(float)(picWidth*(99/360.0)),(float)(picHeight*(234/640.0)), mPaint);
    
            //本周勤奋排名
            mPaint.setColor(Color.parseColor("#022222"));
            mPaint.setTextSize(DensityUtil.sp2px(a,14));
            canvas.drawText("本周勤奋排名",(float)(picWidth*(99/360.0)),(float)(picHeight*(254/640.0)), mPaint);
    
            //今日+
            mPaint.setColor(Color.parseColor("#778aff"));
            mPaint.setTextSize(DensityUtil.sp2px(a,12));
            canvas.drawText("今日+",(float)(picWidth*(226/360.0)),(float)(picHeight*(373/640.0)), mPaint);
    
            mPaint.setTextSize(DensityUtil.sp2px(a,19));
            float textWidth = mPaint.measureText(diligentValue);//计算宽度
            canvas.drawText(diligentValue,(float)(picWidth*(250 /360.0)),(float)(picHeight*(373/640.0)), mPaint);
    
            //勤奋值
            mPaint.setTextSize(DensityUtil.sp2px(a,12));
            canvas.drawText("勤奋值",(float)(picWidth*(260/360.0)+textWidth),(float)(picHeight*(373/640.0)), mPaint);
    
    
            //已连续签到
            mPaint.setColor(Color.parseColor("#666666"));
            mPaint.setTextSize(DensityUtil.sp2px(a,12));
            canvas.drawText("已连续签到",(float)(picWidth*(241/360.0)),(float)(picHeight*(395/640.0)), mPaint);
    
            mPaint.setTextSize(DensityUtil.sp2px(a,20));
            float textWidth1 = mPaint.measureText(signNum);//计算宽度
            canvas.drawText(signNum,(float)(picWidth*(277/360.0)),(float)(picHeight*(395/640.0)), mPaint);
    
            mPaint.setTextSize(DensityUtil.sp2px(a,12));
            canvas.drawText("天",(float)(picWidth*(283/360.0)+textWidth1),(float)(picHeight*(395/640.0)), mPaint);
    
            //看视频数
            mPaint.setFakeBoldText(false);
            mPaint.setColor(Color.parseColor("#999999"));
            mPaint.setTextSize(DensityUtil.sp2px(a,10));
            canvas.drawText("看视频数",(float)(picWidth*(68/360.0)),(float)(picHeight*(550/640.0)), mPaint);
    
            mPaint.setFakeBoldText(true);
            mPaint.setColor(Color.parseColor("#666666"));
            mPaint.setTextSize(DensityUtil.sp2px(a,16));
            canvas.drawText(videoNum,(float)(picWidth*(68/360.0)),(float)(picHeight*(570/640.0)), mPaint);
    
            //已做题数
            mPaint.setFakeBoldText(false);
            mPaint.setColor(Color.parseColor("#999999"));
            mPaint.setTextSize(DensityUtil.sp2px(a,10));
            canvas.drawText("已做题数",(float)(picWidth*(125/360.0)),(float)(picHeight*(550/640.0)), mPaint);
    
            mPaint.setFakeBoldText(true);
            mPaint.setColor(Color.parseColor("#666666"));
            mPaint.setTextSize(DensityUtil.sp2px(a,16));
            canvas.drawText(questionNum,(float)(picWidth*(125/360.0)),(float)(picHeight*(570/640.0)), mPaint);
    
            //已看直播数
            mPaint.setFakeBoldText(false);
            mPaint.setColor(Color.parseColor("#999999"));
            mPaint.setTextSize(DensityUtil.sp2px(a,10));
            canvas.drawText("已看直播数",(float)(picWidth*(178/360.0)),(float)(picHeight*(550/640.0)), mPaint);
    
            mPaint.setFakeBoldText(true);
            mPaint.setColor(Color.parseColor("#666666"));
            mPaint.setTextSize(DensityUtil.sp2px(a,16));
            canvas.drawText(liveNum,(float)(picWidth*(178/360.0)),(float)(picHeight*(570/640.0)), mPaint);
    
            a.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    listener.shareBitmap(shareBitmap);
                }
            });
    
        }
        /**
         * 建造
         */
        public static final class Builder {
    
            private Activity activity;
            private String avatarUrl;//用户头像路径
            private String name;//用户名字
            private String rankNum;//排名数值
            private String diligentValue;//今日勤奋值
            private String signNum;//连续签到的天数
            private String videoNum;//看视频数
            private String questionNum;//做题数
            private String liveNum;//看直播数
    
            public Builder(Activity context) {
                this.activity = context;
            }
    
            public Builder setAvatarUrl(String val) {
                avatarUrl = val;
                return this;
            }
            public Builder setName(String val) {
                name = val;
                return this;
            }
            public Builder setRankNum(int val) {
                rankNum = String.valueOf(val);
                return this;
            }
            public Builder setDiligentValue(int val) {
                diligentValue = String.valueOf(val);
                return this;
            }
            public Builder setSignNum(int val) {
                signNum = String.valueOf(val);
                return this;
            }
            public Builder setVideoNum(int val) {
                videoNum = String.valueOf(val);
                return this;
            }
            public Builder setQuestionNum(int val) {
                questionNum = String.valueOf(val);
                return this;
            }
            public Builder setLiveNum(int val) {
                liveNum = String.valueOf(val);
                return this;
            }
    
            public SharePicUtil build() {
               return new SharePicUtil(this);
            }
        }
    
    
    }
    
    

    DensityUtil上一篇文章贴了代码,这里就不再重复

    相关文章

      网友评论

          本文标题:Android 生成携带数据的分享图片

          本文链接:https://www.haomeiwen.com/subject/gypxyftx.html