Giter Site home page Giter Site logo

slideswitch's Introduction

SlideSwitch

About SlideSwitch

A button that you can slide on or off

How to import into your project

Android Studio

use gradle.

Gradle dependency:

Add the below codes in the module gradle file, which module use this widget.

compile 'com.leaking.slideswitch:slideswitch:1.0.0'

Add the below codes in you project gradle file

allprojects {
    repositories {
        jcenter()
        maven {
            url 'https://dl.bintray.com/leaking/maven'
        }
    }
}

Eclipse

import it as a library project.

How to use it

you can define a slideswitch in xml like the following example

    <com.leaking.slideswitch.SlideSwitch
        android:layout_width="100dip"
        android:layout_height="120dip"
        slideswitch:isOpen="false"
        slideswitch:shape="circle"
        slideswitch:themeColor="#f200aa96" >
    </com.leaking.slideswitch.SlideSwitch>

you can initial the state(on or off) in jave code in this way

	bulletSwitch.setState(true);

and you can listen to the change of the slideswitch like this

    updateSwitch.setSlideListener(new SlideListener() {

            @Override
            public void open() {
                // Do something ,,,
            }

            @Override
            public void close() {
                // Do something ,,,
            }
        });

you even can forbid the widget to change its state(open or close) like this

    slide.setSlideable(false);
    slide.setSlideable(true);

What does it look like

##Author

Quinn Chen

[email protected]

LICENSE

Copyright 2015 Quinn Chen

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

slideswitch's People

Contributors

leaking avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

slideswitch's Issues

The background doesn't show as expected when I set it to circle.

When you set RIM_SIZE larger, you will find it obviously.

The radius of the background rounded rectangle maybe not big enough.

I found this problem due to this code in onDraw method:

radius = backRect.height() / 2 - RIM_SIZE;

Maybe you should remove the RIM_SIZE.

Memory leak

OOM exception -> drag the slider one way, then back again. The app will crash. I can reproduce this 100%. Commenting out line 205 moveToDest(toRight) stops the OOM. However leaves the functionality undesirable as the thumb can be left in whatever position you left it in.

提个建议

在moveToDest方法里面判断手势回调时增加当前状态的判断,如果当前是open状态,则继续重复右滑就不在回调,反之亦然。

public void moveToDest(final boolean toRight) {
        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 1) {
                    listener.open();
                } else {
                    listener.close();
                }
            }
        };
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (toRight) {
                    while (frontRect_left <= max_left) {
                        alpha = (int) (255 * (float) frontRect_left / (float) max_left);
                        invalidateView();
                        frontRect_left += 3;
                        try {
                            Thread.sleep(3);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    alpha = 255;
                    frontRect_left = max_left;
                    if (!isOpen) {
                        isOpen = true;
                        if (listener != null)
                            handler.sendEmptyMessage(1);
                        frontRect_left_begin = max_left;
                    }
                } else {
                    while (frontRect_left >= min_left) {
                        alpha = (int) (255 * (float) frontRect_left / (float) max_left);
                        invalidateView();
                        frontRect_left -= 3;
                        try {
                            Thread.sleep(3);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    alpha = 0;
                    frontRect_left = min_left;
                    if (isOpen) {
                        isOpen = false;
                        if (listener != null)
                            handler.sendEmptyMessage(0);
                        frontRect_left_begin = min_left;
                    }
                }
            }
        }).start();
    }

Animation stops inside ScrollView

Hi, thank you for sharing this widget, I really liked it.
I was using it and making some particular adaptations when I encountered an issue. If I use the widget inside a ScrollView, starting the move to any side and then moving down or up, the ScrollView intercepts the MotionEvent and the switch stops the animation in the middle.

I found a solution and I want to share with you. When ScrollView handle the event, its children Views triggers the MotionEvent ACTION_CANCEL. So I copied the code inside inside ACTION_UP (line 188) and added another case ACTION_CANCEL, like this:

       <rest of code> ...

            break;
        case MotionEvent.ACTION_MOVE:
            eventMovingY = (int) event.getY();
            diffY = eventMovingY - eventStartY;

            eventLastX = (int) event.getRawX();
            diffX = eventLastX - eventStartX;
            int tempX = diffX + frontRect_left_begin;
            tempX = (tempX > max_left ? max_left : tempX);
            tempX = (tempX < min_left ? min_left : tempX);
            if (tempX >= min_left && tempX <= max_left) {
                frontRect_left = tempX;
                alpha = (int) (255 * (float) tempX / (float) max_left);
                invalidateView();
            }

            break;
        case MotionEvent.ACTION_UP:
            moveToDest(isToRight(event));
            break;
        case MotionEvent.ACTION_CANCEL:
            moveToDest(isToRight(event));
            break;
        default:
            break;
    }

private boolean isToRight(MotionEvent event) {
    int wholeX = (int) (event.getRawX() - eventStartX);
    frontRect_left_begin = frontRect_left;
    boolean toRight;
    toRight = (frontRect_left_begin > max_left / 2 ? true : false);
    if (Math.abs(wholeX) < 3) {
        toRight = !toRight;
    }
    return toRight;
}

I'm sorry for my english and thank you again!

ic_launcher.png

Please get rid of the ic_launcher in manifest and in drawable directories

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.