图片轮播 文字轮播 ImageSwitcher TextSwitcher

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/HandsomeFuHS/article/details/68063939

源代码
平常开发中经常会遇到图片轮播(淘宝首页推荐)和文字轮播(淘宝头条)的需求,今天我就为大家带来一种简单的实现方式。

图片轮播:ImageSwitcher

1.在xml中定义ImageSwitcher

<ImageSwitcher
        android:id="@+id/is_switcher"
        android:layout_width="match_parent"
        android:layout_height="200dp"></ImageSwitcher>

2.代码里

public class MainActivity extends AppCompatActivity {
    private static final int IMAGE_SWITCHER = 1;
    //切换间隔
    private static final int DURATION = 2000;
    private ImageSwitcher isSwitcher;
    //当前显示的图片
    private int imageIndex = 0;
    //图片ID
    private int[] images = new int[]{
            R.mipmap.fruit,
            R.mipmap.baikal1,
            R.mipmap.baikal2,
            R.mipmap.alps,
            R.mipmap.the_great_lakes};
    //只有主线程才能操作UI,所以交给handler处理
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case IMAGE_SWITCHER:
                    if (isSwitcher == null)
                        return;
                    imageIndex++;
                    imageIndex = imageIndex % images.length;
                    isSwitcher.setImageResource(images[imageIndex]);
                    Message imgMessage = new Message();
                    imgMessage.what = IMAGE_SWITCHER;
                    handler.sendMessageDelayed(imgMessage, DURATION);
                    break;
            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initImageSwitcher();
    }
    private void initImageSwitcher() {
        isSwitcher = (ImageSwitcher) findViewById(R.id.is_switcher);
        isSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                ImageView imageView = new ImageView(MainActivity.this);
                imageView.setLayoutParams(new FrameLayout.LayoutParams(
                        FrameLayout.LayoutParams.MATCH_PARENT,
                        FrameLayout.LayoutParams.MATCH_PARENT));
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                return imageView;
            }
        });
        isSwitcher.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "第" + (imageIndex + 1) + "张图片", Toast.LENGTH_SHORT).show();
            }
        });
        isSwitcher.setInAnimation(AnimationUtils.loadAnimation(getApplicationContext(),
                android.R.anim.slide_in_left));
        isSwitcher.setOutAnimation(AnimationUtils.loadAnimation(getApplicationContext(),
                android.R.anim.slide_out_right));
        isSwitcher.setImageResource(images[imageIndex]);
        Message imgMsg = new Message();
        imgMsg.what = IMAGE_SWITCHER;
        handler.sendMessageDelayed(imgMsg, DURATION);
    }
}

文字轮播:TextSwitcher

1.在xml中定义TextSwitcher

 <TextSwitcher
        android:id="@+id/ts_switcher"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></TextSwitcher>

2.代码中

public class MainActivity extends AppCompatActivity {
    //切换间隔
    private static final int DURATION = 2000;
    private TextSwitcher tsSwitcher;
    //当前显示的文字
    private int textIndex = 0;
    //要显示的文字数组
    private String[] texts = new String[]{
            "结庐在人境,而无车马喧",
            "问君何能尔,心远地自偏",
            "采菊东篱下,悠然现南山",
            "山气日夕佳,飞鸟相与还",
            "此中有真意,欲辨已忘言"};
    private static final int TEXT_SWITCHER = 2;
    //只有主线程才能操作UI,所以交给handler处理
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case TEXT_SWITCHER:
                    if (tsSwitcher == null)
                        return;
                    textIndex++;
                    textIndex = textIndex % texts.length;
                    tsSwitcher.setText(texts[textIndex]);
                    Message textMessage = new Message();
                    textMessage.what = TEXT_SWITCHER;
                    handler.sendMessageDelayed(textMessage, DURATION);
                    break;
            }

        }
    };

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

    private void initTextSwitcher() {
        tsSwitcher = (TextSwitcher) findViewById(R.id.ts_switcher);
        tsSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                TextView tv = new TextView(MainActivity.this);
                tv.setTextSize(28);
                tv.setTextColor(Color.GREEN);
                tv.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT));
                tv.setGravity(Gravity.CENTER);
                return tv;
            }
        });
        tsSwitcher.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "第" + (textIndex + 1) + "条文字", Toast.LENGTH_SHORT).show();
            }
        });
        tsSwitcher.setText(texts[textIndex]);
        tsSwitcher.setInAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_in_bottom));
        tsSwitcher.setOutAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_out_top));
        Message textMsg = new Message();
        textMsg.what = TEXT_SWITCHER;
        handler.sendMessageDelayed(textMsg, DURATION);
    }
}

其中slide_in_bottom:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromYDelta="50%p" android:toYDelta="0"
        android:duration="@android:integer/config_mediumAnimTime"/>
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0"
        android:duration="@android:integer/config_mediumAnimTime" />
</set>

slide_out_top

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromYDelta="0" android:toYDelta="-50%p"
        android:duration="@android:integer/config_mediumAnimTime"/>
    <alpha android:fromAlpha="1.0" android:toAlpha="0.0"
        android:duration="@android:integer/config_mediumAnimTime" />
</set>

今天就先说到这,谢谢大家!
源代码

猜你喜欢

转载自blog.csdn.net/HandsomeFuHS/article/details/68063939