加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

ImageSwitcher实现引导页效果

发布时间:2020-12-15 03:16:36 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 布局代码: ?xml version="1.0" encoding="UTF-8"?FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

布局代码:

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

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

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/ll_view"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="30dp"
            android:gravity="center_horizontal"
            android:orientation="horizontal">
        </LinearLayout>
    </RelativeLayout>
</FrameLayout>

页面代码:

public class ImageSwitcherActivity extends Activity implements ViewSwitcher.ViewFactory,View.OnTouchListener {
    private int[] imgIds;//图片id数组
    private int currentPosition;//当前选中的图片id序号
    private ImageSwitcher mImageSwitcher;//ImagaSwitcher 的引用

    private float downX;//按下点的X坐标
    private ImageView[] tips;//点点数组
    private LinearLayout linearLayout;//装载点点的容器

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

        imgIds = new int[]{R.drawable.bg,R.drawable.c2,R.drawable.c3,R.drawable.c4,R.drawable.c5,R.drawable.c6,R.drawable.c7,R.drawable.c8,R.drawable.c9};

        mImageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);//实例化ImageSwitcher
        mImageSwitcher.setFactory(this); //设置Factory
        mImageSwitcher.setOnTouchListener(this);//设置OnTouchListener,我们通过Touch事件来切换图片

        linearLayout = (LinearLayout) findViewById(R.id.ll_view);//指示器布局
        tips = new ImageView[imgIds.length];
        for (int i = 0; i < imgIds.length; i++) {
            ImageView mImageView = new ImageView(this);
            tips[i] = mImageView;
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
            layoutParams.rightMargin = 3;
            layoutParams.leftMargin = 3;

            mImageView.setBackgroundResource(R.drawable.page_indicator_unfocused);
            linearLayout.addView(mImageView,layoutParams);
        }

        //上一个界面传过来的位置
        currentPosition = getIntent().getIntExtra("position",0);
        mImageSwitcher.setImageResource(imgIds[currentPosition]);

        setImageBackground(currentPosition);

    }

    //设置选中的tip的背景
    private void setImageBackground(int selectItems) {
        for (int i = 0; i < tips.length; i++) {
            if (i == selectItems) {
                tips[i].setBackgroundResource(R.drawable.page_indicator_focused);
            } else {
                tips[i].setBackgroundResource(R.drawable.page_indicator_unfocused);
            }
        }
    }

    @Override
    public View makeView() {
        final ImageView i = new ImageView(this);
        i.setBackgroundColor(0xff000000);
        i.setScaleType(ImageView.ScaleType.CENTER_CROP);
        i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
        return i;
    }

    @Override
    public boolean onTouch(View v,MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                downX = event.getX();//手指按下的X坐标
                break;
            }
            case MotionEvent.ACTION_UP: {
                float lastX = event.getX();
                //抬起的时候的X坐标大于按下的时候就显示上一张图片
                if (lastX > downX) {
                    if (currentPosition > 0) {
                        //设置动画
                        mImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(getApplication(),R.anim.left_in));
                        mImageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(getApplication(),R.anim.right_out));
                        currentPosition--;
                        mImageSwitcher.setImageResource(imgIds[currentPosition % imgIds.length]);
                        setImageBackground(currentPosition);
                    } else {
                        Toast.makeText(getApplication(),"已经是第一张",Toast.LENGTH_SHORT).show();
                    }
                }

                if (lastX < downX) {
                    if (currentPosition < imgIds.length - 1) {
                        mImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(getApplication(),R.anim.right_in));
                        mImageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(getApplication(),R.anim.lift_out));
                        currentPosition++;
                        mImageSwitcher.setImageResource(imgIds[currentPosition]);
                        setImageBackground(currentPosition);
                    } else {
                        Toast.makeText(getApplication(),"到了最后一张",Toast.LENGTH_SHORT).show();
                    }
                }
            }

            break;
        }

        return true;
    }

}

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读