Coder Social home page Coder Social logo

discview's Introduction

DiscView:

一个仿网易云音乐播放界面旋转唱片的自定义View

效果图:

image 左右滑动可以切换图片(带有切换动画)

设置滑动手势

//MainActivity
discView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                discView.onTouch(v,event);
                return true;
            }
        });
//DiscView
@Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startX = event.getX();
                break;
            case MotionEvent.ACTION_UP:
                if (event.getX() - startX > width / 10) {
                    Log.e(TAG, "right swipe");
                    prev();
                }
                if (event.getX() < startX && (startX - event.getX()) > width / 10) {
                    Log.e(TAG, "left swipe");
                    next();
                }
                if (Math.abs(1.0d * (event.getX() - startX)) < width / 100) {
                    onClick(v);
                }
                break;
        }
        return false;
    }

设置切换时的图片

List<Integer> picList = new ArrayList<>();
        picList.add(R.drawable.pic);

        picList.add(R.drawable.pic_1);

        picList.add(R.drawable.pic_2);

        picList.add(R.drawable.pic_3);

        picList.add(R.drawable.pic_4);

        picList.add(R.drawable.pic_5);
        discView.setUriList(picList);

view的宽高比被固定为1:1.25。

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
        if (widthSpecMode == MeasureSpec.AT_MOST && heightSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(400, 500);
        } else if (widthSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(heightSpecSize * 4 / 5, heightSpecSize);
        } else if (heightSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(widthSpecSize, widthSpecSize * 5 / 4);
        } else {
            if (widthSpecSize * 1.25 != heightSpecSize) {
                setMeasuredDimension(widthSpecSize, widthSpecSize * 5 / 4);
            }
        }

通过DiscView的setPic()方法可以更换view中间的图片

 Bitmap bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(data.getData()));
     discView.setPic(bmp);

#通过点击view可以让view停止/旋转

discview's People

Contributors

jiefly avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.