Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make invisible work #37

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

buildscript {
repositories {
mavenLocal()
mavenCentral()
maven {
url "https://oss.sonatype.org/content/repositories/snapshots"
}
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.1'
classpath project.ANDROID_GRADLE_TOOLS_VERSION
}
}

Expand All @@ -21,12 +22,12 @@ allprojects {
group = GROUP

repositories {
mavenCentral()
mavenLocal()
mavenCentral()
maven {
url "https://oss.sonatype.org/content/repositories/snapshots"
}
}
}

apply plugin: 'android-reporting'
apply plugin: 'android-reporting'
16 changes: 9 additions & 7 deletions demo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.1'
classpath project.ANDROID_GRADLE_TOOLS_VERSION
}
}

apply plugin: 'com.android.application'

android {
compileSdkVersion 21
buildToolsVersion '22.0.1'
compileSdkVersion Integer.parseInt(project.ANDROID_BUILD_SDK_VERSION)
buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION

defaultConfig {
minSdkVersion 8
targetSdkVersion 21
minSdkVersion Integer.parseInt(project.ANDROID_BUILD_MIN_SDK_VERSION)
targetSdkVersion Integer.parseInt(project.ANDROID_BUILD_TARGET_SDK_VERSION)
versionCode 1
versionName '1.0'
versionName "1.0"
}

buildTypes {
release {
minifyEnabled false
Expand All @@ -29,7 +31,7 @@ android {
}

dependencies {
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.android.support:appcompat-v7:23.1.1'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':library')
// compile 'info.hoang8f:android-segmented:1.0.6'
Expand Down
8 changes: 3 additions & 5 deletions demo/src/main/res/layout/fragment_sample.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
android:orientation="horizontal"
segmentedgroup:sc_border_width="1dp"
segmentedgroup:sc_corner_radius="10dp"
segmentedgroup:sc_tint_color="#FFEB3B"
segmentedgroup:sc_checked_text_color="#7C4DFF">
segmentedgroup:sc_tint_color="#FFEB3B">

<RadioButton
android:id="@+id/button21"
Expand Down Expand Up @@ -185,8 +184,7 @@
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal"
segmentedgroup:sc_tint_color="@android:color/white"
segmentedgroup:sc_checked_text_color="#ff33b5e5">
segmentedgroup:sc_tint_color="@android:color/white" >

</info.hoang8f.android.segmented.SegmentedGroup>
</RelativeLayout>
Expand All @@ -213,4 +211,4 @@
</LinearLayout>

</LinearLayout>
</ScrollView>
</ScrollView>
30 changes: 30 additions & 0 deletions gradle.properties.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## Project-wide Gradle settings.
#
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
#
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx10248m -XX:MaxPermSize=256m
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
#
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
#Wed Feb 17 08:14:49 CST 2016

# 代理配置
systemProp.http.proxyHost=127.0.0.1
systemProp.http.proxyPort=1080

# Gradle 插件版本
ANDROID_GRADLE_TOOLS_VERSION=com.android.tools.build:gradle:2.0.0-beta4
# 编译SDK版本
ANDROID_BUILD_SDK_VERSION=23
# 编译目标SDK版本
ANDROID_BUILD_TARGET_SDK_VERSION=23
# 最小支持SDK版本
ANDROID_BUILD_MIN_SDK_VERSION=10
# Android编译工具版本
ANDROID_BUILD_TOOLS_VERSION=23.0.2
12 changes: 6 additions & 6 deletions library/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apply plugin: 'android-library'
apply plugin: 'com.android.library'

android {
compileSdkVersion 21
buildToolsVersion "22.0.1"
compileSdkVersion Integer.parseInt(project.ANDROID_BUILD_SDK_VERSION)
buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION

defaultConfig {
minSdkVersion 8
targetSdkVersion 21
minSdkVersion Integer.parseInt(project.ANDROID_BUILD_MIN_SDK_VERSION)
targetSdkVersion Integer.parseInt(project.ANDROID_BUILD_TARGET_SDK_VERSION)
versionCode 1
versionName "1.0"
}
Expand All @@ -23,4 +23,4 @@ dependencies {
}

// Used to push in maven
apply from: '../maven_push.gradle'
//apply from: '../maven_push.gradle'
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Build;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioGroup;

Expand All @@ -21,7 +21,7 @@ public class SegmentedGroup extends RadioGroup {
private int mMarginDp;
private Resources resources;
private int mTintColor;
private int mCheckedTextColor = Color.WHITE;
private ColorStateList mBorderColor;
private LayoutSelector mLayoutSelector;
private Float mCornerRadius;

Expand All @@ -46,18 +46,13 @@ private void initAttrs(AttributeSet attrs) {
R.styleable.SegmentedGroup_sc_border_width,
getResources().getDimension(R.dimen.radio_button_stroke_border));

mCornerRadius = typedArray.getDimension(
R.styleable.SegmentedGroup_sc_corner_radius,
getResources().getDimension(R.dimen.radio_button_conner_radius));
mCornerRadius = typedArray.getDimension(R.styleable.SegmentedGroup_sc_corner_radius,
getResources().getDimension(R.dimen.radio_button_conner_radius));

mTintColor = typedArray.getColor(
R.styleable.SegmentedGroup_sc_tint_color,
getResources().getColor(R.color.radio_button_selected_color));

mCheckedTextColor = typedArray.getColor(
R.styleable.SegmentedGroup_sc_checked_text_color,
getResources().getColor(android.R.color.white));
mTintColor = typedArray.getColor(R.styleable.SegmentedGroup_sc_tint_color,
getResources().getColor(R.color.radio_button_selected_color));

mBorderColor = typedArray.getColorStateList(R.styleable.SegmentedGroup_sc_border_color);
} finally {
typedArray.recycle();
}
Expand Down Expand Up @@ -85,50 +80,70 @@ public void setTintColor(int tintColor) {
updateBackground();
}

public void setTintColor(int tintColor, int checkedTextColor) {
mTintColor = tintColor;
mCheckedTextColor = checkedTextColor;
public void setBorderColor(ColorStateList color) {
mBorderColor = color;
updateBackground();
}

private int getLastVisibleIndex() {
for (int i = getChildCount() - 1; i >= 0; i--) {
if (getChildAt(i).getVisibility() == VISIBLE) {
return i;
}
}
return 0;
}

private int getFirstVisibleIndex() {
int count = getChildCount();
for (int i = 0; i < count; i++) {
if (getChildAt(i).getVisibility() == VISIBLE) {
return i;
}
}
return count - 1;
}

public void updateBackground() {
int count = super.getChildCount();
int lastVisible = getLastVisibleIndex();

for (int i = 0; i < count; i++) {
View child = getChildAt(i);
updateBackground(child);
if (child.getVisibility() == VISIBLE) {
updateBackground(child);
}

// If this is the last view, don't set LayoutParams
if (i == count - 1) break;
if (i == lastVisible)
break;

LayoutParams initParams = (LayoutParams) child.getLayoutParams();
LayoutParams params = new LayoutParams(initParams.width, initParams.height, initParams.weight);
LayoutParams params = (LayoutParams) child.getLayoutParams();
// Check orientation for proper margins
if (getOrientation() == LinearLayout.HORIZONTAL) {
params.setMargins(0, 0, -mMarginDp, 0);
} else {
params.setMargins(0, 0, 0, -mMarginDp);
}
child.setLayoutParams(params);
child.requestLayout();
}
}

private void updateBackground(View view) {
int checked = mLayoutSelector.getSelected();
int unchecked = mLayoutSelector.getUnselected();
//Set text color
ColorStateList colorStateList = new ColorStateList(new int[][]{
{android.R.attr.state_pressed},
{-android.R.attr.state_pressed, -android.R.attr.state_checked},
{-android.R.attr.state_pressed, android.R.attr.state_checked}},
new int[]{Color.GRAY, mTintColor, mCheckedTextColor});
((Button) view).setTextColor(colorStateList);

//Redraw with tint color
Drawable checkedDrawable = resources.getDrawable(checked).mutate();
Drawable uncheckedDrawable = resources.getDrawable(unchecked).mutate();
((GradientDrawable) checkedDrawable).setColor(mTintColor);
((GradientDrawable) checkedDrawable).setStroke(mMarginDp, mTintColor);
((GradientDrawable) uncheckedDrawable).setStroke(mMarginDp, mTintColor);
if (null != mBorderColor && VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
((GradientDrawable) uncheckedDrawable).setStroke(mMarginDp, mBorderColor);
((GradientDrawable) checkedDrawable).setStroke(mMarginDp, mBorderColor);
} else {
((GradientDrawable) checkedDrawable).setStroke(mMarginDp, mTintColor);
((GradientDrawable) uncheckedDrawable).setStroke(mMarginDp, mTintColor);
}
//Set proper radius
((GradientDrawable) checkedDrawable).setCornerRadii(mLayoutSelector.getChildRadii(view));
((GradientDrawable) uncheckedDrawable).setCornerRadii(mLayoutSelector.getChildRadii(view));
Expand Down Expand Up @@ -204,9 +219,9 @@ private void setChildRadii(int newChildren, int newChild) {
// if there is only one child provide the default radio button
if (children == 1) {
radii = rDefault;
} else if (child == 0) { //left or top
} else if (child == getFirstVisibleIndex()) { //left or top
radii = (getOrientation() == LinearLayout.HORIZONTAL) ? rLeft : rTop;
} else if (child == children - 1) { //right or bottom
} else if (child == getLastVisibleIndex()) { //right or bottom
radii = (getOrientation() == LinearLayout.HORIZONTAL) ? rRight : rBot;
} else { //middle
radii = rMiddle;
Expand All @@ -231,4 +246,4 @@ public float[] getChildRadii(View view) {
return radii;
}
}
}
}
3 changes: 1 addition & 2 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
<declare-styleable name="SegmentedGroup">
<attr name="sc_corner_radius" format="dimension" />
<attr name="sc_border_width" format="dimension" />
<attr name="sc_border_color" format="reference" />
<attr name="sc_tint_color" format="color" />
<attr name="sc_checked_text_color" format="color" />
</declare-styleable>

</resources>