2

Androidでレイアウトの正しい「スクリーンショット」を取得する際に問題があります。レイアウトには、EditTexts と TextViews が含まれています。

次のコードは、レイアウトの「スクリーンショット」のみを提供しますが、EditText または TextView で文字列を変更しても、「スクリーンショット」は更新されません!

「スクリーンショット」が更新され、画面に表示されているものと同じであることを確認するにはどうすればよいですか?

private Bitmap getBitmap() {
    Bitmap bitmap = null;
    LayoutInflater factorys = LayoutInflater.from(this);
    final View textEntryView = factorys.inflate(R.layout.activity_main, null);
    View ll = textEntryView.findViewById(R.id.ll_layout);
    ll.setDrawingCacheEnabled(true);
    ll.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    ll.layout(0, 0, ll.getMeasuredWidth(), ll.getMeasuredHeight());
    ll.buildDrawingCache();
    bitmap = Bitmap.createBitmap(ll.getDrawingCache());
    //bitmap=getCacheBitmapFromView(ll);
    ll.setDrawingCacheEnabled(false);
    return bitmap;
}

ここにコード全体があります

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(mycl);
    }

    OnClickListener mycl = new OnClickListener() {

        @Override
        public void onClick(View v) {

            // TODO Auto-generated method stub
            Bitmap mybpic = null;
            mybpic = getBitmap();
            ImageView imageView = (ImageView) findViewById(R.id.imageView1);
            imageView.setImageBitmap(mybpic);
        }
    };

    private Bitmap getBitmap() {
        Bitmap bitmap = null;
        LayoutInflater factorys = LayoutInflater.from(this);
        final View textEntryView = factorys.inflate(R.layout.activity_main,
                null);
        View ll = textEntryView.findViewById(R.id.ll_layout);

        ll.destroyDrawingCache();
        ll.invalidate();
        ll.setDrawingCacheEnabled(true);
        ll.measure(View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
                .makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        ll.layout(0, 0, ll.getMeasuredWidth(), ll.getMeasuredHeight());
        ll.buildDrawingCache();

        bitmap = Bitmap.createBitmap(ll.getDrawingCache());
        // bitmap=getCacheBitmapFromView(ll);
        ll.setDrawingCacheEnabled(false);
        return bitmap;
    }
}
4

1 に答える 1