2
我有一個ImageView及其可見性設置爲GONE。我正試圖設置一個位圖資源並使其顯示。設置查看可見性以隱形並獲取尺寸
我實現的ImageView的尺寸(我需要適當地進行二次採樣我的位圖)爲零時,它的能見度仍然GONE,所以我設置這行代碼我BitmapAsyncTask運行之前。
ImageView postImageView= (ImageView) getActivity().findViewById(R.id.post_image);
// Set it as visible to take up necessary space for bitmap computation
postImageView.setVisibility(View.INVISIBLE);
尺寸還是回到零,並在進一步的測試中,ImageView的需要一些時間之前的知名度再次設置爲不可見。我現在修復的是AsyncTask內部的一個while循環,等待這些維度可用,但是我想知道是否有更好的方法來做到這一點?
我對當前的AsyncTask代碼:
@Override
protected Bitmap doInBackground(Void... voids) {
Log.i(TAG,"BitmapWorkerTask initialized.");
while(mImageView.getWidth() ==0){
// Wait for the ImageView to be ready
Log.i(TAG,"asd");
}
int reqWidth = mImageView.getWidth()==0?1920:mImageView.getWidth();
int reqHeight = mImageView.getHeight()==0?1080:mImageView.getHeight();
Log.i(TAG, "Dimensions (required): "+reqWidth+" X "+ reqHeight);
//Decode and scale image
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(mCurrentPhotoPath, options);
Log.i(TAG, "Dimensions (source): "+options.outWidth+" X "+ options.outHeight);
options.inSampleSize = calculateInSampleSize(options,reqWidth, reqHeight);
options.inJustDecodeBounds = false;
Bitmap imageBitmap = BitmapFactory.decodeFile(mCurrentPhotoPath,options);
Log.i(TAG,"Dimensions (Bitmap): "+ imageBitmap.getWidth()+" X "+ imageBitmap.getHeight());
return imageBitmap;
}
謝謝!我把我的AsyncTask放在OnLayoutChange裏面,它完美的工作:) – daidaidai 2014-10-27 11:34:44
@daidaidai不客氣:) – Simas 2014-10-27 11:38:07