截取嵌套RelativeLayout

时间:2016-09-17 10:48:25

标签: android android-layout screenshot

我有这种形式的布局xml:

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

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/
    res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:background="#3fb399"
        tools:context="com.sparrowred.cardsender.customizeCard">

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/
        res/android"
            android:id="@+id/relativeViewPrint"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            xmlns:tools="http://schemas.android.com/tools"
            android:orientation="vertical"
            android:weightSum="1"
            android:layout_above="@+id/button_send"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/add_button">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="180dp"
                android:id="@+id/textView_wish_card"
                android:textAlignment="center"
                android:textSize="20sp"
                android:layout_weight="0.06"
                android:layout_alignParentBottom="true"
                android:layout_alignParentStart="true" />

            <TextView
                android:layout_width="149dp"
                android:layout_height="112dp"
                android:background="@drawable/image0"
                android:layout_gravity="center"
                android:alpha="0.4"
                android:id="@+id/textView_wish_cardBack"
                android:layout_weight="0.75"
                android:layout_alignTop="@+id/textView_wish_card"
                android:layout_alignParentStart="true"
                android:layout_alignParentBottom="true"
                android:layout_alignParentEnd="true" />

        </RelativeLayout>

    </RelativeLayout>

</LinearLayout>

这两个文字视图是重叠的。一个拥有一个图像(具有透明度),另一个拥有颜色和文本(这就是为什么我将它们放在一个RelativeLayout上并重叠)。我想获得这个RelativeLayout的截图。我使用这种方法,但它不起作用。有什么帮助吗?

private void ScreenShot2(){
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    RTLout.measure(View.MeasureSpec.makeMeasureSpec(0,
    size.x), View.MeasureSpec.makeMeasureSpec(0, size.y));
    RTLout.layout(0, 0, RTLout.getWidth(), RTLout.getHeight());
    RTLout.setDrawingCacheEnabled(true);
    RTLout.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
    Bitmap b = Bitmap.createBitmap(RTLout.getDrawingCache());
    RTLout.setDrawingCacheEnabled(false);
    if(b !=null) {
        try {
            String extr = Environment.getExternalStorageDirectory().
            getAbsolutePath().toString() + File.separator + "Pictures" 
            + File.separator + "Screenshots";
            File dir = new File(extr);
            if (!dir.exists())
                dir.mkdirs();
            String fileName =
            new SimpleDateFormat("yyyyMMddhhmm'_report.jpg'").
            format(new Date());
            File myPath = new File(dir, fileName);
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(myPath);
                b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                fos.flush();
                fos.close();
                MediaStore.Images.Media.insertImage(getContentResolver(),
                b, "Screen", "screen");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}

这个方法混合了我发现的一些代码...... 09-17 16:54:12.452:W / System(4926):ClassLoader引用未知路径:/data/app/com.sparrowred.cardsender-1/lib/arm64 09-17 16:54:12.670:W / art(4926):在Android 4.1之前,方法android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter,android.content.res。 ColorStateList,android.graphics.PorterDuff $ Mode)会错误地覆盖android.graphics.drawable.Drawable中的package-private方法 09-17 16:54:12.796:I / System(4926):core_booster,getBoosterConfig = false 09-17 16:54:12.869:I / System(4926):core_booster,getBoosterConfig = false 09-17 16:54:13.232:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:13.246:E / HAL(4926):load:id = gralloc!= hmi-&gt; id = gralloc 09-17 16:54:13.314:E / HAL(4926):load:id = gralloc!= hmi-&gt; id = gralloc 09-17 16:54:13.315:I / OpenGLRenderer(4926):初始化的EGL,版本1.4 09-17 16:54:13.322:W / OpenGLRenderer(4926):load:so = / system / lib64 / libhwuibp.so 09-17 16:54:13.322:W / OpenGLRenderer(4926):dlopen失败:library&#34; /system/lib64/libhwuibp.so"未找到 09-17 16:54:13.322:W / OpenGLRenderer(4926):初始化二进制程序缓存:加载失败 09-17 16:54:13.322:E / HAL(4926):load:id = gralloc!= hmi-&gt; id = gralloc 09-17 16:54:13.456:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:17.773:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:18.189:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:20.100:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:20.195:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:20.218:I / System(4926):core_booster,getBoosterConfig = false 09-17 16:54:20.286:I / System(4926):core_booster,getBoosterConfig = false 09-17 16:54:20.635:W / Settings(4926):设置airplane_mode_on已从android.provider.Settings.System移至android.provider.Settings.Global,返回只读值。 09-17 16:54:20.655:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:20.842:I / System(4926):core_booster,getBoosterConfig = false 09-17 16:54:20.906:I / System(4926):core_booster,getBoosterConfig = false 09-17 16:54:21.452:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:22.137:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:22.137:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:22.238:E / SpannableStringBuilder(4926):SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度 09-17 16:54:22.238:E / SpannableStringBuilder(4926):SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度 09-17 16:54:25.969:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:26.647:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:27.986:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:28.118:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:28.128:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:28.158:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:28.158:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:28.193:E / SpannableStringBuilder(4926):SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度 09-17 16:54:28.193:E / SpannableStringBuilder(4926):SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度 09-17 16:54:29.363:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:30.134:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:30.142:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:30.170:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:30.170:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:30.209:E / SpannableStringBuilder(4926):SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度 09-17 16:54:30.209:E / SpannableStringBuilder(4926):SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度 09-17 16:54:30.814:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:31.279:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:32.477:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:32.587:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:32.596:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:32.627:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:32.627:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:32.656:E / SpannableStringBuilder(4926):SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度 09-17 16:54:32.656:E / SpannableStringBuilder(4926):SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度 09-17 16:54:33.375:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:33.756:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:34.710:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:34.836:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:34.853:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:34.883:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:34.884:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:34.912:E / SpannableStringBuilder(4926):SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度 09-17 16:54:34.912:E / SpannableStringBuilder(4926):SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度 09-17 16:54:35.525:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:35.626:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:35.632:E / SpannableStringBuilder(4926):SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度 09-17 16:54:35.632:E / SpannableStringBuilder(4926):SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度 09-17 16:54:36.980:E / SpannableStringBuilder(4926):SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度 09-17 16:54:36.980:E / SpannableStringBuilder(4926):SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度 09-17 16:54:37.201:E / SpannableStringBuilder(4926):SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度 09-17 16:54:37.201:E / SpannableStringBuilder(4926):SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度 09-17 16:54:37.419:E / SpannableStringBuilder(4926):SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度 09-17 16:54:37.419:E / SpannableStringBuilder(4926):SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度 09-17 16:54:39.064:E / SpannableStringBuilder(4926):SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度 09-17 16:54:39.064:E / SpannableStringBuilder(4926):SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度 09-17 16:54:39.835:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:41.707:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:45.142:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:46.0​​84:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:47.246:I / HwSecImmHelper(4926):mSecurityInputMethodService为null 09-17 16:54:47.395:I / art(4926):System.exit调用,状态:0 09-17 16:54:47.395:I / AndroidRuntime(4926):VM退出,结果代码为0,跳过清理。

希望它是正确的

1 个答案:

答案 0 :(得分:0)

buildDrawingCache将其添加到位图之前,您似乎没有在布局上调用getDrawingCache方法。

执行类似的操作(此代码没有您使用的其他选项):

RelativeLayout RTLout= (RelativeLayout )findViewById(R.id.relativeViewPrint);
RTLout.setDrawingCacheEnabled(true);
RTLout.buildDrawingCache();
Bitmap bm = view.getDrawingCache();