【问题标题】:Update the DrawingCache of a specific layout in Android更新Android中特定布局的DrawingCache
【发布时间】:2017-09-02 04:43:56
【问题描述】:

我在获取 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;
    }
}

【问题讨论】:

  • 尝试添加 ll.destroyDrawingCache();之前 ll.setDrawingCacheEnabled(true);
  • 我都试过了 ll.destroyDrawingCache(); ll.invalidate();它对我不起作用@DivyeshPatel
  • 尝试使edittext和textview失效
  • 已解决,我不应该使用 > LayoutInflater @DivyeshPatel

标签: android image android-layout android-edittext drawingcache


【解决方案1】:

已解决,我不应该使用

布局充气器

LayoutInflater 只是将布局 XML 文件实例化为其对应的 android.view.View 对象,而不是当前图形 see here

只需使用 findViewbyId 即可获取视图对象 那么

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

行得通!

【讨论】:

    猜你喜欢
    • 2014-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 2018-07-21
    • 2017-02-17
    • 1970-01-01
    相关资源
    最近更新 更多