android仿知乎头条的截屏分享

仿知乎头条的截屏分享功能

截屏首先无固定页面,那么直接写入BaseActivity中最稳妥
步骤如下:
1、在BaseActivity中
private ScreenShotListenManager screenShotListenManager;
private Context mContext;

 @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
	  screenShotListenManager = ScreenShotListenManager.newInstance(this);
        mContext = this;
}

    @Override
    protected void onResume() {
        super.onResume();
        startScreenShotListen();
    }

    @Override
    protected void onPause() {
        super.onPause();
        stopScreenShotListen();
    }
 /**
    
     * 开始监听
     */
    private void startScreenShotListen() {
        if (!MindApplication.getInstance().getHasScreenShotListener() && screenShotListenManager != null) {
            screenShotListenManager.setListener(new ScreenShotListenManager.OnScreenShotListener() {
                @Override
                public void onShot(String imagePath) {
                    if (!DoubleClickUtils.isFastDoubleClick()){
                        BaseHelper.showShareScreenShotPopupWindow(MVPBaseActivity.this,mContext,screenShotListenManager,imagePath);
                    }
                }
            });
            screenShotListenManager.startListen();
            MindApplication.getInstance().setHasScreenShotListener(true);
        }
    }

    /**
     * 停止监听
     */
    private void stopScreenShotListen() {
        if (MindApplication.getInstance().getHasScreenShotListener() && screenShotListenManager != null) {
            screenShotListenManager.stopListen();
            MindApplication.getInstance().setHasScreenShotListener(false);
        }
    }
2、获取合成带有二维码的截屏图帮助类BaseHelper
public class BaseHelper {

    /**
     * 截屏分享弹框显示
     */
    public static void showShareScreenShotPopupWindow(Activity activity, Context mContext, ScreenShotListenManager screenShotListenManager, String imagePath){
        Log.d("mvp", "BaseActivity -> onShot: " + "获得截图路径:" + imagePath);
        //合成过后的图片(带底部二维码)
        Bitmap screenShotBitmap = screenShotListenManager.createScreenShotBitmap(mContext, imagePath);
        PictureUtil.saveScreenshotImageToGallery(mContext,screenShotBitmap);
        XXPermissionsHelper.requestPermission(activity, new OnPermissionCallback() {
            @Override
            public void onGranted() {
                if (null != AppManager.getAppManager().currentActivity().findViewById(R.id.container_view)) {
                    //Activity的外层view的id定义成一样的,否则出错,无法截屏弹窗分享  id = R.id.container_view
                    CustomShareScreenShotPopupWindow sharePopupWindow = new CustomShareScreenShotPopupWindow(mContext, screenShotBitmap);
                    sharePopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
                    sharePopupWindow.showAtLocation(AppManager.getAppManager().currentActivity().findViewById(R.id.container_view), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
                }
            }
        }, Manifest.permission.READ_PHONE_STATE, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    }
}
3、自定义的popupwindow,上半部分放ImageView,下半部分放弹出的分享弹框
/**
 * @author DotaWang
 * @date 2019/10/30
 * @description 自定义截屏带底部分享按钮操作栏
 */
public class CustomShareScreenShotPopupWindow extends PopupWindow {

    private Context mContext;
    private List<ShareIconBean> list = new ArrayList<>();

    private ImageView ivShareScreenShot;
    private TextView tvCancel;
    private GridView gridView1;
    private HorizontalScrollView scrollView1;
    private GridViewShareAdapter adapter;
    //分享的图片
    private Bitmap bitmap;
    private View mMenuView;

    String[] title1 = new String[]{"微信好友", "朋友圈", "新浪微博", "QQ", "QQ空间", "微信收藏"};
    Integer[] icon1 =  new Integer[]{
            R.mipmap.share_wechat,
            R.mipmap.share_wechat_circle,
            R.mipmap.share_sina,
            R.mipmap.share_qq,
            R.mipmap.share_qq_zone,
            R.mipmap.share_wechat_favorite
    };

    public CustomShareScreenShotPopupWindow(Context mContext, Bitmap bitmap) {
        super(mContext);
        this.mContext = mContext;
        this.bitmap = bitmap;

        init();
    }

    private void init() {
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (inflater != null) {
            mMenuView = inflater.inflate(R.layout.dialog_share_screenshot, null);
        }
        bindDialogViews(mMenuView);
        this.setContentView(mMenuView);
        this.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setAnimationStyle(R.style.BottomDialogAnimation);
        this.setFocusable(true);
        ColorDrawable dw = new ColorDrawable(Color.WHITE);
        this.setBackgroundDrawable(dw);
        setBackgroundAlpha(0.5f);
    }

    private void bindDialogViews(View v) {
        ivShareScreenShot = v.findViewById(R.id.iv_screenshot_share);
        tvCancel = v.findViewById(R.id.tv_cancel);
        gridView1 = v.findViewById(R.id.gridView1_share);
        scrollView1 = v.findViewById(R.id.scrollView_container1);

        initData();
        setUpWindow();
        setGridViewAdapter();
        initView();
        initListener();
    }

    private void initData() {
        //第一行分享
        list.add(new ShareIconBean(icon1[0],title1[0], SHARE_MEDIA.WEIXIN));
        list.add(new ShareIconBean(icon1[1],title1[1], SHARE_MEDIA.WEIXIN_CIRCLE));
        list.add(new ShareIconBean(icon1[2],title1[2], SHARE_MEDIA.SINA));
        list.add(new ShareIconBean(icon1[3],title1[3], SHARE_MEDIA.QQ));
        list.add(new ShareIconBean(icon1[4],title1[4], SHARE_MEDIA.QZONE));
        list.add(new ShareIconBean(icon1[5],title1[5], SHARE_MEDIA.WEIXIN_FAVORITE));
    }
    private void setUpWindow() {
        int size = list.size();
        DisplayMetrics dm = new DisplayMetrics();
        WindowManager.LayoutParams paramsGravity = ((Activity)mContext).getWindow().getAttributes();
        paramsGravity.gravity = Gravity.BOTTOM;
        ((Activity)mContext).getWindow().setAttributes(paramsGravity);
        //宽高尺寸设置
        ((Activity)mContext).getWindowManager().getDefaultDisplay().getMetrics(dm);
        float density = dm.density;
        int allWidth = (int) (110 * size * density);
        int itemWidth = (int) (80 * density);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                allWidth, DPUtils.dip2px(mContext,80));
        gridView1.setLayoutParams(params);
        gridView1.setColumnWidth(itemWidth);
        gridView1.setHorizontalSpacing(10);
        gridView1.setStretchMode(GridView.NO_STRETCH);
        gridView1.setNumColumns(size);
    }
    private void setGridViewAdapter() {
        adapter = new GridViewShareAdapter(mContext);
        adapter.setList(list);
        gridView1.setAdapter(adapter);
    }
    private void initView() {
        Glide.with(mContext).load(bitmap).into(ivShareScreenShot);
    }
    private void initListener() {
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                setBackgroundAlpha(1f);
                dismiss();
            }
        });
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setBackgroundAlpha(1.0f);
                dismiss();
            }
        });
        gridView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ShareIconBean bean = (ShareIconBean) adapter.getItem(position);

                if (null!= bean && null!= bean.getType() && null!= bitmap){
                    switch (bean.getType()) {
                        case WEIXIN:
                            ShareHelper.qqShareBitmap(bitmap,(Activity) mContext,SHARE_MEDIA.WEIXIN);
                            break;
                        case WEIXIN_CIRCLE:
                            ShareHelper.qqShareBitmap(bitmap,(Activity) mContext,SHARE_MEDIA.WEIXIN_CIRCLE);
                            break;
                        case SINA:
                            UMImage image = new UMImage((Activity) mContext, bitmap);
                            new ShareAction((Activity) mContext)
                                    .setPlatform(SHARE_MEDIA.SINA)
                                    .withMedia(image)
                                    .share();
                            break;
                        case QQ:
                            ShareHelper.qqShareBitmap(bitmap,(Activity) mContext,SHARE_MEDIA.QQ);
                            break;
                        case QZONE:
                            ShareHelper.qqShareBitmap(bitmap,(Activity) mContext,SHARE_MEDIA.QZONE);
                            break;
                        case WEIXIN_FAVORITE:
                            ShareHelper.qqShareBitmap(bitmap,(Activity) mContext,SHARE_MEDIA.WEIXIN_FAVORITE);
                            break;
                        default:
                            break;
                    }
                }
                dismiss();
            }
        });
    }

    /**
     * 设置背景遮罩色
     * @param bgAlpha
     */
    public void setBackgroundAlpha(float bgAlpha){
        WindowManager.LayoutParams lp = ((Activity)mContext).getWindow().getAttributes();
        lp.alpha = bgAlpha;
        ((Activity)mContext).getWindow().setAttributes(lp);
    }
}
4、adapter适配器
public class GridViewShareAdapter extends BaseAdapter {
    private Context mContext;
    private List<ShareIconBean> list;

    public GridViewShareAdapter(Context context) {
        this.mContext = context;
    }

    public GridViewShareAdapter(Context context,List<ShareIconBean> list) {
        this.mContext = context;
        this.list = list;
    }

    public void setList(List<ShareIconBean> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return (null == list ? 0 : list.size());
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Hodler hodler;
        if (null == convertView){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.view_item_share,null);
            hodler = new Hodler();
            hodler.imageView = (ImageView) convertView.findViewById(R.id.iv_icon_share);
            hodler.textView = (TextView) convertView.findViewById(R.id.tv_name_share);
            convertView.setTag(hodler);
        }else {
            hodler = (Hodler) convertView.getTag();
        }
        hodler.textView.setText(list.get(position).getTitle());
        Glide.with(mContext).load(list.get(position).getPic()).into(hodler.imageView);
        return convertView;
    }

    class Hodler{
        ImageView imageView;
        TextView textView;
    }
}
5、需要的方法
public class ShareHelper{
	public static void qqShareBitmap(Bitmap bitmap, Activity mAct, SHARE_MEDIA share_media) {
        UMImage image = new UMImage(mAct, bitmap);
        new ShareAction(mAct).setPlatform(share_media).
                  withMedia(image).setCallback(new UmMethod().getUMShareListener(mAct)).share();
    }
}

public class ShareIconBean implements Serializable {
    private int pic;//图片
    private String title;//文字
    private SHARE_MEDIA type;

    public int getPic() {
        return pic;
    }

    public void setPic(int pic) {
        this.pic = pic;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }


    public SHARE_MEDIA getType() {
        return type;
    }

    public void setType(SHARE_MEDIA type) {
        this.type = type;
    }

    public ShareIconBean(int pic, String title, SHARE_MEDIA type) {
        this.pic = pic;
        this.title = title;
        this.type=type;
    }
}
6、需要的xml布局

dialog_share_screenshot.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#909193"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/iv_screenshot_share"
        android:layout_width="match_parent"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="72dp"
        android:layout_marginRight="72dp"
        android:background="#909193"
        android:scaleType="fitCenter"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="center_horizontal"/>

    <LinearLayout
        android:background="@drawable/shape_radius20_f5f6f8"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/tv_choose_share"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:textSize="14sp"
            android:gravity="center"
            android:textColor="@color/color_black_ff333333"
            android:text="截屏分享给好友"/>

        <HorizontalScrollView
            android:id="@+id/scrollView_container1"
            android:scrollbars="none"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <LinearLayout
                android:orientation="vertical"
                android:layout_marginTop="16dp"
                android:layout_width="wrap_content"
                android:layout_height="match_parent">
                <GridView
                    android:id="@+id/gridView1_share"
                    android:gravity="center"
                    android:scrollbars="none"
                    android:numColumns="auto_fit"
                    android:layout_width="match_parent"
                    android:layout_height="80dp"/>
            </LinearLayout>
        </HorizontalScrollView>

        <TextView
            android:id="@+id/tv_cancel"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center"
            android:text="@string/cancel"
            android:textSize="16sp"
            android:textColor="@color/_323232"/>
    </LinearLayout>
</LinearLayout>


view_item_share.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/iv_icon_share"
        android:scaleType="fitXY"
        android:layout_gravity="center"
        android:layout_width="40dp"
        android:layout_height="40dp" />
    <TextView
        android:id="@+id/tv_name_share"
        android:gravity="center"
        android:layout_gravity="center"
        android:textSize="@dimen/sp_12"
        android:layout_marginTop="12dp"
        android:textColor="@color/_323232"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

结束!

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值