【问题标题】:ImageView Not showing image after change visibility from gone to visibleImageView 在将可见性从消失更改为可见后不显示图像
【发布时间】:2013-07-31 20:23:38
【问题描述】:

我有这个 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 显示的是彩色方块,而不是图片。颜色取决于选择的图像。因此,渲染可能有问题,但我对此的了解非常少。

如果我将可见性设置为不可见而不是消失,问题就会消失。

希望有人可以提供帮助。谢谢

【问题讨论】:

  • 你可以移动“view.setVisibility(View.VISIBLE);”在调用 Galley 的 onclick 方法中。
  • scaleBitmap() 函数背后的代码是什么?
  • 编辑并添加了 scaleBitmap() 的代码。谢谢g00dy

标签: java android imageview visibility


【解决方案1】:

难道view.getHeight() 是 0 因为它是 GONE?那么scaleBitmap(getPathFromUri(imageUri), view.getHeight()) 可能会将其缩放到 1 个像素?

【讨论】:

  • 这就是问题所在。只是为了测试,我发送了 400 而不是 view.getHeight() 并工作了。非常感谢!
【解决方案2】:

你还可以显示默认图像而不是使视图不可见

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-05
    • 1970-01-01
    • 2019-08-17
    • 2017-10-24
    • 1970-01-01
    • 2014-04-04
    • 2014-08-15
    • 1970-01-01
    相关资源
    最近更新 更多