zoukankan      html  css  js  c++  java
  • Android使用scrollview截取整个的屏幕并分享微信

    先看看截图效果图

    截取scrollview的屏幕
     1     /**
     2      * 截取scrollview的屏幕
     3      **/
     4     public static Bitmap getScrollViewBitmap(ScrollView scrollView) {
     5         int h = 0;
     6         Bitmap bitmap;
     7         for (int i = 0; i < scrollView.getChildCount(); i++) {
     8             h += scrollView.getChildAt(i).getHeight();
     9         }
    10         // 创建对应大小的bitmap
    11         bitmap = Bitmap.createBitmap(scrollView.getWidth(), h,
    12                 Bitmap.Config.RGB_565);
    13         final Canvas canvas = new Canvas(bitmap);
    14         scrollView.draw(canvas);
    15         return bitmap;
    16     }
    
    

    压缩图片

     1     /**
     2      * 压缩图片
     3      * @param image
     4      * @return
     5      */
     6     public static Bitmap compressImage(Bitmap image, int quality) {
     7         ByteArrayOutputStream baos = new ByteArrayOutputStream();
     8         // 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
     9         image.compress(Bitmap.CompressFormat.JPEG, quality, baos);
    10         int options = 100;
    11         // 循环判断如果压缩后图片是否大于100kb,大于继续压缩
    12 //        while (baos.toByteArray().length / 1024 > 30) {
    13             // 重置baos
    14             baos.reset();
    15             // 这里压缩options%,把压缩后的数据存放到baos中
    16            image.compress(Bitmap.CompressFormat.JPEG, quality, baos);
    17             // 每次都减少10
    18 //            options -= 10;
    19 //        }
    20         // 把压缩后的数据baos存放到ByteArrayInputStream中
    21         ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
    22         // 把ByteArrayInputStream数据生成图片
    23         return BitmapFactory.decodeStream(isBm, null, null);
    24     }

    点击按钮弹窗分享dialog提示窗

     1     //----点分享朋友弹出PopupWindow
     2     private void showPopupWindow() {
     3         View contentView = LayoutInflater.from(GroupBulkActivity.this).inflate(R.layout.popupdow_share, null, false);
     4         window = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
     5         window.setOutsideTouchable(true);
     6         window.setOnDismissListener(new PopupWindow.OnDismissListener() {
     7             @Override
     8             public void onDismiss() {
     9                 backgroundAlpha(GroupBulkActivity.this, 1.0f);
    10             }
    11         });
    12         backgroundAlpha(GroupBulkActivity.this, 0.5f);//0.0-1.0
    13         iv_wx = contentView.findViewById(R.id.iv_image1);
    14         iv_fiend = contentView.findViewById(R.id.iv_image2);
    15         im_delt = contentView.findViewById(R.id.im_delt);
    16         rl_delt = contentView.findViewById(R.id.rl_delt);
    17 
    18         window.showAtLocation(LayoutInflater.from(GroupBulkActivity.this).inflate(R.layout.activity_group_bulk, null), Gravity.BOTTOM, 0, 0);
    19 
    20         iv_wx.setOnClickListener(new View.OnClickListener() {
    21             @Override
    22             public void onClick(View v) {
    23                wechatShare(0);
    24                 window.dismiss();
    25                 backgroundAlpha(GroupBulkActivity.this, 1f);
    26             }
    27         });
    28         iv_fiend.setOnClickListener(new View.OnClickListener() {
    29             @Override
    30             public void onClick(View v) {
    31                 wechatShare(1);
    32                 window.dismiss();
    33                 backgroundAlpha(GroupBulkActivity.this, 1f);
    34             }
    35         });
    36         rl_delt.setOnClickListener(new View.OnClickListener() {
    37             @Override
    38             public void onClick(View v) {
    39                 window.dismiss();
    40                 backgroundAlpha(GroupBulkActivity.this, 1f);
    41             }
    42         });
    43     }

    dailog布局

      1 <?xml version="1.0" encoding="utf-8"?>
      2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      3                 android:id="@+id/parent"
      4                 android:layout_width="match_parent"
      5                 android:layout_height="match_parent">
      6 
      7 
      8     <LinearLayout
      9         android:id="@+id/ll_share"
     10         android:layout_width="match_parent"
     11         android:layout_height="183dp"
     12         android:background="#FFFFFF"
     13         android:orientation="vertical">
     14 
     15         <TextView
     16             android:layout_width="match_parent"
     17             android:layout_height="1px"
     18             android:background="#e4e4e4"/>
     19 
     20         <RelativeLayout
     21             android:layout_width="match_parent"
     22             android:layout_height="45dp">
     23 
     24             <TextView
     25                 android:id="@+id/item_popupwindows_Photo"
     26                 android:layout_width="match_parent"
     27                 android:layout_height="55dp"
     28                 android:background="@drawable/bt_nobgd"
     29                 android:gravity="center"
     30                 android:text="分享给好友"
     31                 android:textColor="#585858"
     32                 android:textSize="14sp"/>
     33 
     34             <RelativeLayout
     35                 android:id="@+id/rl_delt"
     36                 android:layout_width="wrap_content"
     37               android:layout_marginLeft="320dp"
     38                 android:layout_height="55dp">
     39                 <ImageView
     40                     android:id="@+id/im_delt"
     41                     android:layout_width="14dp"
     42                     android:layout_height="14dp"
     43                     android:layout_alignParentRight="true"
     44                     android:layout_marginRight="21dp"
     45                     android:layout_marginTop="14dp"
     46                     android:gravity="center"
     47                     android:src="@drawable/down"
     48                     android:textColor="#585858"
     49                     android:textSize="12sp"/>
     50             </RelativeLayout>
     51 
     52 
     53         </RelativeLayout>
     54 
     55         <TextView
     56             android:layout_width="match_parent"
     57             android:layout_height="1px"
     58             android:background="#9e8888"/>
     59 
     60 
     61         <LinearLayout
     62             android:layout_width="match_parent"
     63             android:layout_height="match_parent"
     64             android:clipChildren="false"
     65             >
     66 
     67 
     68             <LinearLayout
     69                 android:layout_width="0dp"
     70                 android:layout_height="wrap_content"
     71                 android:layout_marginTop="40dp"
     72                 android:layout_weight="1"
     73                 android:gravity="center"
     74                 android:orientation="vertical">
     75 
     76                 <ImageView
     77                     android:id="@+id/iv_image1"
     78                     android:layout_width="50dp"
     79                     android:layout_height="50dp"
     80                     android:src="@drawable/weixin"/>
     81 
     82                 <TextView
     83                     android:layout_width="wrap_content"
     84                     android:layout_height="wrap_content"
     85                     android:textColor="#333333"
     86                     android:textSize="13sp"
     87                     android:layout_marginTop="5dp"
     88                     android:text="微信"/>
     89 
     90 
     91             </LinearLayout>
     92 
     93             <LinearLayout
     94                 android:layout_width="0dp"
     95                 android:layout_height="wrap_content"
     96 
     97                 android:layout_weight="1"
     98                 android:gravity="center"
     99                 android:orientation="vertical">
    100 
    101                 <ImageView
    102 
    103                     android:id="@+id/iv_image2"
    104                     android:layout_width="50dp"
    105                     android:layout_height="50dp"
    106                     android:layout_marginTop="40dp"
    107                     android:src="@drawable/pengyou"/>
    108 
    109                 <TextView
    110                     android:layout_width="wrap_content"
    111                     android:layout_height="wrap_content"
    112                     android:textColor="#333333"
    113                     android:textSize="13sp"
    114                     android:layout_marginTop="5dp"
    115                     android:text="朋友圈"/>
    116             </LinearLayout>
    117         </LinearLayout>
    118     </LinearLayout>
    119 </RelativeLayout>

    分享图片到微信或盆友圈

     1     private void wechatShare(int flag) {
     2         String baseUrl = Cache.exist(CacheKey.BASE_URL) ? Cache.get(CacheKey.BASE_URL).toString() : URL.BASE_URL;
     3         // 分享图片
     4         Bitmap mBitmap = getScrollViewBitmap(scrollView);
     5 //        Bitmap mBitmap = CodeUtils.createImage(data.getCodeUrl(), 200, 200, BitmapFactory.decodeResource(getResources(), R.drawable.logo));
     6         //初始化WXImageObject和WXMediaMessage对象
     7         WXImageObject imageObject = new WXImageObject(compressImage(mBitmap, 80));
     8         WXMediaMessage msg = new WXMediaMessage();
     9         msg.mediaObject = imageObject;
    10 //        mBitmap = getScrollViewBitmap(scrollView);
    11         msg.setThumbImage(Bitmap.createScaledBitmap(mBitmap, 200, 200, true));
    12 
    13         mBitmap.recycle();
    14         //构造一个Req
    15         SendMessageToWX.Req req = new SendMessageToWX.Req();
    16         req.transaction = String.valueOf(System.currentTimeMillis());
    17         req.message = msg;
    18         req.scene = flag == 0 ? SendMessageToWX.Req.WXSceneSession : SendMessageToWX.Req.WXSceneTimeline;
    19         MyApp.api.sendReq(req);
    20 
    21     }

    最终实现效果如图所示........

     
  • 相关阅读:
    【java】之位运算^,&,<<,>>,<<<,>>>总结
    【java】之算法复杂度o(1), o(n), o(logn), o(nlogn)
    【springboot】之自动配置原理
    【git】之使用shell脚本提交代码
    【linux】之日志查看
    【mysql】字段类型和长度的解释
    老生常谈:++a与a--区别
    关于tr:hover变色的问题
    服务器端渲染VS浏览器端渲染
    HTTP状态码302、303、307区别
  • 原文地址:https://www.cnblogs.com/monkey0928/p/10784703.html
Copyright © 2011-2022 走看看