Android 安卓:拍摄一个布局的屏幕截图

Android 安卓:拍摄一个布局的屏幕截图,android,android-layout,android-linearlayout,android-screen,Android,Android Layout,Android Linearlayout,Android Screen,我正在尝试创建一个相框应用程序,因为我有一个相框,我从gallery向该相框添加了一个图像。该相框位于主版面中的一个版面中,我正在尝试为该版面进行屏幕拍摄 Layout.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/

我正在尝试创建一个相框应用程序,因为我有一个相框,我从gallery向该相框添加了一个图像。该相框位于主版面中的一个版面中,我正在尝试为该版面进行屏幕拍摄

Layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/root"
tools:context="com.ccs.photoframe.MainActivity"
android:orientation="vertical"
android:weightSum="1">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight=".1"
    android:orientation="vertical">
    <Button
        android:id="@+id/btn_select"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="Select Photo"/>

</LinearLayout>

<LinearLayout
    android:id="@+id/layout_frame"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight=".8"
    android:background="@drawable/photo_frame"
    android:orientation="vertical"
    >
    <ImageView
        android:id="@+id/img_save_photo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"
       />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:layout_marginBottom="70dp"
        >
    <ImageView
        android:id="@+id/ivImage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="matrix"/>

    </FrameLayout>
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight=".1"
    android:orientation="horizontal">
    <Button
        android:id="@+id/btn_save_photo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Save Photo"/>
</LinearLayout>
但这是整个布局的屏幕截图

是否有任何可能的方法拍摄特定位置的屏幕截图


提前感谢:)

是的,您可以获得任何类似这样的视图的屏幕截图

    ImageView redoo_btn = (ImageView) findViewById(R.id.redoo_btn);
    FrameLayout paintLayout = (FrameLayout) findViewById(R.id.drawing);
    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.drawing1);

    Bitmap bm = Takeshot(linearLayout);
    Bitmap bm = Takeshot(redoo_btn);
    Bitmap bm = Takeshot(paintLayout);

    Bitmap Takeshot(View view) {
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache(true);
        Bitmap b = Bitmap.createBitmap(view.getDrawingCache());
        view.setDrawingCacheEnabled(false); // clear drawing cache
//        view.setImageBitmap(b);
        return b;
    }

是的,您可以这样做:

LinearLayout mlinearlaoytfrme=(LinearLayout)findViewById(R.id.layout_frame);
Bitmap mBItmap=takeScreenshotReceipt();
img_save_photo = (ImageView) findViewById(R.id.img_save_photo);
img_save_photo.setVisibility(View.VISIBLE);
img_save_photo.setBackgroundDrawable(bitmapDrawable);

private Bitmap takeScreenshotReceipt()
{
    View v1 = getRootView().findViewById(R.id.layout_frame);
    v1.setDrawingCacheEnabled(true);
    return v1.getDrawingCache();
}
试试这个方法

public static Bitmap getBitmapFromView(View view) {
        Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(),
                view.getHeight(), Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(returnedBitmap);
        Drawable bgDrawable = view.getBackground();
        if (bgDrawable != null)
            bgDrawable.draw(canvas);
        else
            canvas.drawColor(Color.WHITE);
        view.draw(canvas);
        return returnedBitmap;
    }

如果只需要
layout\u frame
,请删除
getRootView()
调用。事实上,只需使用
layout\u frame
。您不需要查看v1。您所说的特定位置…特定视图是什么意思?对不起,它的特定视图是@SohailZahid@BinilSurendran看到我的回答了吗?这是根布局的屏幕截图@Harshal Kalavadiya
public static Bitmap getBitmapFromView(View view) {
        Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(),
                view.getHeight(), Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(returnedBitmap);
        Drawable bgDrawable = view.getBackground();
        if (bgDrawable != null)
            bgDrawable.draw(canvas);
        else
            canvas.drawColor(Color.WHITE);
        view.draw(canvas);
        return returnedBitmap;
    }