Android getDrawingCache()在布局后返回null

Android getDrawingCache()在布局后返回null,android,Android,请帮助我理解为什么从连接到图像按钮视图的ontouchlistner中调用我的图像按钮视图(appcompatimagebutton)时getDrawingCache()返回null 主要地,在试图解决这个问题时,我指的是这个问题: 这并没有解决我的问题。我确信我忽略了一些事情或者没有完全理解一些事情。请帮我弄清楚是什么 注: 1) 我使用ImageButton还是AppCompatImageButton似乎没有什么区别(但我使用appcompat是因为在实际项目中,我需要它提供的ImageB

请帮助我理解为什么从连接到图像按钮视图的ontouchlistner中调用我的图像按钮视图(appcompatimagebutton)时getDrawingCache()返回null

主要地,在试图解决这个问题时,我指的是这个问题:

这并没有解决我的问题。我确信我忽略了一些事情或者没有完全理解一些事情。请帮我弄清楚是什么

注:

1) 我使用ImageButton还是AppCompatImageButton似乎没有什么区别(但我使用appcompat是因为在实际项目中,我需要它提供的ImageButton没有的功能)

2) 我不明白为什么我需要调用视图的measure和layout方法,因为当用户启动ontouch()时,布局已经绘制到屏幕上,但我认为无论如何我都会包含它,因为我找到的许多答案似乎表明这是必需的。。如果我把它们拿出来也不行

3) 我是否将setEnableDrawingCache和buildDrawingCache调用放置在onTouch中或使用AppCompatImageButton视图初始化的onCreate中似乎无关紧要。在任何一种情况下,对image button视图的getDrawingCache的ontouchlistener调用都返回null


这是代码示例

谢谢

public class MainActivity extends AppCompatActivity {
    AppCompatImageButton mImageButton;
    Bitmap mBitmapDrawingCache;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //create image button
        mImageButton = new AppCompatImageButton(this);
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(270, 270);
        mImageButton.setLayoutParams(layoutParams);

        //getDrawingCache() in ontouchlistener returns null regardless if this is set
        int mseWidth = View.MeasureSpec.makeMeasureSpec(270, View.MeasureSpec.EXACTLY);
        int mseHeight = View.MeasureSpec.makeMeasureSpec(270, View.MeasureSpec.EXACTLY);
        mImageButton.measure(mseWidth, mseHeight);
        mImageButton.layout(0, 0, 270, 270);
        /* this does not work either
        int mseWidth = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int mseHeight = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        mImageButton.measure(mseWidth, mseHeight);
        mImageButton.layout(0, 0, mImageButton.getMeasuredWidth(), mImageButton.getMeasuredHeight());
        */

        //getDrawingCache() in ontouchlistner returns null regardless if either, both or none of these are set
        //mImageButton.buildDrawingCache();
        mImageButton.setDrawingCacheEnabled(true);

        //attach image button view to content view
        ((FrameLayout) findViewById(android.R.id.content)).addView(mImageButton);

        //set touch listener
        mImageButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                //why is this line returning null instead of a Bitmap ?
                mBitmapDrawingCache = Bitmap.createBitmap(v.getDrawingCache());

                Log.d("TAG_A", mBitmapDrawingCache.toString());

                return false;
            }
        });
    }
}

为了从视图中取出位图,生成缓存的顺序如下:

mImageButton.setDrawingCacheEnabled(true);
mImageButton.buildDrawingCache();
mBitmapDrawingCache = Bitmap.createBitmap(mImageButton.getDrawingCache());
您需要在
OnTouchListener
中一起调用此函数,或者更可取的方法是
OnClickListener
(如果您只想在单击时生成位图)


然而,我不知道你为什么要把所有的度量都称为。。。如果您是通过
setContentView(R.layout.activity_main)加载图像那么,不要为所有手动测量而烦恼。

感谢您的回复,将这些调用放在onClick中对我的设计并不可取。在我测试onTouch时,将这些调用放在onTouch中对我不起作用。如果其他人在测试它时,这对他们有用,我想知道我测试它的硬件是否有什么特殊之处?这是否可能在某些设备上有效,而在其他设备上无效?