kyleduo / kyleduo/SwitchButton

2.3手机上用backdrawble,切换时背景无法改变,求助!

Open
#76 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
4.7k
Forks
864
PR merge metrics
No merged PRs in 30d

Description

<com.ftd2009.switchbuttoniosdemo.SwitchButton
    android:id="@+id/sb_ios"
    style="@style/SwitchButtonStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:kswAnimationDuration="300"
    app:kswBackDrawable="@drawable/ios_back_drawable"
    app:kswBackMeasureRatio="2"
    app:kswThumbDrawable="@drawable/ios_thumb_selector"/>

用的是app:kswBackDrawable,@drawable/ios_back_drawable是:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:state_checked="true">
        <shape>
            <solid android:color="#3097fd"/>
            <corners android:radius="15dp"/>
        </shape>
    </item>
    <item android:state_enabled="false" android:state_checked="false">
        <shape>
            <solid android:color="#ff0000"/>
            <corners android:radius="25dp"/>
        </shape>
    </item>
    <item android:state_checked="true">
        <shape>
            <solid android:color="#000000"/>
            <corners android:radius="35dp"/>
        </shape>
    </item>
    <item>
        <shape>
            <solid android:color="#ffff00"/>
            <corners android:radius="45dp"/>
        </shape>
    </item>
</selector>

但是在2.3上切换时改变不了背景色,4.0上都是好的,是什么问题啊。我用的nineold库可以在2.3上有动画,但是切换时背景色改变不了,HELP!
看了看源码,应该是下面这块代码有问题:

if (!mIsBackUseDrawable && mBackColor != null) {
            mCurrBackColor = mBackColor.getColorForState(getDrawableState(), mCurrBackColor);
            mNextBackColor = mBackColor.getColorForState(nextState, mCurrBackColor);
        } else {
            if (mBackDrawable instanceof StateListDrawable && mFadeBack) {
                mBackDrawable.setState(nextState);
                mNextBackDrawable = mBackDrawable.getCurrent().mutate();
            } else {
                mNextBackDrawable = null;
            }
            setDrawableState(mBackDrawable);
            if (mBackDrawable != null) {
                mCurrentBackDrawable = mBackDrawable.getCurrent().mutate();
            }
        } 

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the SwitchButton state-handling code shown in the issue, especially the mBackDrawable and setDrawableState branches. Reproduce the drawable background transition on Android 2.3 and 4.0 using the supplied XML, then compare the resulting drawable state. Done means the background color changes correctly when the switch changes state on both versions.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
mobile
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.