From 00b91bb211089fce8ea73a50dcfef9f6661582f9 Mon Sep 17 00:00:00 2001 From: "mohsen.falahi" Date: Sat, 16 Mar 2019 19:41:52 +0330 Subject: [PATCH] rect switch box added --- build.gradle | 4 +- gradle/wrapper/gradle-wrapper.properties | 4 +- library/build.gradle | 10 ++--- .../cn/refactor/library/ApplicationTest.java | 13 ------ .../cn/refactor/library/SmoothCheckBox.java | 43 ++++++++++++++++--- library/src/main/res/values/attrs.xml | 1 + .../cn/refactor/library/ExampleUnitTest.java | 15 ------- sample/build.gradle | 12 +++--- .../smoothcheckbox/ApplicationTest.java | 13 ------ .../src/main/res/layout/activity_sample.xml | 3 +- .../smoothcheckbox/ExampleUnitTest.java | 15 ------- 11 files changed, 57 insertions(+), 76 deletions(-) delete mode 100644 library/src/androidTest/java/cn/refactor/library/ApplicationTest.java delete mode 100644 library/src/test/java/cn/refactor/library/ExampleUnitTest.java delete mode 100644 sample/src/androidTest/java/cn/refactor/smoothcheckbox/ApplicationTest.java delete mode 100644 sample/src/test/java/cn/refactor/smoothcheckbox/ExampleUnitTest.java diff --git a/build.gradle b/build.gradle index aff4f41..2d45a15 100644 --- a/build.gradle +++ b/build.gradle @@ -3,9 +3,10 @@ buildscript { repositories { jcenter() + google() } dependencies { - classpath 'com.android.tools.build:gradle:2.1.2' + classpath 'com.android.tools.build:gradle:3.3.2' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files @@ -15,6 +16,7 @@ buildscript { allprojects { repositories { jcenter() + google() } } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index c25cec5..7c30ec8 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Wed Oct 21 11:34:03 PDT 2015 +#Mon Jan 21 10:32:21 GMT+03:30 2019 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip diff --git a/library/build.gradle b/library/build.gradle index 117710f..937aefc 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -1,12 +1,12 @@ apply plugin: 'com.android.library' android { - compileSdkVersion 23 - buildToolsVersion "23.0.1" + compileSdkVersion 28 + buildToolsVersion '28.0.3' defaultConfig { minSdkVersion 14 - targetSdkVersion 23 + targetSdkVersion 26 versionCode 1 versionName "1.0" } @@ -19,6 +19,6 @@ android { } dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) - compile 'com.android.support:appcompat-v7:23.1.1' + implementation fileTree(dir: 'libs', include: ['*.jar']) + implementation 'com.android.support:appcompat-v7:28.0.0' } diff --git a/library/src/androidTest/java/cn/refactor/library/ApplicationTest.java b/library/src/androidTest/java/cn/refactor/library/ApplicationTest.java deleted file mode 100644 index 788768d..0000000 --- a/library/src/androidTest/java/cn/refactor/library/ApplicationTest.java +++ /dev/null @@ -1,13 +0,0 @@ -package cn.refactor.library; - -import android.app.Application; -import android.test.ApplicationTestCase; - -/** - * Testing Fundamentals - */ -public class ApplicationTest extends ApplicationTestCase { - public ApplicationTest() { - super(Application.class); - } -} \ No newline at end of file diff --git a/library/src/main/java/cn/refactor/library/SmoothCheckBox.java b/library/src/main/java/cn/refactor/library/SmoothCheckBox.java index 5490e8b..0cc35ab 100644 --- a/library/src/main/java/cn/refactor/library/SmoothCheckBox.java +++ b/library/src/main/java/cn/refactor/library/SmoothCheckBox.java @@ -25,6 +25,8 @@ import android.graphics.Paint; import android.graphics.Path; import android.graphics.Point; +import android.graphics.Rect; +import android.graphics.RectF; import android.os.Build; import android.os.Bundle; import android.os.Parcelable; @@ -53,15 +55,16 @@ public class SmoothCheckBox extends View implements Checkable { private static final int DEF_ANIM_DURATION = 300; private Paint mPaint, mTickPaint, mFloorPaint; + private RectF mRect, mFloorRect; private Point[] mTickPoints; private Point mCenterPoint; private Path mTickPath; - private float mLeftLineDistance, mRightLineDistance, mDrewDistance; private float mScaleVal = 1.0f, mFloorScale = 1.0f; private int mWidth, mAnimDuration, mStrokeWidth; private int mCheckedColor, mUnCheckedColor, mFloorColor, mFloorUnCheckedColor; + private boolean isRect; private boolean mChecked; private boolean mTickDrawing; @@ -95,6 +98,7 @@ private void init(AttributeSet attrs) { mCheckedColor = ta.getColor(R.styleable.SmoothCheckBox_color_checked, COLOR_CHECKED); mUnCheckedColor = ta.getColor(R.styleable.SmoothCheckBox_color_unchecked, COLOR_UNCHECKED); mStrokeWidth = ta.getDimensionPixelSize(R.styleable.SmoothCheckBox_stroke_width, CompatUtils.dp2px(getContext(), 0)); + isRect = ta.getBoolean(R.styleable.SmoothCheckBox_is_rect, false); ta.recycle(); mFloorUnCheckedColor = mFloorColor; @@ -111,6 +115,9 @@ private void init(AttributeSet attrs) { mPaint.setStyle(Paint.Style.FILL); mPaint.setColor(mCheckedColor); + mRect = new RectF(0, 0, 0, 0); + mFloorRect = new RectF(0, 0, 0, 0); + mTickPath = new Path(); mCenterPoint = new Point(); mTickPoints = new Point[3]; @@ -175,6 +182,7 @@ public void setChecked(boolean checked) { /** * checked with animation + * * @param checked checked * @param animate change with animation */ @@ -261,14 +269,37 @@ protected void onDraw(Canvas canvas) { private void drawCenter(Canvas canvas) { mPaint.setColor(mUnCheckedColor); - float radius = (mCenterPoint.x - mStrokeWidth) * mScaleVal; - canvas.drawCircle(mCenterPoint.x, mCenterPoint.y, radius, mPaint); + if (isRect) { + float v = (mCenterPoint.x - mStrokeWidth) * mScaleVal; + mRect.set( + mCenterPoint.x - v, + mCenterPoint.y - v, + mCenterPoint.x + v, + mCenterPoint.y + v + ); + canvas.drawRect(mRect, mPaint); + } else { + float radius = (mCenterPoint.x - mStrokeWidth) * mScaleVal; + canvas.drawCircle(mCenterPoint.x, mCenterPoint.y, radius, mPaint); + } } private void drawBorder(Canvas canvas) { mFloorPaint.setColor(mFloorColor); - int radius = mCenterPoint.x; - canvas.drawCircle(mCenterPoint.x, mCenterPoint.y, radius * mFloorScale, mFloorPaint); + if (isRect) { + float v = mCenterPoint.x * mFloorScale; + mFloorRect.set( + mCenterPoint.x - v, + mCenterPoint.y - v, + mCenterPoint.x + v, + mCenterPoint.y + v + ); + canvas.drawRect(mFloorRect, mFloorPaint); + } else { + int radius = mCenterPoint.x; + canvas.drawCircle(mCenterPoint.x, mCenterPoint.y, radius * mFloorScale, mFloorPaint); + } + } private void drawTick(Canvas canvas) { @@ -421,4 +452,6 @@ public void setOnCheckedChangeListener(OnCheckedChangeListener l) { public interface OnCheckedChangeListener { void onCheckedChanged(SmoothCheckBox checkBox, boolean isChecked); } + + } diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml index ec297db..0f3bcef 100644 --- a/library/src/main/res/values/attrs.xml +++ b/library/src/main/res/values/attrs.xml @@ -6,5 +6,6 @@ + diff --git a/library/src/test/java/cn/refactor/library/ExampleUnitTest.java b/library/src/test/java/cn/refactor/library/ExampleUnitTest.java deleted file mode 100644 index fca6e6f..0000000 --- a/library/src/test/java/cn/refactor/library/ExampleUnitTest.java +++ /dev/null @@ -1,15 +0,0 @@ -package cn.refactor.library; - -import org.junit.Test; - -import static org.junit.Assert.*; - -/** - * To work on unit tests, switch the Test Artifact in the Build Variants view. - */ -public class ExampleUnitTest { - @Test - public void addition_isCorrect() throws Exception { - assertEquals(4, 2 + 2); - } -} \ No newline at end of file diff --git a/sample/build.gradle b/sample/build.gradle index 73ef25d..986c2c6 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -1,13 +1,13 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 23 - buildToolsVersion "23.0.1" + compileSdkVersion 28 + buildToolsVersion '28.0.3' defaultConfig { applicationId "cn.refactor.smoothcheckbox" minSdkVersion 14 - targetSdkVersion 23 + targetSdkVersion 26 versionCode 1 versionName "1.0" } @@ -20,7 +20,7 @@ android { } dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) - compile 'com.android.support:appcompat-v7:23.1.1' - compile project(':library') + implementation fileTree(dir: 'libs', include: ['*.jar']) + implementation 'com.android.support:appcompat-v7:28.0.0' + implementation project(':library') } diff --git a/sample/src/androidTest/java/cn/refactor/smoothcheckbox/ApplicationTest.java b/sample/src/androidTest/java/cn/refactor/smoothcheckbox/ApplicationTest.java deleted file mode 100644 index 057c775..0000000 --- a/sample/src/androidTest/java/cn/refactor/smoothcheckbox/ApplicationTest.java +++ /dev/null @@ -1,13 +0,0 @@ -package cn.refactor.smoothcheckbox; - -import android.app.Application; -import android.test.ApplicationTestCase; - -/** - * Testing Fundamentals - */ -public class ApplicationTest extends ApplicationTestCase { - public ApplicationTest() { - super(Application.class); - } -} \ No newline at end of file diff --git a/sample/src/main/res/layout/activity_sample.xml b/sample/src/main/res/layout/activity_sample.xml index 8a56c0b..4de1ed0 100644 --- a/sample/src/main/res/layout/activity_sample.xml +++ b/sample/src/main/res/layout/activity_sample.xml @@ -25,7 +25,8 @@ android:layout_width="40dp" android:layout_height="40dp" android:layout_margin="5dp" - app:color_checked="#447eeb" /> + app:color_checked="#447eeb" + app:is_rect="true"/>