简体   繁体   中英

How to show more than one picture with ImageSwitcher, Android Eclipse?

I'm trying to make the app show 5 pictures that are include in the drawable file of project, I need when the user tap the "Next" button for first time show the first picture when he tap second show the second picture and so on. but when he tap the "Back" button the previous picture show up, for example the user click 4 times "next" button then when he tap "Back" button it shows the third picture and so on.

JAVA CODE:

package com.example.tables;
import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

public class MainActivity extends Activity implements ViewFactory{

ImageSwitcher imgS;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    imgS=(ImageSwitcher)findViewById(R.id.imageSwitcher1);
    imgS.setFactory(this);

    Animation inShow=AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
    imgS.setInAnimation(inShow);

    Animation outShow=AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);
    imgS.setOutAnimation(outShow);
}

@Override
public View makeView() {

    ImageView tableImage=new ImageView(getApplicationContext());
    tableImage.setScaleType(ImageView.ScaleType.FIT_CENTER);///mal2 alcenter

    tableImage.setLayoutParams(new ImageSwitcher.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT , ActionBar.LayoutParams.WRAP_CONTENT));

    return tableImage;
}


public void buttonNext(View view){
    imgS.setImageResource(R.drawable.i);
}

public void buttonBack(View view){
    imgS.setImageResource(R.drawable.ii);
     }
}

XML CODE:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.tables.MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

            <RadioButton
                android:id="@+id/radioButton1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Reserve table" />

            <TextView
                android:id="@+id/selectTableViewText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="12sp"
                android:textColor="#f00"
                android:layout_marginLeft="30dp"
                android:text="Please check the button up to Reserve the table">
            </TextView>

        <ImageSwitcher
            android:id="@+id/imageSwitcher1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

        </ImageSwitcher>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0" >

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Next" 
                android:onClick="buttonNext"
                android:layout_weight="1"/>

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Back"
                android:onClick="buttonBack" 
                android:layout_weight="1"/>

        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

Make an array of the images then add that to your image switcher. You would also need to add some type of current index constant variable so you can index the switching

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM