Skip to content

Commit

Permalink
滑动效果
Browse files Browse the repository at this point in the history
  • Loading branch information
rodgerYoung committed Aug 7, 2017
1 parent c9799c1 commit e8ec33a
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 31 deletions.
72 changes: 72 additions & 0 deletions .idea/markdown-navigator.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added 20170807103954.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
# 自定义多个多开关
最低版本4.0
# Switches
### 自定义多个多开关
---

要求 最低版本API 14(Android 4.0)版本以上

效果预览<br/>
<br/>
![image](20170807103954.gif)

### 介绍

这是一个通用多按钮切换控件,支持对按钮文字,同时包括按钮、背景、字体等颜色设置,包括点击切换事件等,帮助大家在需要多个按钮的情况下使用

### 使用



2 changes: 1 addition & 1 deletion sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ android {
buildToolsVersion '25.0.0'
defaultConfig {
applicationId "com.custom.yang"
minSdkVersion 15
minSdkVersion 16
targetSdkVersion 24
versionCode 1
versionName "1.0"
Expand Down
8 changes: 8 additions & 0 deletions sample/src/main/java/com/custom/activity/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.custom.switches.SwitchView;

Expand All @@ -13,5 +14,12 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
switchView=(SwitchView)findViewById(R.id.my_switch);
switchView.setStatus(new String[]{"备用","备用","正常"});
switchView.setOnClickStateChange(new SwitchView.OnClickStateChange() {
@Override
public void onStateChange(int state, String statusText) {
Toast.makeText(getApplicationContext(),statusText,Toast.LENGTH_LONG).show();
}
});
}
}
3 changes: 2 additions & 1 deletion sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context="com.custom.activity.MainActivity">

<com.custom.switches.SwitchView
android:id="@+id/my_switch"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_height="wrap_content"
android:background="@android:color/background_dark" />
</LinearLayout>
8 changes: 4 additions & 4 deletions switch/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
compileSdkVersion 25
buildToolsVersion "25.0.1"

defaultConfig {
minSdkVersion 14
targetSdkVersion 26
targetSdkVersion 25
versionCode 1
versionName "1.0"

Expand All @@ -26,7 +26,7 @@ dependencies {
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support:appcompat-v7:25.3.1'
testCompile 'junit:junit:4.12'
}
// 指定编码
Expand Down
94 changes: 75 additions & 19 deletions switch/src/main/java/com/custom/switches/SwitchView.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,30 @@
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

/**
* 多个开关自定义view
* Created by yang on 2016/12/18.
*/

public class SwitchView extends View {
private int textColor = 0;
private int btnColor = 0;
private int borderColor = 0;
private int textColor = 0;//文字颜色
private int btnColor = 0;//按钮颜色
private int borderColor = 0;//背景
private Context context;
private float x = 0;
private float height = 0;
private String[] status = {"你好", "你也好", "呵呵"};
private String[] status = {"", ""};
private int state = 0;
private String text = status[state];

private OnClickStateChange onClickStateChange;
public SwitchView(Context context) {
super(context);
}
Expand All @@ -37,14 +37,12 @@ public SwitchView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
this.context = context;
TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, com.custom.switches.R.styleable.SwitchView, 0, 0);
int btnColor = typedArray.getColor(R.styleable.SwitchView_btn_color, Color.parseColor("#999999"));
int borderColor = typedArray.getColor(R.styleable.SwitchView_border_color, Color.parseColor("#cccccc"));
int textColor = typedArray.getColor(R.styleable.SwitchView_text_color, Color.parseColor("#cccccc"));

int btnColor = typedArray.getColor(R.styleable.SwitchView_btn_color, Color.WHITE);
int borderColor = typedArray.getColor(R.styleable.SwitchView_border_color, Color.parseColor("#4bd763"));
int textColor = typedArray.getColor(R.styleable.SwitchView_text_color, Color.parseColor("#000000"));
this.btnColor = btnColor;
this.borderColor = borderColor;
this.textColor = textColor;

typedArray.recycle();
}

Expand All @@ -62,7 +60,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);

//默认的宽度和高度
int height = measureDimension(50, heightMeasureSpec);
int height = measureDimension(100, heightMeasureSpec);
this.height = height;
x = height / 2;
int width = height * status.length;
Expand Down Expand Up @@ -91,17 +89,28 @@ public int measureDimension(int defaultSize, int measureSpec) {
return result;
}

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
setBackgroundColor(Color.WHITE);
paint.setColor(Color.RED);
paint.setAntiAlias(true);
GradientDrawable gd = new GradientDrawable();//创建drawable
gd.setColor(borderColor);
gd.setCornerRadius(height);
setBackground(gd);

paint.setColor(borderColor);
canvas.drawCircle(x, this.getHeight() / 2, height / 2, paint);
paint.setColor(Color.WHITE);
paint.setTextSize(height / 8);

paint.setColor(btnColor);
canvas.drawCircle(x, this.getHeight() / 2, height / 2-3, paint);

paint.setColor(textColor);
paint.setTextSize(height / 4);
paint.setStrokeWidth(4);
for (int i = 0; i < status.length; i++) {
canvas.drawText(status[i], height * (i + 1) - height / 2 - text.length() * (height / 16), height / 2 + (height / 16), paint);
canvas.drawText(status[i], height * (i + 1) - height / 2 - status[i].length() * (height / 8), height / 2 + (height /8), paint);
}
}

Expand All @@ -111,7 +120,13 @@ public boolean onTouchEvent(final MotionEvent event) {
new Thread() {
@Override
public void run() {
int onState = (int) (event.getX() / height);
post(new Runnable() {
@Override
public void run() {
// Toast.makeText(context,getLeft()+"",Toast.LENGTH_LONG).show();
}
});
int onState = (int) ((event.getX()-getLeft())/ height);
if (state != onState) {
if (state > onState) {
for (int i = 0; i < (state - onState) * height; i++) {
Expand Down Expand Up @@ -142,7 +157,9 @@ public void run() {
post(new Runnable() {
@Override
public void run() {
Toast.makeText(getContext(), state + "", Toast.LENGTH_LONG).show();
if (onClickStateChange!=null){
onClickStateChange.onStateChange(state,status[state]);
}
}
});
}
Expand All @@ -151,4 +168,43 @@ public void run() {
return super.onTouchEvent(event);
}

public String[] getStatus() {
return status;
}

public void setStatus(String[] status) {
this.status = status;
}

public int getTextColor() {
return textColor;
}

public void setTextColor(int textColor) {
this.textColor = textColor;
}

public int getBtnColor() {
return btnColor;
}

public void setBtnColor(int btnColor) {
this.btnColor = btnColor;
}

public int getBorderColor() {
return borderColor;
}

public void setBorderColor(int borderColor) {
this.borderColor = borderColor;
}

public void setOnClickStateChange(OnClickStateChange onClickStateChange) {
this.onClickStateChange = onClickStateChange;
}

public interface OnClickStateChange{
void onStateChange(int state,String statusText);
}
}

0 comments on commit e8ec33a

Please sign in to comment.