zoukankan      html  css  js  c++  java
  • Android+Sqlite 实现古诗阅读应用(三)

      往期传送门:

      Android+Sqlite 实现古诗阅读应用(一)  

      Android+Sqlite 实现古诗阅读应用(二)

      加入截图分享的功能。

       很多应用都有分享的功能,我也想在我的古诗App里加入这个功能,单纯的发送文字看起来太逊了,我决定模仿UC浏览器那样发送古诗的截图,使用官方的分享需要授权KEY,太过麻烦所以打算使用系统的分享。

       1.在meau里添加这个item:

    1     <item
    2         android:id="@+id/menu_item_share"
    3         android:showAsAction="ifRoom"
    4         android:title="分享"
    5         />

       

       2.新建全局变量Intent:

       

    1     private void setshare(){
    2         sendIntent = new Intent();
    3         sendIntent.setAction(Intent.ACTION_SEND);
    4         sendIntent.putExtra(Intent.EXTRA_STREAM, shot());
    5         sendIntent.setType("image/jpeg");
    6     }

      然后在meau的onOptionsItemSelected()方法里设置按下运行setshare()方法,新建一个intent,设置活动为send,然后传入的参数,是一个图片的uri通过shot生成。

      

     1 private Uri shot() {
     2         View view = getWindow().getDecorView();
     3         Display display = this.getWindowManager().getDefaultDisplay();
     4         view.layout(0, 0, display.getWidth(), display.getHeight());
     5         view.setDrawingCacheEnabled(true);//允许当前窗口保存缓存信息,这样getDrawingCache()方法才会返回一个Bitmap
     6 
     7         Rect frame = new Rect();
     8         this.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
     9         int statusBarHeight = frame.top;
    10         // 获取屏幕长和高
    11         int width = this.getWindowManager().getDefaultDisplay().getWidth();
    12         int height = this.getWindowManager().getDefaultDisplay()
    13                 .getHeight();
    14         TypedArray actionbarSizeTypedArray = this.obtainStyledAttributes(new int[]{      //测量actionbar的宽度
    15                 android.R.attr.actionBarSize});
    16         int h = (int)actionbarSizeTypedArray.getDimension(0, 0);
    17         Bitmap bmp = Bitmap.createBitmap(view.getDrawingCache(),0, statusBarHeight+h, width, height  //传入的几个参数分别为,数据源(要截的view),x轴坐标,y轴坐标,宽,长(屏幕-srarusbar-actionbar)
    18                 - statusBarHeight-h);
    19         File f = new File(Environment.getExternalStorageDirectory(),      //写入文件流
    20                 "output_image.jpg");
    21         FileOutputStream fOut = null;
    22         try {
    23             fOut = new FileOutputStream(f);
    24         } catch (FileNotFoundException e) {
    25             e.printStackTrace();
    26         }
    27         bmp.compress(Bitmap.CompressFormat.JPEG, 100, fOut);            
    28         try {
    29             assert fOut != null;
    30             fOut.flush();
    31         } catch (IOException e) {
    32             e.printStackTrace();
    33         }
    34         try {
    35             fOut.close();
    36         } catch (IOException e) {
    37             e.printStackTrace();
    38         }
    39         return Uri.fromFile(f);                           //返回生成的文件的URI
    40     }

      然后:

      

     case R.id.menu_item_share:
                    setmShareActionProvider();
                    startActivity(Intent.createChooser(sendIntent,"图片"));
                    break;

      设置点击启动,就大功告成了,我们就能截到图片,而且没有actionbar和statusbar的干扰了。

      

          发送了图片

          接收到图片

        

      嗯,有朋友反映之前的一篇内容太多了,看代码感觉特别乱,这次的就减少了很多,不过这个小功能虽然小,但是还是很有用的,很多东西都会用到截图分享的功能哦。

      喜欢的请点赞吧!!!

      

  • 相关阅读:
    AUDIT审计的一些使用
    HOW TO PERFORM BLOCK MEDIA RECOVERY (BMR) WHEN BACKUPS ARE NOT TAKEN BY RMAN. (Doc ID 342972.1)
    使用BBED理解和修改Oracle数据块
    Using Class of Secure Transport (COST) to Restrict Instance Registration in Oracle RAC [ID 1340831.1]
    调试利器GDB概念
    第4章 思科IOS
    第3章 ip地址和子网划分
    第2章 TCPIP
    2020年阅读过的黑客资源推荐篇
    第1章 计算机网络
  • 原文地址:https://www.cnblogs.com/lfk-dsk/p/4547686.html
Copyright © 2011-2022 走看看