生成海报分享bitmap图

bitmap = createBitmap(relativeLayout);
WXImageObject imgObj =new WXImageObject(bitmap);
//创建WXMediaMessage对象,并包装创建WXImageObject对象
WXMediaMessage msg =new WXMediaMessage();
msg.mediaObject= imgObj;
//压缩图像
Bitmap thumbBmp = Bitmap.createScaledBitmap(bitmap,120,150, true);
bitmap.recycle();
//释放图像所占用的内存资源
msg.thumbData= bmpToByteArray(thumbBmp, true);

SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransction("img");
        //发送的内容或者对象
        req.message = msg;
        req.scene = SendMessageToWX.Req.WXSceneSession;
        wxapi.sendReq(req);

 

 

 

 

 

 

 

 

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btnCommon;
    private Button btnScroll;
    private Button btnInflate;
    private RelativeLayout container;
    private RelativeLayout llScroll;
    private ImageView imageView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnCommon = findViewById(R.id.btn_common);
        btnScroll = findViewById(R.id.btn_scroll);
        btnInflate = findViewById(R.id.btn_inflate);
        container = findViewById(R.id.container);
        llScroll = findViewById(R.id.ll_scroll);
        imageView = findViewById(R.id.img);
        btnCommon.setOnClickListener(this);
        btnScroll.setOnClickListener(this);
        btnInflate.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        Bitmap bitmap=null;
        switch (v.getId()) {
            case R.id.btn_common:
                bitmap = createBitmap(llScroll);
                imageView.setImageBitmap(bitmap);

                break;
            case R.id.btn_scroll:
                bitmap = createBitmap2(llScroll);
                imageView.setImageBitmap(bitmap);
                break;
            case R.id.btn_inflate:
                View view = LayoutInflater.from(this).inflate(R.layout.view_inflate, null, false);
                bitmap = createBitmap3(view, getScreenWidth(), getScreenHeight());
                break;
            default:
                break;
        }
        saveBitmap(bitmap);

    }


    private Bitmap createBitmap(View view) {
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();
        return bitmap;
    }

    public Bitmap createBitmap2(View v) {
        Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bitmap);
        c.drawColor(Color.WHITE);
        v.draw(c);
        return bitmap;
    }

    public Bitmap createBitmap3(View v, int width, int height) {
        //测量使得view指定大小
        int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
        int measuredHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY);
        v.measure(measuredWidth, measuredHeight);
        //调用layout方法布局后,可以得到view的尺寸大小
        v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
        Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bitmap);
        c.drawColor(Color.WHITE);
        v.draw(c);
        return bitmap;
    }

    private void saveBitmap(Bitmap bitmap) {
        FileOutputStream fos;
        try {
            File root = Environment.getExternalStorageDirectory();
            File file = new File(root, "test.png");
            fos = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
            fos.flush();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public int getScreenHeight() {
        return getResources().getDisplayMetrics().heightPixels;
    }

    public int getScreenWidth() {
        return getResources().getDisplayMetrics().widthPixels;
    }

    public void saveBitmapFile(Bitmap bitmap){
        File file=new File("/mnt/sdcard/pic/01.jpg");
        try {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
            bos.flush();
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦想不上班

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值