如何在ConstraintLayout中截屏?

时间:2019-05-16 11:59:52

标签: android android-layout screenshot android-constraintlayout

我最近开始使用ConstraintLayout,并喜欢它。但是遇到了要解决的问题。需要截取一部分布局的屏幕截图,并且相对或线性布局都很容易,因为我们围绕视图进行布局需要的屏幕截图,ConstraintLayout中不是这种情况。

这是我的观点:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">

    <View
        android:id="@+id/sharable_container"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="@+id/tv_streak_best"
        app:layout_constraintLeft_toLeftOf="@id/total_distance_steps_left_guideline"
        app:layout_constraintRight_toRightOf="@id/total_distance_steps_right_guideline"
        app:layout_constraintTop_toTopOf="parent" />

    <ProgressBar
        android:id="@+id/streak_progress"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:max="360"
        android:progress="0"
        android:progressDrawable="@drawable/progress_gradient_bg"
        android:secondaryProgress="360"
        app:layout_constraintBottom_toBottomOf="@id/streak_bottom_guideline"
        app:layout_constraintDimensionRatio="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.sharesmile.share.views.LBTextView
        android:id="@+id/tv_streak_count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:textColor="@color/clr_5d"
        android:textSize="58sp"
        app:layout_constraintBottom_toBottomOf="@id/streak_progress"
        app:layout_constraintLeft_toLeftOf="@id/streak_progress"
        app:layout_constraintRight_toRightOf="@id/streak_progress"
        app:layout_constraintTop_toTopOf="@id/streak_progress"
        app:layout_constraintVertical_bias="0.35" />

    <com.sharesmile.share.views.LBITextView
        android:id="@+id/tv_streak_count_days"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:text="@string/day_streak"
        android:textColor="@color/clr_5d"
        android:textSize="20sp"
        app:layout_constraintLeft_toLeftOf="@id/streak_progress"
        app:layout_constraintRight_toRightOf="@id/streak_progress"
        app:layout_constraintTop_toBottomOf="@+id/tv_streak_count" />

    <LinearLayout
        android:id="@+id/streak_goal_icons_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="3dp"
        android:orientation="horizontal"
        app:layout_constraintLeft_toLeftOf="@id/tv_streak_count_days"
        app:layout_constraintRight_toRightOf="@id/tv_streak_count_days"
        app:layout_constraintTop_toBottomOf="@id/tv_streak_count_days">


    </LinearLayout>
... more code here
</android.support.constraint.ConstraintLayout>

现在,我希望对进度条及其下方的几个元素进行截屏。我试图围绕它创建一个视图并对其进行截图,但是它不起作用。可以怎样截取屏幕截图?

2 个答案:

答案 0 :(得分:0)

您可以使用

tools:visibility="gone"

隐藏如下视图

<View
    android:id="@+id/sharable_container"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="@+id/tv_streak_best"
    app:layout_constraintLeft_toLeftOf="@id/total_distance_steps_left_guideline"
    app:layout_constraintRight_toRightOf="@id/total_distance_steps_right_guideline"
    app:layout_constraintTop_toTopOf="parent"
    tools:visibility="gone" />

在根视图下添加为以下属性

xmlns:tools="http://schemas.android.com/tools"

答案 1 :(得分:0)

不需要隐藏/显示视图

private void captureScreen(View v) {
    v.setDrawingCacheEnabled(true);
    Bitmap bitmap = v.getDrawingCache();
    String extr = Environment.getExternalStorageDirectory().toString();
    File file = new File(extr, "fff" + ".jpg");
    FileOutputStream f = null;
    try {
        f = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, f);
        f.flush();
        f.close();
        MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "Screen", "screen");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

将您的view传递给上述功能

captureScreen(view you want to take a screen shot);