2

我在获取 android 布局的正确“ScreenShot”时遇到问题。布局包含 EditTexts 和 TextViews。

下面的代码只给出了布局的“ScreenShot”,但是当我在 EditText 或 TextView 中更改字符串时,“ScreenShot”没有更新!

如何确保“ScreenShot”可以更新并与屏幕上显示的相同?

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

1

解决了,我不应该使用

布局充气机

LayoutInflater 只是将一个布局 XML 文件实例化到其对应的 android.view.View 对象中,而不是当前图形 见这里

只需使用 findViewbyId 获取 View Object 然后

view.setDrawingCacheEnabled(true);
view.buildDrawingCache(true);
Bitmap drawingCache = view.getDrawingCache();

这样可行!

于 2017-09-02T07:05:15.637 回答