ImageSwitcher的使用

Android自带控件ImageSwitcher,翻页渐变的效果:


其实很简单,直接代码:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button button_add;
    private Button button_jian;
    private ImageSwitcher imageSwitcher;
    private List<Drawable> list;
    private int index = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button_add = (Button) findViewById(R.id.button1);
        button_jian = (Button) findViewById(R.id.button2);
        imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);

        button_add.setOnClickListener(this);
        button_jian.setOnClickListener(this);


        putData();
        //通过代码设定切换效果
        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.fade_in));
        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.fade_out));
        imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {

            @Override
            public View makeView() {
                // makeView返回的是当前需要显示的ImageView控件,用于填充进ImageSwitcher中。
                ImageView image = new ImageView(MainActivity.this);
                image.setMinimumHeight(200);
                image.setMinimumWidth(200);
                image.setScaleType(ImageView.ScaleType.CENTER_CROP);
                image.setLayoutParams(new ImageSwitcher.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
                return image;
            }
        });
        imageSwitcher.setImageDrawable(list.get(0));

    }

    private void putData() {
        //填充图片的Drawable资源数组
        list = new ArrayList<Drawable>();
        list.add(getResources().getDrawable(R.mipmap.guide_01));
        list.add(getResources().getDrawable(R.mipmap.guide_02));
        list.add(getResources().getDrawable(R.mipmap.guide_03));
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button1:

                index--;
                if (index < 0) {
                    //用于循环显示图片
                    index = list.size() - 1;
                }
                //设定ImageSwitcher显示新图片
                imageSwitcher.setImageDrawable(list.get(index));
                break;

            case R.id.button2:
                index++;
                if (index >= list.size()) {
                    //用于循环显示图片
                    index = 0;
                }
                imageSwitcher.setImageDrawable(list.get(index));
                break;
        }
    }
}



渐变效果用的是android自带的。

下面是xml,是不是很简单:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageSwitcher
        android:id="@+id/imageSwitcher"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ImageSwitcher>


    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="上一张"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="20dp"
        />


    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="下一张"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="20dp"
        android:layout_marginRight="20dp"
        />
</RelativeLayout>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值