【问题标题】:Screenshot not similar to Display view屏幕截图与显示视图不同
【发布时间】:2020-06-18 23:36:11
【问题描述】:

我截取了布局的屏幕截图,但给出的图像与显示运行时视图不同。 我使用com.github.chrisbanes.photoview.PhotoView 库来缩放图像上的手势。

<LinearLayout
            android:id="@+id/ContentLinearLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/random"
            android:orientation="horizontal"
            android:weightSum="2">

            <androidx.cardview.widget.CardView
                android:id="@+id/ImageViewLeftCard"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:cardCornerRadius="20dp"
                android:layout_margin="1dp"
                android:layout_weight="1">
            <com.github.chrisbanes.photoview.PhotoView
                android:id="@+id/ImageViewLeft"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/blackcolor"/>
            </androidx.cardview.widget.CardView>

            <androidx.cardview.widget.CardView
                android:id="@+id/ImageViewRightCard"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:cardCornerRadius="20dp"
                android:layout_margin="1dp"
                android:layout_weight="1">
            <com.github.chrisbanes.photoview.PhotoView
                android:id="@+id/ImageViewRight"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/blackcolor" />
            </androidx.cardview.widget.CardView>

</LinearLayout>

我需要截图后的图像与显示视图相同。

我正在用这段代码截图。

private void screenshot(View v){
    v.setDrawingCacheEnabled(true);
    Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache());
    v.setDrawingCacheEnabled(false);
}

我面临的问题..简单地说,我需要截图后带有圆角的图像。

【问题讨论】:

    标签: java android image screenshot rounded-corners


    【解决方案1】:

    试试这个

    private Bitmap extractBitmap(ViewGroup iViewGroup) {
    
    int width = iViewGroup.getWidth();
    int height = iViewGroup.getWidth();
    
    Bitmap createBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    
    Canvas canvas = new Canvas(createBitmap);
    
    iViewGroup.draw(canvas);
    
    return createBitmap;
    
      }
    }
    

    【讨论】:

      【解决方案2】:

      创建可绘制资源

      round_shape.xml

      <?xml version="1.0" encoding="utf-8"?>
      <shape xmlns:android="http://schemas.android.com/apk/res/android" >
          <corners android:radius="20dp" />
      </shape>
      

      并将此可绘制对象设置为 com.github.chrisbanes.photoview.PhotoView

      的背景
      <LinearLayout
          android:id="@+id/ContentLinearLayout"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="@drawable/random"
          android:orientation="horizontal"
          android:weightSum="2">
      
          <androidx.cardview.widget.CardView
              android:id="@+id/ImageViewLeftCard"
              app:cardCornerRadius="20dp"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_margin="1dp"
              android:layout_weight="1">
      
              <com.github.chrisbanes.photoview.PhotoView
                  android:id="@+id/ImageViewLeft"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:background="@drawable/round_shape"
                  android:background="@color/blackcolor" />
          </androidx.cardview.widget.CardView>
      
          <androidx.cardview.widget.CardView
              android:id="@+id/ImageViewRightCard"
              app:cardCornerRadius="20dp"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_margin="1dp"
              android:layout_weight="1">
      
              <com.github.chrisbanes.photoview.PhotoView
                  android:id="@+id/ImageViewRight"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:background="@drawable/round_shape"
                  android:background="@color/blackcolor" />
          </androidx.cardview.widget.CardView>
      
      </LinearLayout>
      

      并使用与获取屏幕截图相同的功能。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多