-1

画像ビューがforループで更新されないのはなぜですか、最後のURLの画像のみが画像ビューに表示され、すべての画像が表示されるわけではありません。何が間違っているのでしょうか。

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

さて、最初は、2つの連続するコード行の画像を(効果的に)切り替えるデバイスの速度はどれくらいですか?

次に、システムを最初にレイアウトする必要があります。これは、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

基になるオブジェクト(ビットマップ)を設定してから、ImageViewを更新してみてください。

于 2013-02-28T01:57:06.583 に答える
0

Animation.AnimationListener最初のアニメーションにを置くことができ、次の画像をロードしてonAnimationEnd(...)、リスナーのメソッドで新しいトランジションを開始できます。

于 2012-12-15T10:22:34.147 に答える
-1

メソッドloadImage()はUIメインスレッドにありません。ハンドラーを使用して、ImageViewを新しい画像で更新します。

于 2012-12-15T10:19:34.790 に答える