Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
394 views
in Technique[技术] by (71.8m points)

How to set dynamic images to ViewFlipper in android?

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

i am using three imageview in ViewFlipper in xml file

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your xml just add only this,

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

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

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...