【问题标题】:Swapping Image in ImageView在 ImageView 中交换图像
【发布时间】:2012-05-15 19:52:28
【问题描述】:

我一直在尝试在我的应用中实现简单(我认为)的东西。我认为这很简单,但不知何故,它并没有像我想要的那样工作。

我想做的是:

  • 有一个以 ImageView 为中心的活动 - 它有自己的图像(嗯,有两个)。
  • 我想按下那个 ImageView(或者它可能是 ImageButton),打开图库/用相机拍照。
  • 然后,在拍摄/挑选图片后,我想让 ImageView 显示图片的缩略图。

我的布局

                <ImageView
                    android:id="@+id/add_photo_left"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="20dp"
                    android:layout_weight="1"
                    android:adjustViewBounds="true"
                    android:src="@drawable/add_photo_button" />

                <ImageView
                    android:id="@+id/add_photo_right"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:adjustViewBounds="true"
                    android:src="@drawable/add_photo_button" />
            </LinearLayout>

我的代码——获取图片路径后

        BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
        bmpFactoryOptions.inSampleSize = 8;
        Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/5.png",bmpFactoryOptions);
        lastClicked.setImageBitmap(bitmap);
        //lastClicked.setImageResource(R.drawable.green_circle);

我想做的是从 sdcard 中读取调整大小的文件(使用 inSampleSize),然后用它填充 ImageView(或 ImageButton)。 但是使用 setImageBitmap 函数后,视图消失了 - 就像它刚刚加载了空图像(注释行正常工作 - 它只是绿点)。

也许有人已经解决了类似的问题?我将非常感谢任何形式的帮助。

问候, 扫一扫

编辑: ofc lastClicked:

lastClicked = (ImageView)findViewById(R.id.add_photo_left);

【问题讨论】:

    标签: android image


    【解决方案1】:

    你为什么不试试lastClicked.invalidate()

    我至少在我的代码中这样做了,而且它是这样工作的;)

    【讨论】:

    • 哇!这是我在 stackoverflow.com 上第一个接受的答案:D 了解何时需要 invalidate() 但我昨天没有找到一个好的答案会很有趣。
    猜你喜欢
    • 1970-01-01
    • 2014-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-18
    • 2013-08-06
    • 1970-01-01
    相关资源
    最近更新 更多