我想将位图设置为从多个图像网址生成的两个图像视图,并且必须为最后一个图像视图设置ontouchlistener / onclicklistener以动态更改位图图像。任何人都可以给我一些建议吗?
这里我只使用im0(imageview),但我想分别动态地将图像加载到im0,im1和im2(imageview)。并且必须将onclicklistener设置为最后的imageview本身。
我的主要活动是: -
public class MainActivity extends Activity {
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arraylist=new ArrayList<String>();
im0=(ImageView) findViewById(R.id.imageView1);
im1=(ImageView) findViewById(R.id.imageView2);
im2=(ImageView) findViewById(R.id.imageView3);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
jsonobj = JSONfunctions
.getJSONfromURL("http://192.168.1.195/Madhu/anrdmulti/multidocretrieve.php");
try
{
jsonarr = jsonobj.getJSONArray("uploadimages");
for (int i = 0; i < jsonarr.length(); i++)
{
JSONObject jsonobj1 = jsonarr.getJSONObject(i);
id=jsonobj1.getInt("id");
JSONObject jsobj=jsonobj1.getJSONObject("allimages");
for(int j=0;j<jsobj.length();j++)
{
JSONArray image_array=jsobj.getJSONArray("images");
for(int k=0;k<image_array.length();k++)
{
String image= image_array.getString(k);
arraylist.add(image);
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
allimagesarr=arraylist.toArray(new String[arraylist.size()]);
for(int j=0;j<3;j++)
{
Drawable s=LoadImageFromWebOperations(allimagesarr[j]);
im0.setImageDrawable(s);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public static Drawable LoadImageFromWebOperations(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
return null;
}
}
}
我的xml文件是: -
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_marginTop="5dp"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_marginTop="5dp"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_marginTop="5dp"
android:src="@drawable/ic_launcher" />