簡體   English   中英

布局后,getDrawingCache()返回null

[英]getDrawingCache() returns null after layout

請幫助我理解為什么從附加到圖像按鈕視圖的ontouchlistner內的圖像按鈕視圖(appcompatimagebutton)調用getDrawingCache()時返回null的原因。

主要是在嘗試解決這個問題時,我一直在指這個問題:

Android View.getDrawingCache返回null,僅返回null

這沒有解決我的問題。 我確定我正在忽略某些東西或沒有完全理解某些東西。 請幫我弄清楚它是什么。

筆記:

1)這似乎沒有什么不同的天氣,我使用ImageButton或AppCompatImageButton(但我使用的是appcompat,因為在實際項目中我需要它提供ImageButton不能提供的功能)。

2)我不明白為什么我需要調用視圖的度量和布局方法,因為當用戶觸發ontouch()時,布局已經繪制到屏幕上了,但我認為無論如何我都會把它包括在內大約似乎表明這是必需的..如果我把它們都取出,那是行不通的。

3)如果將setEnableDrawingCache和buildDrawingCache調用放置在具有AppCompatImageButton視圖初始化的onTouch或onCreate中,則似乎無關緊要。 無論哪種情況,我對圖像按鈕視圖的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一起調用所有OnClickListener (如果只想在單擊時生成位圖)。

但是,我不確定為什么要調用所有MeasureSpec東西...如果要通過setContentView(R.layout.activity_main);加載圖像setContentView(R.layout.activity_main); 那就不要為所有手動測量而煩惱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM