├── .gitignore ├── .idea ├── .name ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── gradle.xml ├── misc.xml ├── modules.xml ├── scopes │ └── scope_settings.xml └── vcs.xml ├── LICENSE ├── README.md ├── Screenshots ├── horizontal_samples.png ├── snippet_1.png ├── snippet_2.png ├── snippet_3.png ├── snippet_4.png └── vertical_samples.png ├── YinLi-YLSegmentedControl.iml ├── apk └── demo.apk ├── app ├── .gitignore ├── app.iml ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── yinli │ │ └── yinli_ylsegmentedcontrol │ │ └── ApplicationTest.java │ └── main │ ├── AndroidManifest.xml │ ├── ic_launcher-web.png │ ├── java │ └── com │ │ └── yinli │ │ └── yinli_ylsegmentedcontrol │ │ ├── MainActivity.java │ │ ├── Sample1Fragment.java │ │ └── Sample2Fragment.java │ └── res │ ├── drawable │ ├── facebook.png │ ├── ic_action_paste.png │ ├── ic_action_photo.png │ ├── ic_action_search.png │ ├── ic_action_star.png │ ├── twitter.png │ └── youtube.png │ ├── layout │ ├── activity_main.xml │ ├── fragment_sample1.xml │ └── fragment_sample2.xml │ ├── menu │ └── menu_main.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── mipmap-xxxhdpi │ └── ic_launcher.png │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── ylsegmentedcontrol ├── .gitignore ├── build.gradle ├── proguard-rules.pro ├── src ├── androidTest │ └── java │ │ └── com │ │ └── yinli │ │ └── ylsegmentedcontrol │ │ └── ApplicationTest.java └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── yinli │ │ └── ylsegmentedcontrol │ │ ├── YLSegmentedImageRadioButton.java │ │ ├── YLSegmentedRadioButton.java │ │ ├── YLSegmentedRadioGroup.java │ │ └── utils │ │ ├── YLColorHelper.java │ │ └── YLMeasureHelper.java │ └── res │ ├── drawable │ └── button_background.xml │ └── values │ ├── ylsc_attrs.xml │ ├── ylsc_colors.xml │ ├── ylsc_defaults.xml │ ├── ylsc_dimens.xml │ └── ylsc_strings.xml └── ylsegmentedcontrol.iml /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /local.properties 3 | /.idea/workspace.xml 4 | /.idea/libraries 5 | .DS_Store 6 | /build 7 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | YinLi-YLSegmentedControl -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 18 | 19 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | Android API 19 Platform 14 | 15 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/scopes/scope_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Yin Li 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # YLSegmentedControl 2 | A smart custom iOS-like segmented control for Android, which has a remarkable capability to handle appearances for both frame and content automatically. 3 | 4 | * Support any color and size image (with transparent background) as an icon. The library will automatically scale the size and replace the color for the icon image to match different styles. 5 | * Support various states such as "pressed", "checked", "unchecked" and "disabled". 6 | * Support all types - text-only, image-only, and text with image. 7 | * Support both horizontal and vertical modes. 8 | * Easy to customize styles like active color, inactive color, length of radius, square or round, border color, etc. 9 | 10 | #Screenshots 11 |
12 |

13 | 14 | 15 |

16 | 17 | # Demo Project 18 | This library comes with a demo project for you. You can download the latest demo APK from this link: 19 | https://github.com/YinLiWisdom/Android-YLSegmentedControl/blob/master/apk/demo.apk 20 | 21 | It's also on Google Play: 22 | 23 | 24 | Get it on Google Play 26 | 27 | 28 | Having the demo project installed is a good way to be notified of new releases. 29 | 30 | # Useage 31 | ### Step 1 32 | #### Gradle (jCenter) 33 | ``` 34 | dependencies { 35 | compile "com.yinli:ylsegmentedcontrol:1.0.0" 36 | } 37 | ``` 38 | #### Maven 39 | ``` 40 | 41 | com.yinli 42 | ylsegmentedcontrol 43 | 1.0.0 44 | 45 | ``` 46 | 47 | ### Step 2 48 | 1. Add `YLSegmentedControl` into your project. 49 | * The following code snippet will give you a horizontal 3-button segmented control. 50 | 51 |

52 | ```xml 53 | 58 | 59 | 66 | 67 | 73 | 74 | 80 | 81 | 82 | ``` 83 | It allows to change orientation by simply setting `android:orientation=""` to `horizontal` or `vertical`. 84 | 85 | * Text with image segmented button 86 | ``` 87 | 94 | ``` 95 | The icon image will be placed on the left hand side by using above snippet, but it also supports "right", "top", and "bottom" icon positions. 96 |

97 | 98 | * Image-only segmented button 99 | ``` 100 | 106 | ``` 107 |

108 | 109 | 2. Customize styles 110 | * `activeColor` 111 | * `inactiveColor` 112 | * `borderColor` 113 | * `borderWeight` 114 | * `isRound` 115 | * `radius` 116 | 117 | You can find more details from the demo project. 118 | 119 | # Developed By 120 | * Yin Li - 121 | 122 | # License 123 | The MIT License (MIT) 124 | 125 | Copyright (c) 2015 Yin Li 126 | 127 | Permission is hereby granted, free of charge, to any person obtaining a copy 128 | of this software and associated documentation files (the "Software"), to deal 129 | in the Software without restriction, including without limitation the rights 130 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 131 | copies of the Software, and to permit persons to whom the Software is 132 | furnished to do so, subject to the following conditions: 133 | 134 | The above copyright notice and this permission notice shall be included in all 135 | copies or substantial portions of the Software. 136 | 137 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 138 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 139 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 140 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 141 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 142 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 143 | SOFTWARE. 144 | -------------------------------------------------------------------------------- /Screenshots/horizontal_samples.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinLiWisdom/Android-YLSegmentedControl/ad2a3e63686fb111fd715db18007c8095bc6f4c3/Screenshots/horizontal_samples.png -------------------------------------------------------------------------------- /Screenshots/snippet_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinLiWisdom/Android-YLSegmentedControl/ad2a3e63686fb111fd715db18007c8095bc6f4c3/Screenshots/snippet_1.png -------------------------------------------------------------------------------- /Screenshots/snippet_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinLiWisdom/Android-YLSegmentedControl/ad2a3e63686fb111fd715db18007c8095bc6f4c3/Screenshots/snippet_2.png -------------------------------------------------------------------------------- /Screenshots/snippet_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinLiWisdom/Android-YLSegmentedControl/ad2a3e63686fb111fd715db18007c8095bc6f4c3/Screenshots/snippet_3.png -------------------------------------------------------------------------------- /Screenshots/snippet_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinLiWisdom/Android-YLSegmentedControl/ad2a3e63686fb111fd715db18007c8095bc6f4c3/Screenshots/snippet_4.png -------------------------------------------------------------------------------- /Screenshots/vertical_samples.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinLiWisdom/Android-YLSegmentedControl/ad2a3e63686fb111fd715db18007c8095bc6f4c3/Screenshots/vertical_samples.png -------------------------------------------------------------------------------- /YinLi-YLSegmentedControl.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /apk/demo.apk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinLiWisdom/Android-YLSegmentedControl/ad2a3e63686fb111fd715db18007c8095bc6f4c3/apk/demo.apk -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/app.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 21 5 | buildToolsVersion "21.1.2" 6 | 7 | defaultConfig { 8 | applicationId "com.yinli.yinli_ylsegmentedcontrol" 9 | minSdkVersion 9 10 | targetSdkVersion 21 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | compile project(':ylsegmentedcontrol') 25 | compile 'com.android.support:appcompat-v7:21.0.3' 26 | compile 'com.android.support:support-v4:21.0.3' 27 | } 28 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/yinli/Documents/android-sdks/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/yinli/yinli_ylsegmentedcontrol/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.yinli.yinli_ylsegmentedcontrol; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 10 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /app/src/main/ic_launcher-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinLiWisdom/Android-YLSegmentedControl/ad2a3e63686fb111fd715db18007c8095bc6f4c3/app/src/main/ic_launcher-web.png -------------------------------------------------------------------------------- /app/src/main/java/com/yinli/yinli_ylsegmentedcontrol/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.yinli.yinli_ylsegmentedcontrol; 2 | 3 | import android.net.Uri; 4 | import android.support.v4.app.FragmentTransaction; 5 | import android.support.v7.app.ActionBarActivity; 6 | import android.os.Bundle; 7 | import android.view.Menu; 8 | import android.view.MenuItem; 9 | 10 | 11 | public class MainActivity extends ActionBarActivity { 12 | 13 | @Override 14 | protected void onCreate(Bundle savedInstanceState) { 15 | super.onCreate(savedInstanceState); 16 | setContentView(R.layout.activity_main); 17 | 18 | if (findViewById(R.id.fragmentContainer) != null) { 19 | if (savedInstanceState != null) { 20 | return; 21 | } 22 | 23 | Sample1Fragment fragment = new Sample1Fragment(); 24 | getSupportFragmentManager().beginTransaction().add(R.id.fragmentContainer, fragment).commit(); 25 | } 26 | } 27 | 28 | @Override 29 | public boolean onCreateOptionsMenu(Menu menu) { 30 | // Inflate the menu; this adds items to the action bar if it is present. 31 | getMenuInflater().inflate(R.menu.menu_main, menu); 32 | return true; 33 | } 34 | 35 | @Override 36 | public boolean onOptionsItemSelected(MenuItem item) { 37 | // Handle action bar item clicks here. The action bar will 38 | // automatically handle clicks on the Home/Up button, so long 39 | // as you specify a parent activity in AndroidManifest.xml. 40 | int id = item.getItemId(); 41 | 42 | FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 43 | //noinspection SimplifiableIfStatement 44 | if (id == R.id.sample1) { 45 | Sample1Fragment fragment = new Sample1Fragment(); 46 | transaction.replace(R.id.fragmentContainer, fragment); 47 | transaction.commit(); 48 | return true; 49 | } 50 | if (id == R.id.sample2) { 51 | Sample2Fragment fragment = new Sample2Fragment(); 52 | transaction.replace(R.id.fragmentContainer, fragment); 53 | transaction.commit(); 54 | return true; 55 | } 56 | 57 | return super.onOptionsItemSelected(item); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /app/src/main/java/com/yinli/yinli_ylsegmentedcontrol/Sample1Fragment.java: -------------------------------------------------------------------------------- 1 | package com.yinli.yinli_ylsegmentedcontrol; 2 | 3 | 4 | import android.graphics.Color; 5 | import android.os.Bundle; 6 | import android.support.v4.app.Fragment; 7 | import android.view.Gravity; 8 | import android.view.LayoutInflater; 9 | import android.view.View; 10 | import android.view.ViewGroup; 11 | import android.widget.CompoundButton; 12 | import android.widget.RadioButton; 13 | import android.widget.RadioGroup; 14 | import android.widget.Toast; 15 | 16 | import com.yinli.ylsegmentedcontrol.YLSegmentedRadioButton; 17 | import com.yinli.ylsegmentedcontrol.YLSegmentedRadioGroup; 18 | 19 | 20 | /** 21 | * A simple {@link Fragment} subclass. 22 | */ 23 | public class Sample1Fragment extends Fragment implements RadioGroup.OnCheckedChangeListener{ 24 | 25 | 26 | public Sample1Fragment() { 27 | // Required empty public constructor 28 | } 29 | 30 | @Override 31 | public View onCreateView(LayoutInflater inflater, ViewGroup container, 32 | Bundle savedInstanceState) { 33 | // Inflate the layout for this fragment 34 | View root = inflater.inflate(R.layout.fragment_sample1, container, false); 35 | YLSegmentedRadioGroup radioGroup1 = (YLSegmentedRadioGroup) root.findViewById(R.id.radioGroup1); 36 | YLSegmentedRadioGroup radioGroup2 = (YLSegmentedRadioGroup) root.findViewById(R.id.radioGroup2); 37 | 38 | radioGroup2.setActiveColor(Color.parseColor("#36CD51")); 39 | radioGroup2.setInactiveColor(Color.LTGRAY); 40 | radioGroup2.setGravity(Gravity.CENTER); 41 | radioGroup2.setRadius(8); 42 | radioGroup2.setStrokeWidth(5); 43 | addRadioButton(radioGroup2); 44 | addRadioButton(radioGroup2); 45 | addRadioButton(radioGroup2); 46 | radioGroup2.updateGroup(); 47 | 48 | radioGroup1.setOnCheckedChangeListener(this); 49 | radioGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 50 | @Override 51 | public void onCheckedChanged(RadioGroup group, int checkedId) { 52 | Toast.makeText(getActivity(), "Button", Toast.LENGTH_SHORT).show(); 53 | } 54 | }); 55 | 56 | return root; 57 | } 58 | 59 | private void addRadioButton(YLSegmentedRadioGroup group) { 60 | YLSegmentedRadioButton radioButton = new YLSegmentedRadioButton(getActivity()); 61 | radioButton.setText("Button"); 62 | radioButton.setCompoundDrawables(getResources().getDrawable(R.drawable.ic_action_paste), null, null, null); 63 | radioButton.updateButton(); 64 | group.addView(radioButton); 65 | } 66 | 67 | @Override 68 | public void onCheckedChanged(RadioGroup group, int checkedId) { 69 | switch (checkedId) { 70 | case R.id.hButton1: 71 | Toast.makeText(getActivity(), "Button 1", Toast.LENGTH_SHORT).show(); 72 | break; 73 | case R.id.hButton2: 74 | Toast.makeText(getActivity(), "Button 2", Toast.LENGTH_SHORT).show(); 75 | break; 76 | case R.id.hButton3: 77 | Toast.makeText(getActivity(), "Button 3", Toast.LENGTH_SHORT).show(); 78 | break; 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /app/src/main/java/com/yinli/yinli_ylsegmentedcontrol/Sample2Fragment.java: -------------------------------------------------------------------------------- 1 | package com.yinli.yinli_ylsegmentedcontrol; 2 | 3 | 4 | import android.graphics.Color; 5 | import android.os.Bundle; 6 | import android.support.v4.app.Fragment; 7 | import android.view.Gravity; 8 | import android.view.LayoutInflater; 9 | import android.view.View; 10 | import android.view.ViewGroup; 11 | import android.widget.RadioGroup; 12 | import android.widget.Toast; 13 | 14 | import com.yinli.ylsegmentedcontrol.YLSegmentedRadioGroup; 15 | 16 | 17 | /** 18 | * A simple {@link Fragment} subclass. 19 | */ 20 | public class Sample2Fragment extends Fragment implements RadioGroup.OnCheckedChangeListener{ 21 | 22 | 23 | public Sample2Fragment() { 24 | // Required empty public constructor 25 | } 26 | 27 | 28 | @Override 29 | public View onCreateView(LayoutInflater inflater, ViewGroup container, 30 | Bundle savedInstanceState) { 31 | // Inflate the layout for this fragment 32 | View root = inflater.inflate(R.layout.fragment_sample2, container, false); 33 | YLSegmentedRadioGroup radioGroup1 = (YLSegmentedRadioGroup) root.findViewById(R.id.radioGroup1); 34 | radioGroup1.setOnCheckedChangeListener(this); 35 | return root; 36 | } 37 | 38 | @Override 39 | public void onCheckedChanged(RadioGroup group, int checkedId) { 40 | switch (checkedId) { 41 | case R.id.star: 42 | Toast.makeText(getActivity(), "Star", Toast.LENGTH_SHORT).show(); 43 | break; 44 | case R.id.search: 45 | Toast.makeText(getActivity(), "Search", Toast.LENGTH_SHORT).show(); 46 | break; 47 | case R.id.camera: 48 | Toast.makeText(getActivity(), "Camera", Toast.LENGTH_SHORT).show(); 49 | break; 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/facebook.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinLiWisdom/Android-YLSegmentedControl/ad2a3e63686fb111fd715db18007c8095bc6f4c3/app/src/main/res/drawable/facebook.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_action_paste.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinLiWisdom/Android-YLSegmentedControl/ad2a3e63686fb111fd715db18007c8095bc6f4c3/app/src/main/res/drawable/ic_action_paste.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_action_photo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinLiWisdom/Android-YLSegmentedControl/ad2a3e63686fb111fd715db18007c8095bc6f4c3/app/src/main/res/drawable/ic_action_photo.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_action_search.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinLiWisdom/Android-YLSegmentedControl/ad2a3e63686fb111fd715db18007c8095bc6f4c3/app/src/main/res/drawable/ic_action_search.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_action_star.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinLiWisdom/Android-YLSegmentedControl/ad2a3e63686fb111fd715db18007c8095bc6f4c3/app/src/main/res/drawable/ic_action_star.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/twitter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinLiWisdom/Android-YLSegmentedControl/ad2a3e63686fb111fd715db18007c8095bc6f4c3/app/src/main/res/drawable/twitter.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/youtube.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinLiWisdom/Android-YLSegmentedControl/ad2a3e63686fb111fd715db18007c8095bc6f4c3/app/src/main/res/drawable/youtube.png -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 8 | 9 | 14 | 15 | 16 | 17 | 22 | 23 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /app/src/main/res/layout/fragment_sample1.xml: -------------------------------------------------------------------------------- 1 | 11 | 12 | 16 | 17 | 23 | 24 | 31 | 32 | 38 | 39 | 45 | 46 | 47 | 48 | 54 | 55 | 62 | 63 | 71 | 72 | 79 | 80 | 87 | 88 | 89 | 90 | 96 | 97 | 104 | 105 | 115 | 116 | 125 | 126 | 135 | 136 | 137 | 138 | 144 | 145 | 151 | 152 | 159 | 160 | 166 | 167 | 173 | 174 | 175 | 176 | 182 | 183 | 189 | 190 | 197 | 198 | 204 | 205 | 211 | 212 | 213 | 214 | 220 | 221 | 226 | 227 | 228 | 229 | 235 | 236 | 241 | 242 | 251 | 252 | 253 | 254 | 255 | 256 | -------------------------------------------------------------------------------- /app/src/main/res/layout/fragment_sample2.xml: -------------------------------------------------------------------------------- 1 | 11 | 12 | 16 | 21 | 22 | 28 | 29 | 34 | 35 | 40 | 41 | 42 | 43 | 49 | 50 | 55 | 56 | 63 | 64 | 70 | 71 | 77 | 78 | 79 | 80 | 86 | 87 | 92 | 93 | 99 | 100 | 105 | 106 | 111 | 112 | 113 | 114 | 115 | 116 | -------------------------------------------------------------------------------- /app/src/main/res/menu/menu_main.xml: -------------------------------------------------------------------------------- 1 | 5 | 9 | 10 | 13 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinLiWisdom/Android-YLSegmentedControl/ad2a3e63686fb111fd715db18007c8095bc6f4c3/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinLiWisdom/Android-YLSegmentedControl/ad2a3e63686fb111fd715db18007c8095bc6f4c3/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinLiWisdom/Android-YLSegmentedControl/ad2a3e63686fb111fd715db18007c8095bc6f4c3/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinLiWisdom/Android-YLSegmentedControl/ad2a3e63686fb111fd715db18007c8095bc6f4c3/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinLiWisdom/Android-YLSegmentedControl/ad2a3e63686fb111fd715db18007c8095bc6f4c3/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | YLSegmentedControl Demo 3 | 4 | More 5 | Powered by Yin Li 6 | 7 | Horizontal Samples 8 | Vertical Samples 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:1.1.1' 9 | classpath 'com.github.dcendents:android-maven-plugin:1.2' 10 | classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0' 11 | // NOTE: Do not place your application dependencies here; they belong 12 | // in the individual module build.gradle files 13 | } 14 | } 15 | 16 | allprojects { 17 | repositories { 18 | jcenter() 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YinLiWisdom/Android-YLSegmentedControl/ad2a3e63686fb111fd715db18007c8095bc6f4c3/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Apr 10 15:27:10 PDT 2013 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':ylsegmentedcontrol' 2 | -------------------------------------------------------------------------------- /ylsegmentedcontrol/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /ylsegmentedcontrol/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'com.github.dcendents.android-maven' 3 | apply plugin: 'com.jfrog.bintray' 4 | 5 | version = "1.0.0" 6 | android { 7 | compileSdkVersion 21 8 | buildToolsVersion "21.1.2" 9 | resourcePrefix "ylsegmentedcontrol_" 10 | defaultConfig { 11 | minSdkVersion 9 12 | targetSdkVersion 21 13 | versionCode 1 14 | versionName "1.0" 15 | } 16 | buildTypes { 17 | release { 18 | minifyEnabled false 19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 20 | } 21 | } 22 | } 23 | 24 | dependencies { 25 | compile fileTree(dir: 'libs', include: ['*.jar']) 26 | compile 'com.android.support:appcompat-v7:21.0.3' 27 | } 28 | 29 | def siteUrl = 'https://github.com/YinLiWisdom/Android-YLSegmentedControl' // Homepage URL of the library 30 | def gitUrl = 'https://github.com/YinLiWisdom/Android-YLSegmentedControl.git' // Git repository URL 31 | group = "com.yinli" // Maven Group ID for the artifact 32 | 33 | 34 | install { 35 | repositories.mavenInstaller { 36 | // This generates POM.xml with proper parameters 37 | pom { 38 | project { 39 | packaging 'aar' 40 | 41 | // Add your description here 42 | name 'A smart custom iOS-like segmented control for Android' 43 | description 'A smart custom iOS-like segmented control for Android, which has a magical capability to handle the frame and content appearances automatically.\n' + 44 | '\n' + 45 | 'Support any color and size image (with transparent background) as an icon. The library will automatically scale the size and replace the color for the icon image to match different styles.\n' + 46 | 'Support various states such as "pressed", "checked", "unchecked" and "disabled".\n' + 47 | 'Support all types - text-only, image-only, and text with image.\n' + 48 | 'Support both horizontal and vertical modes.\n' + 49 | 'Easy to customize basic styles like active color, inactive color, length of radius, square or round, border color, etc.' 50 | url siteUrl 51 | 52 | // Set your license 53 | licenses { 54 | license { 55 | name 'The MIT License (MIT)' 56 | url 'http://www.apache.org/licenses/LICENSE-2.0.txt' 57 | } 58 | } 59 | developers { 60 | developer { 61 | id 'yinliwisdom' 62 | name 'Yin Li' 63 | email 'li.yinmax@outlook.com' 64 | } 65 | } 66 | scm { 67 | connection gitUrl 68 | developerConnection gitUrl 69 | url siteUrl 70 | 71 | } 72 | } 73 | } 74 | } 75 | } 76 | 77 | task sourcesJar(type: Jar) { 78 | from android.sourceSets.main.java.srcDirs 79 | classifier = 'sources' 80 | } 81 | 82 | task javadoc(type: Javadoc) { 83 | source = android.sourceSets.main.java.srcDirs 84 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) 85 | } 86 | 87 | task javadocJar(type: Jar, dependsOn: javadoc) { 88 | classifier = 'javadoc' 89 | from javadoc.destinationDir 90 | } 91 | artifacts { 92 | archives javadocJar 93 | archives sourcesJar 94 | } 95 | 96 | Properties properties = new Properties() 97 | properties.load(project.rootProject.file('local.properties').newDataInputStream()) 98 | 99 | bintray { 100 | user = properties.getProperty("bintray.user") 101 | key = properties.getProperty("bintray.apikey") 102 | 103 | configurations = ['archives'] 104 | pkg { 105 | repo = "maven" 106 | name = "Android-YLSegmentedControl" 107 | websiteUrl = siteUrl 108 | vcsUrl = gitUrl 109 | licenses = ["MIT"] 110 | publish = true 111 | } 112 | } -------------------------------------------------------------------------------- /ylsegmentedcontrol/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/yinli/Documents/android-sdks/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /ylsegmentedcontrol/src/androidTest/java/com/yinli/ylsegmentedcontrol/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.yinli.ylsegmentedcontrol; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /ylsegmentedcontrol/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /ylsegmentedcontrol/src/main/java/com/yinli/ylsegmentedcontrol/YLSegmentedImageRadioButton.java: -------------------------------------------------------------------------------- 1 | package com.yinli.ylsegmentedcontrol; 2 | 3 | import android.content.Context; 4 | import android.content.res.Resources; 5 | import android.content.res.TypedArray; 6 | import android.graphics.Canvas; 7 | import android.graphics.Color; 8 | import android.graphics.drawable.ColorDrawable; 9 | import android.graphics.drawable.Drawable; 10 | import android.util.AttributeSet; 11 | import android.view.Gravity; 12 | import android.widget.RadioButton; 13 | 14 | /** 15 | * Created by Yin Li on 26/03/15. 16 | */ 17 | public class YLSegmentedImageRadioButton extends RadioButton { 18 | 19 | private Resources res; 20 | private Drawable mIconDrawable; 21 | 22 | public Drawable getIconDrawable() { 23 | return mIconDrawable; 24 | } 25 | 26 | public void setIconDrawable(Drawable iconDrawable) { 27 | this.mIconDrawable = iconDrawable; 28 | invalidate(); 29 | } 30 | 31 | public YLSegmentedImageRadioButton(Context context) { 32 | this(context, null); 33 | } 34 | 35 | public YLSegmentedImageRadioButton(Context context, AttributeSet attrs) { 36 | super(context, attrs); 37 | init(context, attrs); 38 | } 39 | 40 | public YLSegmentedImageRadioButton(Context context, AttributeSet attrs, int defStyleAttr) { 41 | super(context, attrs, defStyleAttr); 42 | init(context, attrs); 43 | } 44 | 45 | private void init(Context context, AttributeSet attrs) { 46 | res = getResources(); 47 | 48 | /* Retrieve styles attributes */ 49 | TypedArray typedArray = res.obtainAttributes(attrs, R.styleable.YLSegmentedImageRadioButton); 50 | try { 51 | mIconDrawable = typedArray.getDrawable(R.styleable.YLSegmentedImageRadioButton_iconImageSrc); 52 | if (mIconDrawable == null) { 53 | throw new IllegalArgumentException("iconImageSrc must be set"); 54 | } 55 | } finally { 56 | typedArray.recycle(); 57 | } 58 | } 59 | 60 | @Override 61 | protected int[] onCreateDrawableState(int extraSpace) { 62 | return super.onCreateDrawableState(extraSpace); 63 | } 64 | 65 | @Override 66 | protected void drawableStateChanged() { 67 | super.drawableStateChanged(); 68 | 69 | if (mIconDrawable != null) { 70 | int[] myDrawableState = getDrawableState(); 71 | // Set the state of the Drawable 72 | mIconDrawable.setState(myDrawableState); 73 | invalidate(); 74 | } 75 | } 76 | 77 | @Override 78 | protected void onFinishInflate() { 79 | super.onFinishInflate(); 80 | updateButtonAppearance(); 81 | } 82 | 83 | @Override 84 | protected void onDraw(Canvas canvas) { 85 | super.onDraw(canvas); 86 | 87 | if (mIconDrawable != null) { 88 | int width = getMeasuredWidth() - getPaddingLeft() - getPaddingRight(); 89 | int height = getMeasuredHeight() - getPaddingTop() - getPaddingBottom(); 90 | int drawableWidth = mIconDrawable.getIntrinsicWidth(); 91 | int drawableHeight = mIconDrawable.getIntrinsicHeight(); 92 | float scaleRatio = 1; 93 | if (drawableWidth > width || drawableHeight > height) { 94 | scaleRatio = Math.min((float) width / (float) drawableWidth, (float) height / (float) drawableHeight); 95 | } 96 | 97 | int left = (int) ((width - drawableWidth * scaleRatio) / 2.0f) + getPaddingLeft(); 98 | int top = (int) ((height - drawableHeight * scaleRatio) / 2.0f) + getPaddingTop(); 99 | int right = left + (int) (drawableWidth * scaleRatio); 100 | int bottom = top + (int) (drawableHeight * scaleRatio); 101 | mIconDrawable.setBounds(left, top, right, bottom); 102 | mIconDrawable.draw(canvas); 103 | } 104 | } 105 | 106 | private void updateButtonAppearance() { 107 | /* Set paddings but will prevent zero-padding by assign a default value */ 108 | float verticalPadding = res.getDimension(R.dimen.default_image_radio_button_vertical_padding); 109 | float horizontalPadding = res.getDimension(R.dimen.default_image_radio_button_horizontal_padding); 110 | float paddingLeft = getPaddingLeft() <= 0 ? horizontalPadding : getPaddingLeft(); 111 | float paddingRight = getPaddingRight() <= 0 ? horizontalPadding : getPaddingRight(); 112 | float paddingTop = getPaddingTop() <= 0 ? verticalPadding : getPaddingTop(); 113 | float paddingBottom = getPaddingBottom() <= 0 ? verticalPadding : getPaddingBottom(); 114 | setPadding((int) horizontalPadding, (int) verticalPadding, (int) horizontalPadding, (int) verticalPadding); 115 | 116 | setMinHeight((int) res.getDimension(R.dimen.default_button_min_height)); 117 | setGravity(Gravity.CENTER); 118 | setButtonDrawable(new ColorDrawable((Color.TRANSPARENT))); 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /ylsegmentedcontrol/src/main/java/com/yinli/ylsegmentedcontrol/YLSegmentedRadioButton.java: -------------------------------------------------------------------------------- 1 | package com.yinli.ylsegmentedcontrol; 2 | 3 | import android.content.Context; 4 | import android.content.res.Resources; 5 | import android.graphics.Canvas; 6 | import android.graphics.Color; 7 | import android.graphics.drawable.ColorDrawable; 8 | import android.graphics.drawable.Drawable; 9 | import android.util.AttributeSet; 10 | import android.util.DisplayMetrics; 11 | import android.view.Gravity; 12 | import android.view.WindowManager; 13 | import android.widget.RadioButton; 14 | 15 | import com.yinli.ylsegmentedcontrol.utils.YLMeasureHelper; 16 | 17 | /** 18 | * Created by yinli on 22/03/15. 19 | */ 20 | public class YLSegmentedRadioButton extends RadioButton { 21 | 22 | private Context mContext; 23 | private Resources res; 24 | public YLSegmentedRadioButton(Context context) { 25 | this(context, null); 26 | } 27 | 28 | public YLSegmentedRadioButton(Context context, AttributeSet attrs) { 29 | super(context, attrs); 30 | init(context, attrs); 31 | } 32 | 33 | public YLSegmentedRadioButton(Context context, AttributeSet attrs, int defStyleAttr) { 34 | super(context, attrs, defStyleAttr); 35 | init(context, attrs); 36 | } 37 | 38 | private void init(Context context, AttributeSet attrs) { 39 | this.mContext = context; 40 | this.res = getResources(); 41 | } 42 | 43 | @Override 44 | protected void onFinishInflate() { 45 | super.onFinishInflate(); 46 | updateButton(); 47 | } 48 | 49 | @Override 50 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 51 | super.onLayout(changed, left, top, right, bottom); 52 | } 53 | 54 | @Override 55 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 56 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); 57 | 58 | } 59 | 60 | @Override 61 | protected void onDraw(Canvas canvas) { 62 | super.onDraw(canvas); 63 | 64 | } 65 | 66 | public void updateButton() { 67 | updateButtonAppearance(); 68 | resizeButtonIcon(); 69 | } 70 | 71 | private void updateButtonAppearance() { 72 | /* Set paddings but will prevent zero-padding by assign a default value */ 73 | float verticalPadding = res.getDimension(R.dimen.default_radio_button_vertical_padding); 74 | float horizontalPadding = res.getDimension(R.dimen.default_radio_button_horizontal_padding); 75 | float paddingLeft = getPaddingLeft() <= 0 ? horizontalPadding : getPaddingLeft(); 76 | float paddingRight = getPaddingRight() <= 0 ? horizontalPadding : getPaddingRight(); 77 | float paddingTop = getPaddingTop() <= 0 ? verticalPadding : getPaddingTop(); 78 | float paddingBottom = getPaddingBottom() <= 0 ? verticalPadding : getPaddingBottom(); 79 | setPadding((int) horizontalPadding, (int) verticalPadding, (int) horizontalPadding, (int) verticalPadding); 80 | 81 | setMinHeight((int) res.getDimension(R.dimen.default_button_min_height)); 82 | setGravity(Gravity.CENTER); 83 | setButtonDrawable(new ColorDrawable((Color.TRANSPARENT))); 84 | } 85 | 86 | private void resizeButtonIcon() { 87 | DisplayMetrics metrics = new DisplayMetrics(); 88 | WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); 89 | windowManager.getDefaultDisplay().getMetrics(metrics); 90 | 91 | Drawable[] results = new Drawable[4]; 92 | Drawable[] drawables = getCompoundDrawables(); 93 | 94 | for (int i = 0; i < drawables.length; i++) { 95 | if (drawables[i] != null) { 96 | float scaleFactor = (float) YLMeasureHelper.getTextBounds(getTextSize(), getText().toString()).height() * metrics.scaledDensity / (float)drawables[i].getIntrinsicHeight(); 97 | results[i] = YLMeasureHelper.scaleDrawable(res, drawables[i], scaleFactor); 98 | } 99 | } 100 | setCompoundDrawablesWithIntrinsicBounds(results[0], results[1], results[2], results[3]); 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /ylsegmentedcontrol/src/main/java/com/yinli/ylsegmentedcontrol/YLSegmentedRadioGroup.java: -------------------------------------------------------------------------------- 1 | package com.yinli.ylsegmentedcontrol; 2 | 3 | import android.content.Context; 4 | import android.content.res.ColorStateList; 5 | import android.content.res.Resources; 6 | import android.content.res.TypedArray; 7 | import android.graphics.Bitmap; 8 | import android.graphics.Canvas; 9 | import android.graphics.Paint; 10 | import android.graphics.PorterDuff; 11 | import android.graphics.PorterDuffColorFilter; 12 | import android.graphics.drawable.BitmapDrawable; 13 | import android.graphics.drawable.Drawable; 14 | import android.graphics.drawable.GradientDrawable; 15 | import android.graphics.drawable.StateListDrawable; 16 | import android.os.Build; 17 | import android.util.AttributeSet; 18 | import android.view.Gravity; 19 | import android.view.View; 20 | import android.view.ViewGroup; 21 | import android.widget.Button; 22 | import android.widget.LinearLayout; 23 | import android.widget.RadioGroup; 24 | 25 | import com.yinli.ylsegmentedcontrol.utils.YLColorHelper; 26 | 27 | /** 28 | * Created by yinli on 22/03/15. 29 | */ 30 | public class YLSegmentedRadioGroup extends RadioGroup { 31 | 32 | /* Color attributes */ 33 | private int mCheckedTextColor, mDisabledTextColor, mNormalTextColor, mPressedTextColor; 34 | private int mCheckedBackgroundColor, mDisabledBackgroundColor, mNormalBackgroundColor, mPressedBackgroundColor; 35 | private int mBorderColor; 36 | private float mStrokeWidth, mRadius; 37 | private boolean mIsRound; 38 | 39 | private Resources res; 40 | 41 | private enum ButtonType { 42 | LEFT, TOP, MIDDLE, RIGHT, BOTTOM, SINGLE 43 | } 44 | 45 | public float getStrokeWidth() { 46 | return mStrokeWidth; 47 | } 48 | 49 | public void setStrokeWidth(float strokeWidth) { 50 | this.mStrokeWidth = strokeWidth; 51 | updateGroup(); 52 | } 53 | 54 | public float getRadius() { 55 | return mRadius; 56 | } 57 | 58 | public void setRadius(float radius) { 59 | this.mRadius = radius; 60 | updateGroup(); 61 | } 62 | 63 | public boolean isIsRound() { 64 | return mIsRound; 65 | } 66 | 67 | public void setIsRound(boolean isRound) { 68 | this.mIsRound = isRound; 69 | updateGroup(); 70 | } 71 | 72 | public void setActiveColor(int color) { 73 | mNormalTextColor = color; 74 | mCheckedBackgroundColor = color; 75 | mPressedBackgroundColor = YLColorHelper.reduceColorOpacity(mCheckedBackgroundColor); 76 | mBorderColor = color; 77 | updateGroup(); 78 | } 79 | 80 | public void setInactiveColor(int color) { 81 | mCheckedTextColor = color; 82 | mPressedTextColor = color; 83 | mNormalBackgroundColor = color; 84 | updateGroup(); 85 | } 86 | 87 | public YLSegmentedRadioGroup(Context context) { 88 | this(context, null); 89 | } 90 | 91 | public YLSegmentedRadioGroup(Context context, AttributeSet attrs) { 92 | super(context, attrs); 93 | res = getResources(); 94 | /* Load default colors for text */ 95 | final int defaultCheckedTextColor = res.getColor(R.color.default_text_color_checked); 96 | final int defaultDisabledTextColor = res.getColor(R.color.default_text_color_disabled); 97 | final int defaultNormalTextColor = res.getColor(R.color.default_text_color_normal); 98 | final int defaultPressedTextColor = res.getColor(R.color.default_text_color_pressed); 99 | 100 | /* Load default colors for background */ 101 | final int defaultCheckedBackgroundColor = res.getColor(R.color.default_background_color_checked); 102 | final int defaultDisabledBackgroundColor = res.getColor(R.color.default_background_color_disabled); 103 | final int defaultNormalBackgroundColor = res.getColor(R.color.default_background_color_normal); 104 | /* Convert checked background color to pressed background color by reducing opacity */ 105 | final int defaultPressedBackgroundColor = YLColorHelper.reduceColorOpacity(defaultCheckedBackgroundColor); 106 | 107 | final int defaultBorderColor = res.getColor(R.color.default_border_color); 108 | 109 | final float defaultStrokeWidth = res.getDimension(R.dimen.default_stroke_width); 110 | 111 | final float defaultRadius = res.getDimension(R.dimen.default_corner_radius); 112 | 113 | /* Retrieve styles attributes */ 114 | TypedArray typedArray = res.obtainAttributes(attrs, R.styleable.YLSegmentedRadioGroup); 115 | try { 116 | mCheckedTextColor = typedArray.getColor(R.styleable.YLSegmentedRadioGroup_inactiveColor, defaultCheckedTextColor); 117 | mDisabledTextColor = defaultDisabledTextColor; 118 | mNormalTextColor = typedArray.getColor(R.styleable.YLSegmentedRadioGroup_activeColor, defaultNormalTextColor); 119 | mPressedTextColor = mCheckedTextColor; 120 | 121 | mCheckedBackgroundColor = typedArray.getColor(R.styleable.YLSegmentedRadioGroup_activeColor, defaultCheckedBackgroundColor); 122 | mDisabledBackgroundColor = defaultDisabledBackgroundColor; 123 | mNormalBackgroundColor = typedArray.getColor(R.styleable.YLSegmentedRadioGroup_inactiveColor, defaultNormalBackgroundColor); 124 | mPressedBackgroundColor = YLColorHelper.reduceColorOpacity(mCheckedBackgroundColor); 125 | 126 | // mBorderColor = typedArray.getColor(R.styleable.YLSegmentedRadioGroup_borderColor, defaultBorderColor); 127 | 128 | mBorderColor = mCheckedBackgroundColor; 129 | 130 | mStrokeWidth = typedArray.getDimension(R.styleable.YLSegmentedRadioGroup_borderWeight, defaultStrokeWidth); 131 | mRadius = typedArray.getDimension(R.styleable.YLSegmentedRadioGroup_radius, defaultRadius); 132 | mIsRound = typedArray.getBoolean(R.styleable.YLSegmentedRadioGroup_isRound, true); 133 | } finally { 134 | typedArray.recycle(); 135 | } 136 | init(); 137 | } 138 | 139 | private void init() { 140 | 141 | } 142 | 143 | @Override 144 | protected void onFinishInflate() { 145 | super.onFinishInflate(); 146 | updateGroup(); 147 | } 148 | 149 | public void updateGroup() { 150 | int count = getChildCount(); 151 | if (count == 1) { 152 | updateButtonAppearance(getChildAt(0), ButtonType.SINGLE, -1); 153 | } else if (count > 1) { 154 | int orientation = getOrientation(); 155 | if (orientation == LinearLayout.HORIZONTAL) { 156 | updateButtonAppearance(getChildAt(0), ButtonType.LEFT, orientation); 157 | updateButtonAppearance(getChildAt(count - 1), ButtonType.RIGHT, orientation); 158 | } else { 159 | updateButtonAppearance(getChildAt(0), ButtonType.TOP, orientation); 160 | updateButtonAppearance(getChildAt(count - 1), ButtonType.BOTTOM, orientation); 161 | } 162 | for (int i = 1; i < count - 1; i++) { 163 | updateButtonAppearance(getChildAt(i), ButtonType.MIDDLE, orientation); 164 | } 165 | } 166 | } 167 | 168 | private void updateButtonAppearance(View view, ButtonType type, int orientation) { 169 | /* Handle duplicated stroke */ 170 | int offset = (int) -mStrokeWidth; 171 | if (orientation == LinearLayout.HORIZONTAL) { 172 | LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams(); 173 | if (type == ButtonType.MIDDLE || type == ButtonType.RIGHT) { 174 | params.setMargins(offset, 0, 0, 0); 175 | } 176 | view.setLayoutParams(params); 177 | } else { 178 | LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams(); 179 | if (type == ButtonType.MIDDLE || type == ButtonType.BOTTOM) { 180 | params.setMargins(0, offset, 0, 0); 181 | } 182 | view.setLayoutParams(params); 183 | } 184 | 185 | if (view instanceof YLSegmentedImageRadioButton) { 186 | updateImageButtonIcon(view); 187 | updateButtonBackground(view, type); 188 | } else if (view instanceof YLSegmentedRadioButton) { 189 | updateButtonText(view); 190 | updateButtonIcon(view); 191 | updateButtonBackground(view, type); 192 | } 193 | } 194 | 195 | private void updateImageButtonIcon(View view) { 196 | Drawable drawable = ((YLSegmentedImageRadioButton) view).getIconDrawable(); 197 | Bitmap bitmap = drawableToBitmap(drawable); 198 | StateListDrawable stateListDrawable = new StateListDrawable(); 199 | stateListDrawable.addState(new int[]{-android.R.attr.state_pressed, -android.R.attr.state_checked}, processIconDrawable(bitmap, mNormalTextColor)); 200 | stateListDrawable.addState(new int[]{-android.R.attr.state_pressed, -android.R.attr.state_enabled}, processIconDrawable(bitmap, mDisabledTextColor)); 201 | stateListDrawable.addState(new int[]{-android.R.attr.state_pressed, android.R.attr.state_checked}, processIconDrawable(bitmap, mCheckedTextColor)); 202 | stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, processIconDrawable(bitmap, mPressedTextColor)); 203 | 204 | ((YLSegmentedImageRadioButton) view).setIconDrawable(stateListDrawable); 205 | } 206 | 207 | private void updateButtonIcon(View view) { 208 | StateListDrawable stateListDrawable[] = new StateListDrawable[4]; 209 | Drawable[] drawables = ((Button) view).getCompoundDrawables(); 210 | for (int i = 0; i < drawables.length; i++) { 211 | if (drawables[i] != null) { 212 | Bitmap bitmap = drawableToBitmap(drawables[i]); 213 | stateListDrawable[i] = new StateListDrawable(); 214 | 215 | stateListDrawable[i].addState(new int[]{-android.R.attr.state_pressed, -android.R.attr.state_checked}, processIconDrawable(bitmap, mNormalTextColor)); 216 | stateListDrawable[i].addState(new int[]{-android.R.attr.state_pressed, -android.R.attr.state_enabled}, processIconDrawable(bitmap, mDisabledTextColor)); 217 | stateListDrawable[i].addState(new int[]{-android.R.attr.state_pressed, android.R.attr.state_checked}, processIconDrawable(bitmap, mCheckedTextColor)); 218 | stateListDrawable[i].addState(new int[]{android.R.attr.state_pressed}, processIconDrawable(bitmap, mPressedTextColor)); 219 | } 220 | } 221 | ((Button) view).setCompoundDrawablesWithIntrinsicBounds(stateListDrawable[0], stateListDrawable[1], stateListDrawable[2], stateListDrawable[3]); 222 | } 223 | 224 | private Drawable processIconDrawable(Bitmap bitmap, int color) { 225 | Bitmap temp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); 226 | 227 | Canvas c = new Canvas(temp); 228 | Paint p = new Paint(); 229 | p.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN)); 230 | c.drawBitmap(bitmap, 0, 0, p); 231 | return new BitmapDrawable(res, temp); 232 | } 233 | 234 | private static Bitmap drawableToBitmap(Drawable drawable) { 235 | if (drawable instanceof BitmapDrawable) { 236 | return ((BitmapDrawable) drawable).getBitmap(); 237 | } 238 | 239 | Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); 240 | Canvas canvas = new Canvas(bitmap); 241 | drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); 242 | drawable.draw(canvas); 243 | 244 | return bitmap; 245 | } 246 | 247 | private void updateButtonText(View view) { 248 | int[][] states = new int[][]{ 249 | {-android.R.attr.state_pressed, -android.R.attr.state_checked}, 250 | {-android.R.attr.state_pressed, -android.R.attr.state_enabled}, 251 | {-android.R.attr.state_pressed, android.R.attr.state_checked}, 252 | {android.R.attr.state_pressed} 253 | }; 254 | 255 | int[] colors = new int[]{ 256 | mNormalTextColor, 257 | mDisabledTextColor, 258 | mCheckedTextColor, 259 | mPressedTextColor 260 | }; 261 | 262 | ColorStateList colorStateList = new ColorStateList(states, colors); 263 | ((Button) view).setTextColor(colorStateList); 264 | } 265 | 266 | private void updateButtonBackground(View view, ButtonType type) { 267 | 268 | GradientDrawable normalDrawable = createDrawable(type); 269 | GradientDrawable disabledDrawable = createDrawable(type); 270 | GradientDrawable pressedDrawable = createDrawable(type); 271 | GradientDrawable checkedDrawable = createDrawable(type); 272 | 273 | normalDrawable.setColor(mNormalBackgroundColor); 274 | disabledDrawable.setColor(mDisabledBackgroundColor); 275 | pressedDrawable.setColor(mPressedBackgroundColor); 276 | checkedDrawable.setColor(mCheckedBackgroundColor); 277 | 278 | StateListDrawable stateListDrawable = new StateListDrawable(); 279 | stateListDrawable.addState(new int[]{-android.R.attr.state_pressed, -android.R.attr.state_checked}, normalDrawable); 280 | stateListDrawable.addState(new int[]{-android.R.attr.state_pressed, -android.R.attr.state_enabled}, disabledDrawable); 281 | stateListDrawable.addState(new int[]{-android.R.attr.state_pressed, android.R.attr.state_checked}, checkedDrawable); 282 | stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, pressedDrawable); 283 | 284 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { 285 | view.setBackgroundDrawable(stateListDrawable); 286 | } else { 287 | view.setBackground(stateListDrawable); 288 | } 289 | } 290 | 291 | private GradientDrawable createDrawable(ButtonType type) { 292 | GradientDrawable drawable = (GradientDrawable) res.getDrawable(R.drawable.button_background).mutate(); 293 | if (mIsRound) { 294 | if (type == ButtonType.LEFT) { 295 | drawable.setCornerRadii(new float[]{mRadius, mRadius, 0, 0, 0, 0, mRadius, mRadius}); 296 | } else if (type == ButtonType.RIGHT) { 297 | drawable.setCornerRadii(new float[]{0, 0, mRadius, mRadius, mRadius, mRadius, 0, 0}); 298 | } else if (type == ButtonType.SINGLE) { 299 | drawable.setCornerRadii(new float[]{mRadius, mRadius, mRadius, mRadius, mRadius, mRadius, mRadius, mRadius}); 300 | } else if (type == ButtonType.TOP) { 301 | drawable.setCornerRadii(new float[]{mRadius, mRadius, mRadius, mRadius, 0, 0, 0, 0}); 302 | } else if (type == ButtonType.BOTTOM) { 303 | drawable.setCornerRadii(new float[]{0, 0, 0, 0, mRadius, mRadius, mRadius, mRadius}); 304 | } 305 | drawable.setStroke((int) mStrokeWidth, mBorderColor); 306 | } 307 | return drawable; 308 | } 309 | 310 | @Override 311 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 312 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); 313 | /* int balancedHeight = getMeasuredHeight(); 314 | int balancedWidth = getMeasuredWidth(); 315 | int count = getChildCount(); 316 | int orientation = getOrientation(); 317 | for (int i = 0; i < count; i++) { 318 | if (orientation == LinearLayout.HORIZONTAL) { 319 | getChildAt(i).getLayoutParams().height = balancedHeight; 320 | } else { 321 | getChildAt(i).getLayoutParams().width = balancedWidth; 322 | } 323 | 324 | }*/ 325 | } 326 | } 327 | -------------------------------------------------------------------------------- /ylsegmentedcontrol/src/main/java/com/yinli/ylsegmentedcontrol/utils/YLColorHelper.java: -------------------------------------------------------------------------------- 1 | package com.yinli.ylsegmentedcontrol.utils; 2 | 3 | import android.graphics.Color; 4 | 5 | /** 6 | * Created by Yin Li on 26/03/15. 7 | */ 8 | public class YLColorHelper { 9 | public static int reduceColorOpacity(int color) { 10 | String colorHex = String.format("%06X", 0xFFFFFF & color); 11 | int resultColor = Color.parseColor("#55" + colorHex); 12 | return resultColor; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /ylsegmentedcontrol/src/main/java/com/yinli/ylsegmentedcontrol/utils/YLMeasureHelper.java: -------------------------------------------------------------------------------- 1 | package com.yinli.ylsegmentedcontrol.utils; 2 | 3 | import android.content.res.Resources; 4 | import android.graphics.Bitmap; 5 | import android.graphics.Paint; 6 | import android.graphics.Rect; 7 | import android.graphics.drawable.BitmapDrawable; 8 | import android.graphics.drawable.Drawable; 9 | 10 | /** 11 | * Created by Yin Li on 26/03/15. 12 | */ 13 | public class YLMeasureHelper { 14 | 15 | public static Drawable scaleDrawable (Resources resources, Drawable drawable, float scaleFactor) { 16 | if ((drawable == null) || !(drawable instanceof BitmapDrawable)) { 17 | return drawable; 18 | } 19 | 20 | if (scaleFactor <= 0) { 21 | scaleFactor = 1; 22 | } 23 | 24 | Bitmap b = ((BitmapDrawable)drawable).getBitmap(); 25 | 26 | int sizeX = Math.round(drawable.getIntrinsicWidth() * scaleFactor); 27 | int sizeY = Math.round(drawable.getIntrinsicHeight() * scaleFactor); 28 | 29 | Bitmap bitmapResized = Bitmap.createScaledBitmap(b, sizeX, sizeY, false); 30 | 31 | return new BitmapDrawable(resources, bitmapResized); 32 | } 33 | 34 | public static Rect getTextBounds(float textSize, String text) { 35 | Rect bounds = new Rect(); 36 | Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 37 | paint.setTextSize(textSize); 38 | 39 | paint.getTextBounds(text, 0, text.length(), bounds); 40 | 41 | bounds.height(); 42 | return bounds; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /ylsegmentedcontrol/src/main/res/drawable/button_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /ylsegmentedcontrol/src/main/res/values/ylsc_attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /ylsegmentedcontrol/src/main/res/values/ylsc_colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #9A9A9A 4 | #559A9A9A 5 | 6 | #9A9A9A 7 | #EAEAEA 8 | -------------------------------------------------------------------------------- /ylsegmentedcontrol/src/main/res/values/ylsc_defaults.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | @android:color/white 5 | @android:color/white 6 | @color/default_disabled_text_color 7 | @color/default_main_color 8 | 9 | @color/default_main_color 10 | 11 | @color/default_main_color 12 | @color/default_secondary_color 13 | @color/default_disabled_background_color 14 | @android:color/white 15 | -------------------------------------------------------------------------------- /ylsegmentedcontrol/src/main/res/values/ylsc_dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5dp 4 | 1dp 5 | 35dp 6 | 8dp 7 | 10dp 8 | 10dp 9 | 15dp 10 | 8dp 11 | 5dp 12 | 13 | -------------------------------------------------------------------------------- /ylsegmentedcontrol/src/main/res/values/ylsc_strings.xml: -------------------------------------------------------------------------------- 1 | 2 | YLSegmentedControl 3 | 4 | -------------------------------------------------------------------------------- /ylsegmentedcontrol/ylsegmentedcontrol.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | --------------------------------------------------------------------------------