Android把view保存成图片分享

viewHolder.ma.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) { 
               
                        Log.e("aaa", "二维码url" + erweimaUrl);
                        context.runOnUiThread(new Runnable() {
                            public void run() {
                                dia = new Dialog(context, R.style.Theme_AppCompat_Dialog_Alert);
                                dia.setContentView(R.layout.dialog_bargain_dialog);
                               final RelativeLayout relativeLayout = (RelativeLayout) dia.findViewById(R.id.longsave);
                                RelativeLayout relativeLayout2 = (RelativeLayout)dia.findViewById(R.id.save);
                                RelativeLayout relativeLayout3 = (RelativeLayout)dia.findViewById(R.id.shareWXSceneTimelinez);
                                RelativeLayout relativeLayout4 = (RelativeLayout)dia.findViewById(R.id.shareWXSceneSessionz);
                                Glide.with(context).load(erweimaUrl)
                                	.diskCacheStrategy(DiskCacheStrategy.ALL)
                                	.error(R.mipmap.ic_launcher)
                                	.into((ImageView)dia.findViewById(R.id.start_img));
                                dia.setCanceledOnTouchOutside(true);
                                dia.show();
                                WindowManager.LayoutParams attributes =dia.getWindow().getAttributes();
                                attributes.x = 0;
                                attributes.y = 40;
                                dia.onWindowAttributesChanged(attributes);
                                //分享到朋友圈
                                relativeLayout3.setOnClickListener(new View.OnClickListener() {
                                    public void onClick(View view) {
                                        relativeLayout.draw(new Canvas(Bitmap.createBitmap(relativeLayout.getWidth(), relativeLayout.getWidth(), Bitmap.Config.ARGB_8888)));
                                        relativeLayout.setDrawingCacheEnabled(true);
                                        relativeLayout.measure(View.MeasureSpec.makeMeasureSpec(0, 0), View.MeasureSpec.makeMeasureSpec(0, 0));
                                        relativeLayout.layout(0, 0, relativeLayout.getWidth(), relativeLayout.getHeight());
                                        Bitmap createBitmap = Bitmap.createBitmap(relativeLayout.getDrawingCache());
                                        relativeLayout.setDrawingCacheEnabled(false);
                                        WXImageObject wXImageObject = new WXImageObject(createBitmap);
                                        WXMediaMessage wXMediaMessage = new WXMediaMessage();
                                        wXMediaMessage.mediaObject = wXImageObject;
                                        Bitmap createScaledBitmap = Bitmap.createScaledBitmap(createBitmap, 120, 150, true);
                                        createBitmap.recycle();
                                        wXMediaMessage.thumbData = Util.bmpToByteArray(createScaledBitmap, true);
                                        SendMessageToWX.Req req = new SendMessageToWX.Req();
                                        req.transaction = buildTransaction("img");
                                        req.message = wXMediaMessage;
                                        dia.dismiss();
                                        Log.e("", "onClick: 点击了朋友圈");
                                        req.scene = 1;
                                        api.sendReq(req);
                                        dia.dismiss();
                                    }
                                });
                                //分享给好友
                                relativeLayout4.setOnClickListener(new View.OnClickListener() {
                                    public void onClick(View view) {
                                        relativeLayout.draw(new Canvas(Bitmap.createBitmap(relativeLayout.getWidth(), relativeLayout.getWidth(), Bitmap.Config.ARGB_8888)));
                                        relativeLayout.setDrawingCacheEnabled(true);
                                        relativeLayout.measure(View.MeasureSpec.makeMeasureSpec(0, 0), View.MeasureSpec.makeMeasureSpec(0, 0));
                                        relativeLayout.layout(0, 0, relativeLayout.getWidth(), relativeLayout.getHeight());
                                        Bitmap createBitmap = Bitmap.createBitmap(relativeLayout.getDrawingCache());
                                        relativeLayout.setDrawingCacheEnabled(false);
                                        WXImageObject wXImageObject = new WXImageObject(createBitmap);
                                        WXMediaMessage wXMediaMessage = new WXMediaMessage();
                                        wXMediaMessage.mediaObject = wXImageObject;
                                        Bitmap createScaledBitmap = Bitmap.createScaledBitmap(createBitmap, 120, 150, true);
                                        createBitmap.recycle();
                                        wXMediaMessage.thumbData = Util.bmpToByteArray(createScaledBitmap, true);
                                        SendMessageToWX.Req req = new SendMessageToWX.Req();
                                        req.transaction = buildTransaction("img");
                                        req.message = wXMediaMessage;
                                        dia.dismiss();
                                        Log.e("", "onClick: 点击了微信分享");
                                        req.scene = 0;
                                        api.sendReq(req);
                                        dia.dismiss();
                                    }
                                });
                                //保存到本地
                                relativeLayout2.setOnClickListener(new View.OnClickListener() {
                                    private FileOutputStream out;

                                    public void onClick(View view) {
                                        relativeLayout.draw(new Canvas(Bitmap.createBitmap(relativeLayout.getWidth(), relativeLayout.getWidth(), Bitmap.Config.ARGB_8888)));
                                        relativeLayout.setDrawingCacheEnabled(true);
                                        relativeLayout.measure(View.MeasureSpec.makeMeasureSpec(0, 0), View.MeasureSpec.makeMeasureSpec(0, 0));
                                        relativeLayout.layout(0, 0, relativeLayout.getWidth(), relativeLayout.getHeight());
                                        Bitmap createBitmap = Bitmap.createBitmap(relativeLayout.getDrawingCache());
                                        relativeLayout.setDrawingCacheEnabled(false);
                                        String str = context.getExternalFilesDir("") + "/LALALALA/";
                                        File file = new File(str);
                                        if (!file.exists()) {
                                            file.mkdirs();
                                        }
                                        try {
                                            this.out = new FileOutputStream(new File(str, System.currentTimeMillis() + ".jpg"));
                                            createBitmap.compress(Bitmap.CompressFormat.JPEG, 90, this.out);
                                            System.out.println("_________保存到____sd______指定目录文件夹下____________________");
                                        } catch (FileNotFoundException e) {
                                            e.printStackTrace();
                                        }
                                        try {
                                            this.out.flush();
                                            this.out.close();
                                        } catch (IOException e2) {
                                            e2.printStackTrace();
                                        }
                                        ToastUtil.showShort("图片已经保存至" +context.getExternalFilesDir("") + "/LALALALA/目录文件夹下");
                                        Log.e("aaa", "保存到" + context.getExternalFilesDir("") + "/LALALALA/");
                                    }
                                });
                            }
                        });
                    }
                });
            }
        });

FENJIEXIAN

public String buildTransaction(String str) {
        if (str == null) {
            return String.valueOf(System.currentTimeMillis());
        }
        return str + System.currentTimeMillis();
    }

    private void initSend(final SendMessageToWX.Req req, View view) {
        ((RelativeLayout) view.findViewById(R.id.shareWXSceneSessionz)).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Log.e("", "onClick: 点击了微信分享");
                req.scene = 0;
                api.sendReq(req);
                mDialog.dismiss();
            }
        });
        ((RelativeLayout) view.findViewById(R.id.shareWXSceneTimelinez)).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Log.e("", "onClick: 点击了朋友圈");
                req.scene = 1;
                api.sendReq(req);
                mDialog.dismiss();
            }
        });
        ((TextView) view.findViewById(R.id.cancelbbb)).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Log.e("", "onClick: 点击了关闭了");
                mDialog.dismiss();
            }
        });
    }

布局如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="350dp"
    android:layout_height="570dp">

    <RelativeLayout
        android:id="@+id/vbn"
        android:layout_width="300dp"
        android:layout_height="500dp"
        android:layout_centerHorizontal="true">

        <RelativeLayout
            android:id="@+id/longsave"
            android:layout_width="300dp"
            android:layout_height="500dp"
            android:layout_centerInParent="true"
            android:background="@drawable/ic_launcher_background">

            <RelativeLayout
                android:layout_width="300dp"
                android:layout_height="500dp">

                <ImageView
                    android:id="@+id/start_img"
                    android:layout_width="44dp"
                    android:layout_height="44dp"
                    android:layout_alignParentBottom="true"
                    android:layout_marginLeft="62dp"
                    android:layout_marginBottom="36dp"
                    android:background="@android:color/transparent" />

                <ImageView
                    android:id="@+id/shopgoods"
                    android:layout_width="170dp"
                    android:layout_height="170dp"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="135dp"
                    android:layout_marginRight="20dp"
                    android:background="@mipmap/ic_launcher"/>

                <RelativeLayout
                    android:layout_width="85dp"
                    android:layout_height="85dp"
                    android:layout_alignParentRight="true"
                    android:layout_marginTop="175dp"
                    android:layout_marginRight="20dp">

                    <ImageView
                        android:id="@+id/asd"
                        android:layout_width="85dp"
                        android:layout_height="85dp"
                        android:src="@mipmap/ic_launcher" />

                    <TextView
                        android:id="@+id/overtime"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="8dp"
                        android:layout_marginTop="12dp"
                        android:gravity="center_horizontal"
                        android:lineSpacingExtra="5dp"
                        android:maxEms="5"
                        android:maxLines="3"
                        android:text="截止时间2090-10-01 23:59:59"
                        android:textColor="#000"
                        android:textSize="12sp" />
                </RelativeLayout>


            </RelativeLayout>
        </RelativeLayout>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="300dp"
        android:layout_height="100dp"
        android:layout_below="@+id/vbn"
        android:layout_centerHorizontal="true"
        android:background="#ffffff">

        <RelativeLayout
            android:id="@+id/shareWXSceneTimelinez"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="50dp"
            android:layout_marginTop="0dp">

            <ImageView
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="5dp"
                android:src="@mipmap/ic_launcher" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="2dp"
                android:text="朋友圈"
                android:textSize="10sp" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/shareWXSceneSessionz"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="0dp"
            android:layout_marginRight="100dp">

            <ImageView
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="5dp"
                android:src="@mipmap/ic_launcher" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="2dp"
                android:text="微信好友"
                android:textSize="10sp" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/save"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="0dp"
            android:layout_marginRight="50dp">

            <ImageView
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="5dp"
                android:src="@mipmap/ic_launcher" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="2dp"
                android:text="本地相册"
                android:textSize="10sp" />
        </RelativeLayout>
    </RelativeLayout>
</RelativeLayout>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值