捕获android上的当前活动屏幕

时间:2016-01-31 16:01:23

标签: android android-canvas screenshot android-maps-v2 android-bitmap

我正在尝试捕获当前活动的屏幕,以便我可以使用android的共享表来共享图像。我的活动包含一个地图片段和TextViews。我试过的所有内容都会抓取a grid的屏幕截图  (在画廊中显示为黑色)。我应该如何捕捉整个屏幕?这是我使用的XML文件和java代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="15"
android:gravity="bottom"
android:id="@+id/rootview"
android:background="#e7ebed">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/under_root"
    android:layout_weight="1">

    <fragment
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map2"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="330dp"
    tools:context="com.github.pires.obd.reader.activity.TripReport" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="227dp"
        android:layout_weight="1">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="0.66">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Average speed"
                android:id="@+id/avgspeed"
                android:layout_weight="1"
                android:textColor="#000000" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Engine Runtime"
                android:id="@+id/runtime"
                android:layout_weight="1"
                android:textColor="#000000" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.66">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Distance"
                android:id="@+id/dist"
                android:layout_weight="1"
                android:textColor="#000000" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Idle Time"
                android:id="@+id/idletime"
                android:layout_weight="1"
                android:textColor="#000000" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.66">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:id="@+id/totaltime"
                android:layout_weight="1" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageView2"
                android:layout_weight="1" />
        </LinearLayout>

    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="bottom">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Share"
        android:id="@+id/sharebutton"
        android:layout_weight="1"
        android:background="#7986cb"
        android:textColor="#ffffff"
        android:onClick="shareAction" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Quit"
        android:id="@+id/butt_quit"
        android:layout_weight="1"
        android:textColor="#ffffff"
        android:background="#e53935"
        android:onClick="exitApp" />
</LinearLayout>
</LinearLayout>

以下是代码:

       public void takeScreenShot()
{
    View v1 = ll;
    Bitmap bm = captureScreen(v1);
    String pat="";
    try {
        pat = saveImage(bm);
    }
    catch(IOException e)
    {

    }
    Uri temp = Uri.parse(pat);
    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
    sharingIntent.setType("image/jpeg");
    sharingIntent.putExtra(Intent.EXTRA_STREAM, temp);
    startActivity(Intent.createChooser(sharingIntent, "Share image using"));
}
public static Bitmap captureScreen(View v) {

    Bitmap screenshot = null;
    try {

        if(v!=null) {

              screenshot=Bitmap.createBitmap(v.getMeasuredWidth(),v.getMeasuredHeight(),    Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(screenshot);
            v.draw(canvas);
        }

    }catch (Exception e){

     }

    return screenshot;
}

public static String saveImage(Bitmap bitmap) throws IOException {

    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 40, bytes);
    File f = new File(Environment.getExternalStorageDirectory() + File.separator + "test.png");
    String path  = f.getAbsolutePath();
    f.createNewFile();
    FileOutputStream fo = new FileOutputStream(f);
    fo.write(bytes.toByteArray());
    fo.close();
    return path;
}

0 个答案:

没有答案