在 Android Studio 中与 mipmap 图像资源交互,使用 ImageView 小部件显示

Interacting with mipmap image assets in Android Studio, displayed using ImageView widget

我正在尝试做一些应该很简单的事情,导入 png 文件,在许多 imageview 小部件(作为背景)中使用它们。作为我的代码的一部分,我希望看到当点击小部件 a 时,小部件 b 会更新为小部件 a 的背景。

似乎我应该可以毫不费力地传递这种信息,所有的帮助都只是说 "import into Drawable folder",但今年早些时候 Android 工作室停止导入图像assets 放入 drawable 文件夹,并开始将它们放入 mipmap 文件夹。只要我在 activity_mail.xml 设计视图中定义背景,我就可以将这些图像用作背景,但我无法在我的代码中与图像视图背景进行交互,因为所有方法都需要引用键入可绘制对象,但我的图像未被解释为可绘制对象,因为我无法将它们放入该文件夹中。 我错过了什么?

首先,我创建了一个包含单独资源 ID 的数组,希望我可以在我的目标图像视图中使用它们。 我不确定用 mipmap 图像资源填充数组是否有效,因为我似乎无法处理任何在我的代码中不是可绘制的图像,但它适用于我代码中其他地方的音频文件。我已将描述放入标签字段中,该字段也可用于引用所用图像,但我也无法正常工作。

public class MainActivity extends AppCompatActivity {
    int[] letters = new int[26];

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    int i = 0;

    for (char alphabet = 'a'; alphabet <= 'z'; alphabet++) {
        String ltr = String.valueOf(alphabet);
        ltr = "letter"+ltr;
        letters[i] = this.getResources().getIdentifier(ltr,"mipmap",this.getPackageName());

        i++;

    }

  }
}

触摸图像视图时调用以下方法。我正在尝试从图像视图背景中获取描述,并使用我的 mipmap 文件夹中的 png 文件更新另一个图像视图的背景,我(理论上)将其引用存储在数组中。

public void onClickLetter(View v) {
    String actualLetter;
    int letternumber;


    String letterclicked = v.getContentDescription().toString();
    letterclicked = letterclicked.replace("letter", "");  //this gives me a number, which I parse to an int below, which allows me to select a resource ID stored in my array, this is working perfectly elsewhere in my code


    System.out.println(letterclicked);

    actualLetter = (v.getTag()).toString();
    System.out.println(actualLetter);

    letternumber = parseInt(letterclicked); //I should be able to use the letters[letternumber] array reference now, but I can't seem to pass it to anything since it's not a drawable file.

如何在源 imageview 小部件(此方法中的 View v 参数)中传递对图像文件的引用,并使用图像文件作为背景更新第二个 imageview 小部件,其引用存储在大批?

是否有不期望可绘制文件的 setbackground 选项?

如果我应该使用 drawable 文件夹,请告诉我如何将 png 文件添加到 drawable 文件夹。

我的问题是因为使用 imageview 而不是按钮,还是其他类型的小部件?

我的问题是我存储 mipmap 资源 ID 的方式(这是图像资产由 android studio 自动放置的地方)吗?我知道 mipmap 文件仅用于图标,但正如我今年早些时候提到的那样 android 工作室开始将所有图像放入 mipmap 文件夹中。

这里有解决问题的答案。将视图切换到项目,您可以使用 drawable 文件夹。 Are all images in Android apps considered Icons?