【问题标题】:Transfer image from one ImageButton to another将图像从一个 ImageButton 传输到另一个
【发布时间】:2017-12-02 03:19:06
【问题描述】:

我有两个 ImageButton,每个都有不同的图像。我需要找到基本上允许 ImageButton2 显示 ImageButton1 的图像的 Java 代码。我一直在胡思乱想,但我认为魔法命令应该是这样的:

ImageButton2.setImageResource( ImageButton1.getImageAlpha() );

ImageButton2.setImageResource( ((BitmapDrawable) ImageButton1.getDrawable()).getBitmap() );

int id = ImageButton1.getId();
ImageButton2.setImageResource( R.drawable.(id) );

但是这些都不能编译。答案必须是这样的:

ImageButton2.setImageResource( ImageButton1.getImageResource() );

有人看到解决方案吗?我整天都在做这件事。谢谢。

【问题讨论】:

    标签: java android android-imageview android-view imagebutton


    【解决方案1】:

    可以通过以下方式实现:

    Drawable drawable = imageButton1.getDrawable(); Drawable mutatedDrawable = drawable.mutate(); imageButton2.setImageDrawable(mutatedDrawable);

    默认情况下,drawable 将被共享。如果您希望对一个可绘制对象所做的更改不影响附加到另一个视图的同一个可绘制对象,那么您必须显式地mutate() 可绘制对象。

    【讨论】:

      猜你喜欢
      • 2014-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-20
      • 1970-01-01
      • 1970-01-01
      • 2022-07-21
      相关资源
      最近更新 更多