如何在Android的设置dymanic图像ViewFlipper?图像、如何在、dymanic、Android

2023-09-12 03:30:28 作者:摇头你得带手势.?

我做这样的,它加载的静态图像,这些?

I am doing like these it is loading static these image?

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();
            }
}

我是用三的ImageView在ViewFlipper在 XML 文件

<?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的。

How to set dynamic images to these imageView. By using single ImageView.

推荐答案

在你的XML仅仅只添加这一点,

In your xml just add only this,

<ViewFlipper android:id="@+id/flipper" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"> 
</ViewFlipper>

无需任何ImageViews包围。

need not enclose with any ImageViews.

现在这样在你的代码。

Now do this in your coding.

考虑到已存储的图片在这样的阵列,

Considering that you have stored your Images in a array like this,

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()

Now in your 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]);
        }

而现在这种方法添加到您的活动,

And now add this method to your activity,

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.show previous(); 方法,你可以展示你的动态图像。

That's it. And now using your viewFlipper.showNext(); and viewFlipper.showPrevious(); methods you can show your dynamic images.