本文介绍了从一个活动传递的ImageView到另一个 - 意向 - Android电子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在绘制文件夹170的图像在我的Andr​​oid应用程序。我有一个活动显示他们。什么是想要做的是点击的ImageView传递到另一个活动(Zoom_activity),用户可以放大它,玩弄它。我该如何实现呢?

I have 170 images in the drawable folder in my android app. I have one activity displaying all of them. What is want to do is to pass the clicked imageview to another activity (Zoom_activity) where the user can zoom it and play around with it. How do I achieve it?

所有的图像都500x500px。所以我不认为它们解码成位图,并通过意向传递Btmaps的。请提出一个更好的和简单的方法来做到这一点!我已经有一个看看这里的SO其他的答案,但他们没有解决我的问题。

All the images are 500x500px. So I can't think of decoding them into Bitmaps and passing Btmaps via Intent. Please suggest a better and simple way to do it! I have already had a look at the other answers here on SO but none of them solved my problem.

下面是我的code:

Activity_1.java

Activity_1.java

Intent startzoomactivity = new Intent(Activity_one.this, Zoom_Image.class);
String img_name = name.getText().toString().toLowerCase(); //name is a textview which is in refrence to the imageview.
startzoomactivity.putExtra("getimage", img_name);
startActivity(startzoomactivity);

Zoom_Activity.java

Zoom_Activity.java

    Intent startzoomactivity = getIntent();
    String img_res = getIntent().getStringExtra("getimage");
    String img_fin = "R.drawable."+img_res;
    img.setImageResource(Integer.parseInt(img_fin));

错误:应用程序强制关闭

Error: App force closes

请帮我解决这个问题!结果
谢谢!

Please help me solve this problem!
Thanks!

推荐答案

的Integer.parseInt()仅适用于字符串,如1或123,真正含有只是字符串重新整型的presentation。

Integer.parseInt() only works for strings like "1" or "123" that really contain just the string representation of an Integer.

您需要的是找到它的名字绘制资源。

What you need is find a drawable resource by its name.

这使用反射可以做到:

String name = "image_0";
final Field field = R.drawable.getField(name);
int id = field.getInt(null);
Drawable drawable = getResources().getDrawable(id);

或使用 Resources.getIdentifier()

String name = "image_0";
int id = getResources().getIdentifier(name, "drawable", getPackageName());
Drawable drawable = getResources().getDrawable(id);

这篇关于从一个活动传递的ImageView到另一个 - 意向 - Android电子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 09:51