-2

我在考虑如何使用 ImageSwitchers 和 Gallery 制作自动幻灯片时遇到了麻烦。我唯一了解的是当您从图库中选择图片时过渡图片之间的动画,例如它们淡入淡出或从左到右移动的方式。

编辑

我的目的是在创建图库并选择每张图片然后使用 imageswitcher 在底部布局上显示所选图片时尝试以相同的方式制作幻灯片。就在这一次,而不是实际点击,我希望图片自动移动(最后一张之后的下一张),就像在 Powerpoints 上一样,显然还有一个计时器。

这是整个类: package project.CaseStudy;

public class Slideshow extends Activity implements ViewFactory {
    //---the images to display---
    Integer[] imageIDs = {
            R.drawable.pic1,
            R.drawable.pic2,
            R.drawable.pic3,
            R.drawable.pic4,
            R.drawable.pic5,
            R.drawable.pic6,
            R.drawable.pic7
    };

    TextView legend;
    TextView imageID;

    CheckBox repeat;
    CheckBox image;
    CheckBox text;

    public int picture;
    public int pictureCurrentlySelected;

    private ImageSwitcher imageSwitcher;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.slideshow);

        legend = (TextView) findViewById(R.id.legend);
        imageID = (TextView) findViewById(R.id.imagenum);

        repeat = (CheckBox) findViewById(R.id.repeat);
        image = (CheckBox) findViewById(R.id.number);
        text = (CheckBox) findViewById(R.id.text);

        //text.setEnabled(true);
        //image.setEnabled(true);
        //repeat.setEnabled(true);


        imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher1);
        imageSwitcher.setFactory(this);


        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_in));
        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_out));


        /*imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.slide_in_left));
        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.slide_out_right));

        */
        pictureCurrentlySelected = -1;
        Gallery gallery = (Gallery) findViewById(R.id.gallery1);
        gallery.setAdapter(new ImageAdapter(this));
        gallery.setOnItemClickListener(new OnItemClickListener()
        {
            public void onItemClick(AdapterView<?> parent,
            View v, int position, long id)
            {
                imageSwitcher.setImageResource(imageIDs[position]);
                picture = position;
                pictureCurrentlySelected = picture;
                check();
            }
        });
    }

    public View makeView()
    {
        ImageView imageView = new ImageView(this);
        imageView.setBackgroundColor(0xFF000000);
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        imageView.setLayoutParams(new
                ImageSwitcher.LayoutParams(
                        LayoutParams.FILL_PARENT,
                        LayoutParams.FILL_PARENT));
        return imageView;
    }

    public class ImageAdapter extends BaseAdapter
    {
        private Context context;
        private int itemBackground;

        public ImageAdapter(Context c)
        {
            context = c;

            //---setting the style---
            TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
            itemBackground = a.getResourceId(
                    R.styleable.Gallery1_android_galleryItemBackground, 0);
            a.recycle();
        }

        //---returns the number of images---
        public int getCount()
        {
            return imageIDs.length;
        }

        //---returns the item---
        public Object getItem(int position)
        {
            return position;
        }

        //---returns the ID of an item---
        public long getItemId(int position)
        {
            return position;
        }

        //---returns an ImageView view---
        public View getView(int position, View convertView, ViewGroup parent)
        {
            ImageView imageView = new ImageView(context);

            imageView.setImageResource(imageIDs[position]);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
            imageView.setBackgroundResource(itemBackground);

            return imageView;
        }
    }

    public void onCheckBoxClickListener(View v)
    {
        boolean checked = ((CheckBox) v).isChecked();

        switch(v.getId()){

        case R.id.repeat:
            /*if(checked)
            {
                return;
            }*/
            break;

        case R.id.text:
            if(!checked)
                legend.setText("");
            else
            {
                if(pictureCurrentlySelected == -1)
                {
                    legend.setText("");
                }
                else
                    check();
            }

            break;

        case R.id.number:
            if(!checked)
                imageID.setText("");
            else
            {
                if(pictureCurrentlySelected == -1)
                {
                    legend.setText("");
                }
                else
                    check();
            }

            break;

        }
    }

    private void check()
    {
        if(text.isChecked())
        {
            if(picture == 0)
                legend.setText("This is San Fransisco's Bridge. A beautiful Sight!");
            else if(picture == 1)
                legend.setText("This is the train used to pick up the bitches around town.");
            else if(picture == 2)
                legend.setText("The outstanding lake of San Fransisco");
            else if(picture == 3)
                legend.setText("A beautiful view of the city!");
            else if(picture == 4)
                legend.setText("Beauty at its limit.");
            else if(picture == 5)
                legend.setText("Town at sunset.");
            else
                legend.setText("Bridge during within a beautiful sight.");

        }
        /*else
            legend.setText("");*/

        if(image.isChecked())
        {
            if(picture == 0)
                imageID.setText("Image number 1");
            else if(picture == 1)
                imageID.setText("Image number 2");
            else if(picture == 2)
                imageID.setText("Image number 3");
            else if(picture == 3)
                imageID.setText("Image number 4");
            else if(picture == 4)
                imageID.setText("Image number 5");
            else if(picture == 5)
                imageID.setText("Image number 6");
            else
                imageID.setText("Image number 7");

        }
        //else
            //legend.setText("");

        if(repeat.isChecked())
        {
        }
        //else;

    }

}

如果它太复杂而无法理解,请忽略最后两种方法我只是在草绘我的程序,并且最终会在以后修复冗余。

这是 XML 布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Images of San Francisco" />

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

        <CheckBox
            android:id="@+id/repeat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onCheckBoxClickListener"
            android:text="Repeat" />

        <CheckBox
            android:id="@+id/number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onCheckBoxClickListener"
            android:text="Image ID" />

        <CheckBox
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onCheckBoxClickListener"
            android:text="Text" />

    </LinearLayout>

    <Gallery
        android:id="@+id/gallery1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

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

        <ImageSwitcher
            android:id="@+id/switcher1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
        </ImageSwitcher>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Image number: " />

        <TextView
            android:id="@+id/imagenum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/legend"
            android:layout_width="match_parent"
            android:layout_height="57dp"
            android:text="" />

    </LinearLayout>

</LinearLayout>
4

1 回答 1

0

我不确定您要实现的确切目标,但我所知道的创建小型幻灯片的最佳(阅读:最简单)方法是将您的 imageViews 和/或 textViews 放入框架布局中。然后在您的代码中,您将使用一个 switch 语句来设置每个图像的可见性。

例如。案例0:

image1.setVisibility (View.VISIBLE);

image2.setVisibility (View.INVISIBLE);

ETC....

于 2013-04-16T07:05:18.977 回答