简体   繁体   English

ImageView在将可见性从变为可见后不显示图像

[英]ImageView Not showing image after change visibility from gone to visible

I have this 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>

And I want to make it visible after user selects an image form gallery: 我希望在用户选择图像表单库后使其可见:

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;
    }

}

Code for scaleBitmap function: 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;
}

The problem is that the ImageView displays a color square, not the picture. 问题是ImageView显示的是颜色方块,而不是图片。 The color depends on which image is chosen. 颜色取决于选择的图像。 So maybe something is wrong with the rendering but my knowledge about this is very small. 所以渲染渲染可能有问题,但我对此的了解非常少。

The problem dissapears if I set visibility to invisible instead of gone. 如果我将可见性设置为不可见而不是消失,问题就会消失。

Hope someone can help. 希望有人能提供帮助。 Thanks 谢谢

Could it be that view.getHeight() is 0 because of it being GONE ? 可能是view.getHeight()是0,因为它是GONE So scaleBitmap(getPathFromUri(imageUri), view.getHeight()) might be scaling it to 1 pixel? 因此scaleBitmap(getPathFromUri(imageUri), view.getHeight())可能会将其缩放到1像素?

你也可以显示默认图像,而不是让视图不可见

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Android Studio中将GONE可见性更改为VISIBLE可见性 - how to change a GONE visibility to VISIBLE visibility in android studio 如何从 View.gone 恢复视图。 在 xml 中使用 'android:visibility="gone"' 后 setVisibility(View.VISIBLE) 不工作 - How to recover view from View.gone. setVisibility(View.VISIBLE) not working after using 'android:visibility="gone"' in xml 从GONE到VISIBLE的Android可见性不适用于第一次 - Android Visibility from GONE to VISIBLE doesn't work first time 从RecyclerView中的某些行更改ImageView可见性 - Change ImageView visibility from certain rows in RecyclerView 如何设置可见性从另一个类到属于活动的textview和imageview? - How to set visibility gone from another class to textview and imageview that belong to activity? 为默认的recyclerView创建展开动画(将视图的可见性从变为可见) - Create expand animation for default recyclerView (changing view's visibility from gone to visible) 捕获后在ImageView中显示图像(Android Java) - Showing image in ImageView after capturing (android java) 选择图像后,ImageView没有显示圆形 - Imageview not showing circular after image is selected ImageView不显示从图库中选择的图像 - ImageView not showing image selected from gallery 方向更改后,ImageView背景不显示图像 - ImageView background not displaying image after orientation change
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM