如何在安卓中为ViewFlipper设置动态图片?

12
我这样做,它正在加载静态图像吗?
public class ArchiveGroup extends Activity  {
    Button btn;
    ViewFlipper flip;
    public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.next);
                btn=(Button)findViewById(R.id.btn);
                flip=(ViewFlipper)findViewById(R.id.flip);

        }
            public void ClickHandler(View v)
            {
             flip.showNext();
            }
            public void ClickHandler1(View v)
            {
            Toast.makeText(this,"text",Toast.LENGTH_LONG).show();
            }
}

我在xml文件中使用了三个ImageView在ViewFlipper中。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
    <Button
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Flip"
    android:id="@+id/btn"
    android:onClick="ClickHandler"
    />
    <ViewFlipper
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:id="@+id/flip"
    >
    <ImageView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:src="@drawable/ic_launcher"
    />
     <ImageView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:src="@drawable/ic_launcher"
    />
    <ImageView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:src="@drawable/ic_launcher"
    />

    </ViewFlipper>
</LinearLayout>

如何将动态图片设置给这些ImageView,只使用一个ImageView。

2个回答

50
在你的xml中只需添加以下内容:
<ViewFlipper android:id="@+id/flipper" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"> 
</ViewFlipper>

不需要用任何ImageView来封装。

现在在你的代码中这样做。

假设你已将图片存储在像这样的数组中,

int gallery_grid_Images[]={R.drawable.gallery_image_1,R.drawable.gallery_image_2,R.drawable.gallery_image_3,
        R.drawable.gallery_image_4,R.drawable.gallery_image_5,R.drawable.gallery_image_6,R.drawable.gallery_image_7,
        R.drawable.gallery_image_8,R.drawable.gallery_image_9,R.drawable.gallery_image_10
        };

现在,在您的 onCreate() 方法中,

viewFlipper = (ViewFlipper) findViewById(R.id.flipper);
 for(int i=0;i<gallery_grid_Images.length;i++)
        {
        //  This will create dynamic image view and add them to ViewFlipper
            setFlipperImage(gallery_grid_Images[i]);
        }

现在将此方法添加到您的活动中:

private void setFlipperImage(int res) {
    Log.i("Set Filpper Called", res+"");
    ImageView image = new ImageView(getApplicationContext());
    image.setBackgroundResource(res);
    viewFlipper.addView(image);
}

就是这样。现在使用viewFlipper.showNext();viewFlipper.showPrevious();方法,你可以展示你的动态图片。


0
**we can get images from url in vewfliper**


 URL urls = new URL("image url");
                    Bitmap image = BitmapFactory.decodeStream(urls.openConnection().getInputStream());
                    Data_holder.imge_bitmap.add(image);
**the**


 private void setFlipperImage(Bitmap res) {
        Log.i("Set Filpper Called", res+"");
        ImageView image = new ImageView(getApplicationContext());
        image.setImageBitmap(res);
        mViewFlipper.addView(image);
u can call function
setFlipperImage(Data_holder.imge_bitmap.get(i));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接