【问题标题】:How to take screenshot in ConstraintLayout?如何在 ConstraintLayout 中截屏?
【发布时间】:2023-04-04 18:09:02
【问题描述】:

我最近开始使用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>

现在我想截取进度条和它下面的几个元素的屏幕截图。我试图围绕它创建一个视图并截取它,但它不起作用。有什么办法可以截屏?

【问题讨论】:

  • 您能否详细说明一下,您想以编程方式截取屏幕截图吗?
  • 隐藏/删除所有不需要放在截图中的东西,然后拿走
  • @VladyslavMatviienko 没有别的办法??因为有很多元素需要删除。
  • 你无论如何都需要删除它们...
  • 这是在约束布局中截屏的唯一方法吗?

标签: android android-layout screenshot android-constraintlayout


【解决方案1】:

你可以使用

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"

【讨论】:

  • 这给出了一个空白视图屏幕截图,因为视图中没有任何内容。
  • 将此行写在您要隐藏的特定视图中,以及您需要为他们显示的特定视图中保持可见性对他们可见
  • 我认为你没有理解这个问题。在截取屏幕截图时,我们需要一个有助于截取屏幕截图的布局。而在ConstarintLayout中,如果我们只想截取布局中的少数元素,我们没有这样的布局。
【解决方案2】:

不需要隐藏/显示您的视图

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);

【讨论】:

  • 我知道如何截取屏幕截图,但是由于所有视图都在同一个父级中,因此要在约束布局中传递什么视图。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多