-1

为什么我的 imageview 没有在 for 循环中更新,只有最后一个 url 中的图像显示在 imageview 中,而不是所有图像,我做错了什么?

public class LoadImageActivity extends Activity {
    ImageView image_view;
    Bitmap bitmap;

    String[] imageLocation={
            "http://wallbase1.org/thumbs/rozne/thumb-499842.jpg",
            "http://ns3002439.ovh.net/thumbs/rozne/thumb-2493796.jpg",
            "http://ns3002439.ovh.net/thumbs/rozne/thumb-2486664.jpg" };  

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        image_view = (ImageView)findViewById(R.id.imageview);

        for (int i = 0; i < 2; i++) {
            bitmap = loadImage(imageLocation[i]);
            image_view.setImageBitmap(bitmap);

            Animation rotate = AnimationUtils.loadAnimation(LoadImageActivity.this, R.anim.rotate);
            findViewById(R.id.imageview).startAnimation(rotate);
        }
    }

    public  Bitmap loadImage(String image_location){
        URL imageURL = null;
        try {
            imageURL = new URL(image_location);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        try {
            HttpURLConnection connection = HttpURLConnection)imageURL.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream inputStream = connection.getInputStream();

            bitmap = BitmapFactory.decodeStream(inputStream);//Convert to bitmap
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }
}
4

4 回答 4

1

好吧,起初,您设备在两行连续代码中切换图像的速度有多慢(有效)?

其次,必须先对系统进行布局,这发生在onCreate()返回之后。你所做的是:

-set reference to bitmap that will be shown when layouted
-changed that reference
-let the system do the layout (Question: Which reference will be read?)

看起来您想要设置第一个位图,旋转它,然后设置另一个位图。你需要做的是:

-set bitmap
-start animation with an Animation.AnimationListener, which sets the second bitmap in onAnimationEnd().

@感谢亚当!

于 2012-12-15T10:18:41.647 回答
0

你可以在你的第一个动画上放一个,你可以在监听Animation.AnimationListener器的方法中加载下一个图像并开始新的过渡。onAnimationEnd(...)

于 2012-12-15T10:22:34.147 回答
0

尝试设置底层对象(位图),然后更新 ImageView。

于 2013-02-28T01:57:06.583 回答
-1

该方法loadImage()不在 UI 主线程中,请使用 Handler 使用新图像更新 ImageView。

于 2012-12-15T10:19:34.790 回答