这是我的问题:我正在从服务器异步下载图像并将它们组织在一个数组中,如Image [1],相当于Image1,我需要组织数组的原因是我试图有一个360度旋转的图像。因此,当我将图像下载到阵列中时,似乎它没有正确地完成工作。任何建议都非常受欢迎。
public class tst extends Activity implements OnClickListener {
TextView t;
Button b1;
ImageView iV;
Bitmap bitmap = null;
Bitmap [] angles;
String stringAngle;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
angles = new Bitmap[13];
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(this);
iV = (ImageView) findViewById(R.id.imageView1);
}
@Override
public void onClick(View v) {
if(v.getId() == R.id.button1){
//Toast.makeText(getApplicationContext(), "ghjh", Toast.LENGTH_SHORT).show();
for(int i = 1 ; i<13 ; i++)
{
stringAngle = Integer.toString(i);
new DownloadImageTask()
.execute("http://www.sampleurl"
+stringAngle+".png");
}
iV.setImageBitmap(angles[7]);
}
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
protected Bitmap doInBackground(String... urls) {
return loadImageFromNetwork(urls[0]);
}
protected void onPostExecute(Bitmap result) {
// Do something with bitmap eg:
for(int m = 1 ; m<13;m++)
{
angles[m] =(Bitmap) result;
}
}
}
private Bitmap loadImageFromNetwork(String url) {
try {
bitmap = BitmapFactory.decodeStream((InputStream) new URL(url)
.getContent());
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
}
在这种情况下,iV.setImageBitmap(angles [7])总会返回不同的结果。