2013-07-31 54 views
0

我有這樣的ImageView:ImageView的不是出現了變化知名度後的圖像從去可見

<ScrollView 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@android:color/black" 
android:id="@+id/formSuggestionScroll"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    tools:context=".SuggestQuestionActivity" 
    android:orientation="vertical" 
    android:scrollbars="vertical" 
    android:background="@android:color/black"> 

    <Button 
    android:id="@+id/btnOpenGallery" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:text="@string/suggest_btnOpenGallery" 
    android:onClick="btnOpenGallery_click" 
    android:textColor="@android:color/white" 
    android:paddingTop="@dimen/buttons_paddingTop"/> 

    <ImageView 
     android:id="@+id/selectedPicContainer" 
     android:layout_width="@dimen/imagen_dimen_width" 
     android:layout_height="@dimen/imagen_dimen_height" 
     android:layout_marginTop="@dimen/image_padding" 
     android:adjustViewBounds="true" 
     android:layout_gravity="center_horizontal" 
     android:visibility="gone"/> 

    <Button 
    android:id="@+id/btnSend" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:text="@string/suggest_send" 
    android:onClick="btnSend_click" 
    android:textColor="@android:color/white" 
    android:paddingTop="@dimen/buttons_paddingTop"/> 

</LinearLayout> 

我希望用戶選擇一個圖像格式庫後使其可見:

public void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    switch(requestCode){ 

     case SELECT_PHOTO: 
      if(resultCode == RESULT_OK){ 

       ImageView view = (ImageView) findViewById(R.id.selectedPicContainer); 


       imageUri = data.getData(); 
       Bitmap galleryPic = scaleBitmap(getPathFromUri(imageUri), view.getHeight()); 

       if(galleryPic != null){ 
        view.setImageBitmap(galleryPic); 
        view.setVisibility(View.VISIBLE); 
       }else{ 
        Toast toast = Toast.makeText(this, "selection failed", Toast.LENGTH_LONG); 
        toast.show(); 
       } 
      } 
      break; 
    } 

} 

scaleBitmap函數代碼:

private Bitmap scaleBitmap(String imagePath, int maxDimension) { 

    Bitmap scaledBitmap; 

    BitmapFactory.Options op = new BitmapFactory.Options(); 

    op.inJustDecodeBounds = true; 
    scaledBitmap = BitmapFactory.decodeFile(imagePath,op); 

    if(maxDimension < op.outHeight || maxDimension < op.outWidth){ 
     op.inSampleSize = Math.round(Math.max((float) op.outHeight/(float) maxDimension, 
               (float)op.outWidth/(float) maxDimension)); 
    } 

    op.inJustDecodeBounds = false; 
    scaledBitmap = BitmapFactory.decodeFile(imagePath,op); 

    return scaledBitmap; 
} 

問題是ImageView顯示顏色方塊,而不是圖片。顏色取決於選擇哪個圖像。所以,渲染可能有些問題,但我對此的瞭解很少。

如果將可見性設置爲不可見而不是消失,問題消失。

希望有人能幫忙。謝謝

+0

可以移動「view.setVisibility(View.VISIBLE);」在您稱之爲Galley的onclick方法中。 –

+0

'scaleBitmap()'函數背後的代碼是什麼? – g00dy

+0

爲scaleBitmap()編輯並添加了代碼。謝謝g00dy – xzdead

回答

2

難道view.getHeight()是0因爲它是GONE?那麼scaleBitmap(getPathFromUri(imageUri), view.getHeight())可能會將其縮放到1像素?

+0

這就是問題所在。要測試,我發送了400而不是view.getHeight()並工作。非常感謝! – xzdead

0

ü也可以顯示,而不是使所述視圖不可見的默認圖像

相關問題