屏幕截取--背景图片被状态栏、标题栏遮挡,截取整个背景图片

2016-06-22  本文已影响284人  BangAiN

项目中有一个截取屏幕,分享到微信的功能。


share_bg2.jpg

如图所示,图片在app内部显示时,图片顶部的文本需要被遮挡住。如果图片仅仅通过titleBar遮挡,是遮挡不住的。看到ios那边的实现后,决定将图片对应的控件的顶部移到StatusBar的位置,让statusbar和titlebar一块遮挡。
布局文件:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:fitsSystemWindows="true"
             android:id="@+id/fl_root"
             android:orientation="vertical"
             tools:context=".webview.ShareFerryInfoActivity">
    //ll_content和navigation_title嵌套在FrameLayout中
    //share_bg即为要显示的图片背景
    <LinearLayout
        android:id="@+id/ll_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/share_bg"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/ll_text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="@dimen/dimen_73_dip"
            android:layout_marginTop="@dimen/dimen_60_dip"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/dimen_35_dip"
                android:src="@drawable/share_ferry"/>

            <TextView
                android:id="@+id/tv_ferry_share_mdl"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#b8dbff"
                android:textSize="@dimen/small_text_size"/>

            <TextView
                android:id="@+id/tv_ferry_share_leg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#b8dbff"
                android:textSize="@dimen/small_text_size"/>

            <TextView
                android:id="@+id/tv_ferry_share_date"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#b8dbff"
                android:textSize="@dimen/small_text_size"/>

            <TextView
                android:id="@+id/tv_ferry_share_seats"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#b8dbff"
                android:textSize="@dimen/small_text_size"/>

            <TextView
                android:id="@+id/tv_ferry_share_sale"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#b8dbff"
                android:textSize="@dimen/small_text_size"/>

            <TextView
                android:id="@+id/tv_ferry_share_discount"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="#b8dbff"
                android:textSize="@dimen/small_text_size"/>

            <TextView
                android:id="@+id/tv_ferry_share_rule"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#b8dbff"
                android:textSize="@dimen/small_text_size"/>
        </LinearLayout>

    </LinearLayout>
//titlebar
    <include
        layout="@layout/navigation_title"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dimen_70_dip"
        android:gravity="top"/>
</FrameLayout>

1.将ll_content移动到statusbar的位置

llContent = (LinearLayout)findViewById(R.id.ll_content);
int statusHeight = ScreenUtils.getStatusHeight(this);
//设置TranslationY
llContent.setTranslationY(-statusHeight);

2.ll_content整体往上移动statusHeight高度后,会导致底部相应高度的空白出现


abc.jpg
FrameLayout flRoot = (FrameLayout) findViewById(R.id.fl_root);
ViewTreeObserver observer = flRoot.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                ViewGroup.LayoutParams layoutParams = llContent.getLayoutParams();
                layoutParams.width = ScreenUtils.getScreenWidth(ShareFerryInfoActivity.this);
                layoutParams.height = ScreenUtils.getScreenHeight(ShareFerryInfoActivity.this);
                llContent.setLayoutParams(layoutParams);
            }
        });

上述代码中ViewTreeObserver也同样解决华为Mate7下虚拟按键显示、隐藏导致的底部显示虚拟按键高度的空白。
3.微信分享图片到好友,朋友圈
在测试过程中,发现三星s6以及note5分享给好友可以使用,而分享到朋友圈则不能使用,屏幕会闪烁一下。查看打印日志发现,当前app发送给微信的意图Intent传送的字节数过大。解决:修改截图生成的Bitmap的尺寸或者图片质量。

上一篇 下一篇

猜你喜欢

热点阅读