Android截屏分享功能

Android截屏分享功能,第1张

概述最近项目需要实现Android截屏分享功能,包括Android截屏获取图片、将图片保存到本地、通知系统相册更新、通过微信、QQ、微博分享截屏图片,本篇文章作为总结回顾。

最近项目需要实现AndroID截屏分享功能,包括AndroID截屏获取图片、将图片保存到本地、通知系统相册更新、通过微信、QQ、微博分享截屏图片,本篇文章作为总结回顾。

    一、AndroID截屏获取图片

    通过对vIEw进行绘制,得到bitmap,可以对Activity、Fragment进行绘制,也可以对其他的VIEw进行绘制。

    1 Activity截图(带空白的状态栏)

public Bitmap shotScreen(Activity activity) {  VIEw vIEw = activity.getwindow().getDecorVIEw();  vIEw.setDrawingCacheEnabled(true);  vIEw.buildDrawingCache();  Bitmap bitmap = Bitmap.createBitmap(vIEw.getDrawingCache(),vIEw.getMeasureDWIDth(),vIEw.getMeasuredHeight());  vIEw.setDrawingCacheEnabled(false);  vIEw.destroyDrawingCache();  return bitmap; }

    2 Activity截图(去掉状态栏)

public Bitmap shotActivityNobar(Activity activity) {  // 获取windows中最顶层的vIEw  VIEw vIEw = activity.getwindow().getDecorVIEw();  vIEw.buildDrawingCache();  // 获取状态栏高度  Rect rect = new Rect();  vIEw.getwindowVisibledisplayFrame(rect);  int statusbarHeights = rect.top;  display display = activity.getwindowManager().getDefaultdisplay();  // 获取屏幕宽和高  int wIDths = display.getWIDth();  int heights = display.getHeight();  // 允许当前窗口保存缓存信息  vIEw.setDrawingCacheEnabled(true);  // 去掉状态栏  Bitmap bmp = Bitmap.createBitmap(vIEw.getDrawingCache(),statusbarHeights,wIDths,heights - statusbarHeights);  // 销毁缓存信息  vIEw.destroyDrawingCache();  return bmp; } 

3 Fragment截图

public Bitmap getFragmentBitmap(Fragment fragment) {  VIEw v = fragment.getVIEw();  v.buildDrawingCache(false);  return v.getDrawingCache(); }

    绘制VIEw的方式只适用于静态VIEw,SurfaceVIEw、IjkVIDeoVIEw等播放视频的VIEw,不能截取到;此外,某些动画类VIEw也不能截取到。针对上述情况,可获取SurfaceVIEw的一帧生成bitmap,将bitmap进行合并,同理动画类VIEw也可以合并。例如,最底层播放视频、中间层播放动画、最上层是普通的vIEw,可将bitmap合并,如下:

public Bitmap combineBitmAPInCenter(Bitmap background,Bitmap mIDBitmap,Bitmap foreground) {   if (!background.isMutable()) {    background = background.copy(Bitmap.Config.ARGB_8888,true);   }   Paint paint = new Paint();   Canvas canvas = new Canvas(background);   int bw = background.getWIDth();   int bh = background.getHeight();   int mw = mIDBitmap.getWIDth();   int mh = mIDBitmap.getHeight();   int mx = (mw - bw) / 2;   int my = (mh - bh) / 2;   canvas.drawBitmap(mIDBitmap,mx,my,paint);   int fw = foreground.getWIDth();   int fh = foreground.getHeight();   int fx = (fw - bw) / 2;   int fy = (fh - bh) / 2;   canvas.drawBitmap(foreground,fx,fy,paint);   canvas.save(Canvas.ALL_SAVE_FLAG);   canvas.restore();   return background; }

    需要注意:vIEw的绘制必须在UI线程完成,否则会crash;bitmap的合并会较耗时,需在子线程完成。

二 、将bitmap保存在本地

public voID saveBitmapTofile(String filename,Bitmap bitmap) {   if (TextUtils.isEmpty(filename) || bitmap == null) return;   try {    file f = new file(filename);    f.createNewfile();    fileOutputStream fOut = new fileOutputStream(f);    bitmap.compress(Bitmap.CompressFormat.JPEG,80,fOut);    fOut.flush();    fOut.close();   } catch (fileNotFoundException e) {    Log.i("ScreenShotUtil","保存失败");    e.printstacktrace();   } catch (IOException e) {    e.printstacktrace();   } } 

三、通知系统相册更新

public voID AlbumScan(String filename) {   MediaScannerConnection.scanfile(mActivity.getApplicationContext(),new String[]{filename},new String[]{"image/jpeg"},null); }

四、通过微信、QQ、微博分享

    通过微信、QQ、微博分享的sdk实现,如下:

    1 分享到微信朋友圈

public voID sharetoWexinCircle() {   if (iShareService.isWeixinInstalled()) {//判断微信是否安装    Bitmap mWXShareBitmap = mLatesBitmap;//将截屏得到的bitmap赋值    WXImageObject imgObject = new WXImageObject(mWXShareBitmap);    WXMediaMessage mediaMessage = new WXMediaMessage();    mediaMessage.mediaObject = imgObject;    Log.i(TAG,"mediaMessage.mediaObject = imgObject");    //设置缩略图    Bitmap thumbBmp = Bitmap.createScaledBitmap(mWXShareBitmap,50,true);    mediaMessage.thumbData = bmpToByteArray(thumbBmp,true);    Log.i(TAG,"设置缩略图");    //request to wx    SendMessagetoWX.Req req = new SendMessagetoWX.Req();    req.transaction = buildTransaction("img");//分享类型是图片    req.message = mediaMessage;    req.scene = SendMessagetoWX.Req.WXSceneTimeline;//分享到朋友圈    Log.i(TAG,"request to wx");    IWXAPI API = WXAPIFactory.createWXAPI(mActivity,"微信分享ID",true);    API.registerapp("微信分享ID");    boolean result = API.sendReq(req);    Log.i(TAG,"share to circle:"+result);   } else {    ToastUtils.show(mActivity,"您没有安装微信客户端");   } } public byte[] bmpToByteArray(final Bitmap bmp,final boolean needRecycle) {   ByteArrayOutputStream output = new ByteArrayOutputStream();   bmp.compress(Bitmap.CompressFormat.PNG,100,output);   if (needRecycle) {    bmp.recycle();   }   byte[] result = output.toByteArray();   try {    output.close();   } catch (Exception e) {    e.printstacktrace();   }   return result; } <pre name="code" >private String buildTransaction(final String type) {   return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis(); }</pre> <h3><a name="t8"></a><br> 

2、分享到微信好友</h3> 

<pre></pre> <pre name="code" > public voID shareWexinFrIEnd() {   if (iShareService.isWeixinInstalled()) {    mWXShareBitmap = mLatesBitmap;    WXImageObject imgObject = new WXImageObject(mWXShareBitmap);    WXMediaMessage mediaMessage = new WXMediaMessage();    mediaMessage.mediaObject = imgObject;    //设置缩略图    Bitmap thumbBmp = Bitmap.createScaledBitmap(mWXShareBitmap,true);    //request to wx    SendMessagetoWX.Req req = new SendMessagetoWX.Req();    req.transaction =buildTransaction("img");    req.message = mediaMessage;    req.scene = SendMessagetoWX.Req.WXScenesession;    IWXAPI API = WXAPIFactory.createWXAPI(mActivity,"share to weixin:"+result );   } else {    ToastUtils.show(mActivity,"您没有安装微信客户端");   } }</pre> <h3><a name="t9"></a>3、分享到QQ好友</h3> <pre name="code" > public voID sharetoQQFrIEnd() {   if (iShareService.isQQInstalled()) {//判断QQ是否安装    Tencent tencent = Tencent.createInstance("qq ID",mActivity);    Bundle params = new Bundle();    params.putString(QQShare.SHARE_TO_QQ_IMAGE_LOCAL_URL,mLatestimagePath);//QQ分享只能分享本地图片,传入本地图片路径    params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE,QQShare.SHARE_TO_QQ_TYPE_IMAGE);    tencent.sharetoQQ(mActivity,params,new iuiListener() {//分享后的回调     @OverrIDe     public voID onComplete(Object o) {      ToastUtils.show(mActivity,"分享成功!");     }     @OverrIDe     public voID onError(UIError uIError) {      ToastUtils.show(mActivity,"分享失败,稍后重试!");      Log.i(TAG,"sharetoQQ onError");     }     @OverrIDe     public voID onCancel() {      Log.i(TAG,"sharetoQQ onCancel");     }    });   } else {    ToastUtils.show(mActivity,"您没有安装QQ客户端");   } }</pre> <pre></pre> <pre></pre> 

4 QQ空间分享

public voID sharetoQQZone() {   if (iShareService.isQQInstalled()) {    Tencent tencent = Tencent.createInstance("QQ ID",mLatestimagePath);    params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE,QQShare.SHARE_TO_QQ_TYPE_IMAGE);    params.putInt(QQShare.SHARE_TO_QQ_EXT_INT,QQShare.SHARE_TO_QQ_FLAG_QZONE_auto_OPEN);    tencent.sharetoQQ(mActivity,new iuiListener() {     @OverrIDe     public voID onComplete(Object o) {      ToastUtils.show(mActivity,"分享失败,请稍后重试!");      Log.i(TAG,"sharetoQQ onError,error code:"+uIError.errorCode);     }     @OverrIDe     public voID onCancel() {      Log.i(TAG,"您没有安装QQ客户端");   } 

5 微博分享 

public voID sharetoWeibo() {   if (iShareService.isWeiboInstalled()) {    Log.i(TAG,"iShareService.isWeiboInstalled()");    IWeiboShareAPI API = WeiboShareSDK.createWeiboAPI(mActivity,"微博ID");    if(API.isWeiboAppInstalled()) {     API.registerapp();    }    WeiboMultiMessage weiboMultiMessage = new WeiboMultiMessage();    Bitmap bitmap = mLatesBitmap;//截图bitmap    ImageObject imageObject = new ImageObject();    imageObject.setimageObject(bitmap);    weiboMultiMessage.imageObject = imageObject;    SendMultiMessagetoWeiboRequest request = new SendMultiMessagetoWeiboRequest();    request.transaction = String.valueOf(System.currentTimeMillis());    request.multiMessage = weiboMultiMessage;    boolean requestWeibo = API.sendRequest(request);    Log.i(TAG,"share to weibo:"+requestWeibo );   } else {    ToastUtils.show(mActivity,"您没有安装微博客户端");   } } 

总结

以上所述是小编给大家介绍的AndroID截屏分享功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

总结

以上是内存溢出为你收集整理的Android截屏分享功能全部内容,希望文章能够帮你解决Android截屏分享功能所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1143459.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存