如何从imageView获取可绘制名称 [英] How to get drawable name from imageView

查看:36
本文介绍了如何从imageView获取可绘制名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,可以在活动打开时创建一个随机图像.随机图像阵列中有大约 30 张图像.图像已生成,但我想要的是将可绘制名称写入文本视图.它看起来像这样:

I have an app that create a random image when the activity opens. There are like 30 images in the random image array. The image is generated, but what I want is to write the drawable name into a textview. It looks like this:

    ImageView RImage= (ImageView) findViewById(R.id.image);
    RImage.setImageResource(generator());

    Drawable myDrawable = RImage.getDrawable();

    TextView writeID = (TextView) findViewById(R.id.idtext);
    writeID.setText(String.valueOf(generator()));
}

private int generator() {
    TypedArray imgs = getResources().obtainTypedArray(R.array.list3);
    int imgid = imgs.getResourceId(new Random().nextInt(imgs.length()), -1);
    imgs.recycle();
    return imgid;
}

目前我收到数字,我不知道如何将 int 转换为字符串,或者如何使用生成器生成字符串,显示在 textview 中,然后将其转换为 int 以显示在图像视图.

Currently I receive numbers, and I cant figure out how can I convert the int to a string, or how can I use the generator to generate a string, display in the textview and then convert it to int to display it in the imageview.

我创建了另一个活动,我可以在其中选择我想从微调器中看到的图像,它只是将带有字符串的选定选项发送到按下按钮时的下一个活动,在另一个活动中,我删除了 .png 和 res/drawable/从字符串中提取,并将字符串转换为 int 以显示图像.

I created an another activity where I can pick the image I want to see from a spinner, it simply send the selected option with string to the next activity on button press, and in the other activity i remove the .png and res/drawable/ from the string, and convert the string into an int to display the image.

   Bundle extras = getIntent().getExtras();
    String transportItemChosen = extras.getString("SpinnerValue");
    transportItemChosen = transportItemChosen.replace(".png", "");
    transportItemChosen = transportItemChosen.replace("res/drawable/", "");

    String uri = ("@drawable/" + transportItemChosen);
    int id = getResources().getIdentifier(uri, null, getPackageName());

   ImageView mImageView;
    mImageView = (ImageView) findViewById(R.id.selectedimage);
    mImageView.setImageResource(id);

所以我需要生成器生成字符串,然后将其转换为整数.谢谢!

So I need the generator to generate to string and then convert it to int. Thanks!

推荐答案

通过使用方法将 int id 转换为 String 名称来更改代码

Change your code by converting your int ids into String names by Using the method

getResourceEntryName(int id);

换行:

writeID.setText(String.valueOf(generator()));

至于:

writeID.setText(getResources().getResourceEntryName(generator()));

反之亦然:

如果你有一个 String 让我们假设这个 String 你是从 textView 得到的或者使用上面的方法将它转换回一个 int 确定您执行以下操作:

And if you are having a String lets assume this String you get from a textView or using above method to convert it back to an int id you do the following:

int id= getResources().getIdentifier("your_string_here", "drawable", getPackageName());

如果你是从 Fragment 而不是 Activity 调用代码,记得在调用 getResources() 或在调用 getPackageName() 之前.我在输入时没有测试代码,但我确信它会起作用(可能是错字)有关更多信息,请考虑访问有关资源的文档 来自这个官方链接.

And if you are calling the code from a Fragment not an Activity remember to put getActivity() before calling getResources()or before calling getPackageName(). I have not tested the code as I am typing, but I am sure it will work (may be a typo) for more information consider visting the Documentation on Resources from this official link.

这篇关于如何从imageView获取可绘制名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆