1

私は多くの方法を試しました。画面全体のスクリーンショットを撮ることはできますが、Horizo​​ntalScrollView全体のスクリーンショットを撮り、それを画像ファイルとして保存する必要があります。これが私が使用しているサンプルXMLレイアウトです:

<RelativeLayout 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" >

    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/hsv" >

        <LinearLayout 
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:orientation="horizontal"
            android:id="@+id/ll">

             <Button 
                android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:onClick="click"
            android:id="@+id/b"
            android:text="capture"
            android:layout_margin="20dp"/> 

            <ImageView 
                android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:id="@+id/img1"
            android:src="@drawable/plug1"
            android:layout_margin="20dp"/>

            <ImageView 
                android:layout_height="wrap_content"
            android:layout_width="200dp"
            android:id="@+id/img2"
            android:src="@drawable/plug3"
            android:layout_margin="20dp"/>

        </LinearLayout>

    </HorizontalScrollView>

</RelativeLayout>

アクティビティコード:

Button b; 
    ImageView img1, img2; 
    LinearLayout ll; View v; 
    HorizontalScrollView s; 
    int h,w;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        b = (Button)findViewById(R.id.b);
        img1 = (ImageView)findViewById(R.id.img1);
        img2 = (ImageView)findViewById(R.id.img2);

        ll = (LinearLayout)findViewById(R.id.ll);

        s = (HorizontalScrollView) findViewById(R.id.hsv);
        s.setDrawingCacheEnabled(true);

    }

     //Fired onClick of the button
       public void click(View v) {
           shoot();
       }

    // To capture layout, create a bitmap and replace an image with the resulting bitmap.
    public void shoot() {

        s.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);

        int totalHeight = s.getMeasuredHeight();
        int totalWidth = s.getMeasuredWidth();

        s.layout(0, 0, totalHeight, totalWidth);    
        s.buildDrawingCache(true);

        Bitmap b = Bitmap.createBitmap(s.getDrawingCache());             
        s.setDrawingCacheEnabled(false);

        img1.setImageBitmap(b);     
    }

MeasureSpecを使用しないと、正しいレイアウトの高さと幅が得られません。

ボタンのOnClickは、半分のボタンの画像のみを取得しています。私はViewTreeObserverを使用していないので、これが起こっていると思います。しかし、ViewTreeObserverを使用すると、結果が得られません。また、ボタンを2回クリックするとアプリケーションがクラッシュします-ビットマップのNullPointerException。

StackOverFlowで利用できるほぼすべてのものを試しました。これを行う方法を見つけるのを手伝ってください。それも可能ですか?

4

2 に答える 2

1

それを行う簡単な方法を見つけました。

XML

        <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:layout_gravity="center_vertical">

     <HorizontalScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            >

         <LinearLayout 
                  android:layout_width="wrap_content"
                  android:layout_height="fill_parent"
                  android:orientation="horizontal"
                  android:id="@+id/def"
                  >

    <Button 
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="capture"
        android:onClick="click"/>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <ImageView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/black"
        android:id="@+id/img"/>

    <ImageView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/a"/>

    </LinearLayout>

    </HorizontalScrollView>

</LinearLayout>

活動コード:

public class CaptureBeyondActivity extends Activity {
/** Called when the activity is first created. */

LinearLayout def;
ImageView img;
HorizontalScrollView hsv;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

def = (LinearLayout)findViewById(R.id.def);
img = (ImageView)findViewById(R.id.img);

}

public void click(View v) {
Bitmap bitmap;
def.setDrawingCacheEnabled(true);
def.buildDrawingCache(true);

        bitmap = Bitmap.createBitmap(def.getWidth(), def.getHeight(),
                Config.ARGB_8888);
        def.layout(0, 0, def.getLayoutParams().width,
                def.getLayoutParams().height);
        Canvas c = new Canvas(bitmap);
        def.draw(c);

def.setDrawingCacheEnabled(false);

if(bitmap!=null){
    img.setImageBitmap(bitmap);
    img.invalidate();
}
}}

ここでは、Horizo​​ntalScrollViewにいくつかの要素を配置しました。ボタンをクリックするとスクリーンショットが撮られ、2番目の画像が撮られたスクリーンショットに置き換えられます。オフスクリーン要素のスクリーンショットも撮ります。コードをさらに拡張して、最終的なビットマップ画像をSDカードに保存できます。

于 2013-07-23T05:30:29.870 に答える
0

これと同じ質問を見るのは2回目です(はい、ここではスタックオーバーフローです)。簡単な答えは次のとおりです。簡単な方法はありません。

スクリーンショットを保存するすべての方法は、ビットマップキャッシュへの描画を有効にし、このビットマップを取得するという同じ原則を使用します。ただし、ビューが画面に表示されていない場合、ビューは描画されません。

水平画面ビューを自動スクロールし、いくつかのスクリーンショットを撮り、さまざまなビットマップをつなぎ合わせる非常に複雑なシステムを作成できます。または、無限の幅のキャンバスを作成し、その上に描画するようにレイアウトに要求する非常に複雑なシステムかもしれませんが、この状況ではメモリが不足する可能性があります。しかし、私が言ったように、それらのどれも簡単な作業ではありません。

于 2013-01-09T11:44:54.407 に答える