【问题标题】:Interacting with mipmap image assets in Android Studio, displayed using ImageView widget与 Android Studio 中的 mipmap 图像资源交互,使用 ImageView 小部件显示
【发布时间】:2016-03-06 02:29:16
【问题描述】:

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

这似乎是我应该能够毫不费力地传递的信息类型,那里的所有帮助都只是说“导入 Drawable 文件夹”,但今年早些时候 Android Studio 停止将导入的图像资源放入 Drawable 文件夹,并开始将它们放入 mipmap 文件夹。只要我在 activity_mail.xml 设计视图中定义背景,我就可以将这些图像用作背景,但是我无法与代码中的 imageview 背景进行交互,因为所有方法都期望引用类型可绘制,但我的图像不会被解释为可绘制,因为我无法将它们放入该文件夹。 我错过了什么?

首先,我使用单独的资源 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++;

    }

  }
}

当一个 imageview 被触摸时,下面的方法被调用。我正在尝试从图像视图背景中获取描述,并使用我的 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 小部件,其引用存储在数组中?

是否存在不需要可绘制文件的背景设置选项?

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

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

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

【问题讨论】:

    标签: java android imageview png mipmaps


    【解决方案1】:

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 2014-01-01
    • 1970-01-01
    相关资源
    最近更新 更多