Skip to content

Commit

Permalink
让StrokeColor支持selector
Browse files Browse the repository at this point in the history
  • Loading branch information
xiandanin committed Apr 14, 2018
1 parent 3aff785 commit a283b5d
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.SeekBar;
import android.widget.Toast;

import com.gcssloop.widget.RCRelativeLayout;

public class ExampleActivity extends AppCompatActivity {
RCRelativeLayout layout;
CheckBox cb_clip_background;
CheckBox cb_circle;
SeekBar seekbar_stroke_width;
SeekBar seekbar_radius_top_left;
Expand All @@ -23,12 +26,35 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_example);
layout = (RCRelativeLayout) findViewById(R.id.rc_layout);
cb_circle = (CheckBox) findViewById(R.id.cb_circle);
cb_clip_background = (CheckBox) findViewById(R.id.cb_clip_background);
seekbar_stroke_width = (SeekBar) findViewById(R.id.seekbar_stroke_width);
seekbar_radius_top_left = (SeekBar) findViewById(R.id.seekbar_radius_top_left);
seekbar_radius_top_right = (SeekBar) findViewById(R.id.seekbar_radius_top_right);
seekbar_radius_bottom_left = (SeekBar) findViewById(R.id.seekbar_radius_bottom_left);
seekbar_radius_bottom_right = (SeekBar) findViewById(R.id.seekbar_radius_bottom_right);

//checked状态
final Toast toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
layout.setOnCheckedChangeListener(new RCRelativeLayout.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RCRelativeLayout layout, boolean isChecked) {
toast.setText("checked = " + isChecked);
toast.show();
}
});
layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
layout.toggle();
}
});
//剪裁背景
cb_clip_background.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
layout.setClipBackground(isChecked);
}
});
//圆形
cb_circle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
Expand Down Expand Up @@ -72,6 +98,7 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
});
seekbar_stroke_width.setProgress(getResources().getDimensionPixelSize(R.dimen.default_stroke_width));
cb_clip_background.setChecked(true);

}

Expand Down
6 changes: 6 additions & 0 deletions RCLayout/app/src/main/res/drawable/selector_stroke_color.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/colorCheckedColor" android:state_checked="true" />
<item android:color="@color/colorPressedColor" android:state_pressed="true" />
<item android:color="@color/colorPrimary" />
</selector>
83 changes: 38 additions & 45 deletions RCLayout/app/src/main/res/layout/activity_example.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
android:layout_width="@dimen/size_example_image"
android:layout_height="@dimen/size_example_image"
android:layout_centerHorizontal="true"
app:stroke_color="@color/colorPrimary">
android:background="@color/colorPrimary"
app:stroke_color="@drawable/selector_stroke_color">

<ImageView
android:layout_width="match_parent"
Expand All @@ -29,33 +30,40 @@
android:orientation="vertical"
android:paddingLeft="15dp">

<CheckBox
android:id="@+id/cb_circle"
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:layout_marginBottom="10dp"
android:layout_marginRight="15dp"
android:text="圆形"
android:textColor="@android:color/white" />
android:layout_marginRight="15dp">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/cb_circle">
<CheckBox
android:id="@+id/cb_clip_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="剪裁背景"
android:textColor="@android:color/white" />

<TextView
<CheckBox
android:id="@+id/cb_circle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="StrokeWidth:"
android:layout_marginLeft="15dp"
android:text="圆形"
android:textColor="@android:color/white" />
</LinearLayout>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
style="@style/tv_label_example"
android:text="StrokeWidth:" />

<SeekBar
android:id="@+id/seekbar_stroke_width"
android:layout_width="230dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
style="@style/seekbar_example" />
</RelativeLayout>

<RelativeLayout
Expand All @@ -64,16 +72,12 @@
android:layout_marginTop="10dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadiusTopLeft:"
android:textColor="@android:color/white" />
style="@style/tv_label_example"
android:text="TopLeftRadius:" />

<SeekBar
android:id="@+id/seekbar_radius_top_left"
android:layout_width="230dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
style="@style/seekbar_example" />
</RelativeLayout>

<RelativeLayout
Expand All @@ -82,16 +86,12 @@
android:layout_marginTop="10dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadiusTopRight:"
android:textColor="@android:color/white" />
style="@style/tv_label_example"
android:text="TopRightRadius:" />

<SeekBar
android:id="@+id/seekbar_radius_top_right"
android:layout_width="230dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
style="@style/seekbar_example" />
</RelativeLayout>

<RelativeLayout
Expand All @@ -100,34 +100,27 @@
android:layout_marginTop="10dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadiusTopLeft:"
android:textColor="@android:color/white" />
style="@style/tv_label_example"
android:text="TopLeftRadius:" />

<SeekBar
android:id="@+id/seekbar_radius_bottom_left"
android:layout_width="230dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
style="@style/seekbar_example" />
</RelativeLayout>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
android:layout_marginTop="10dp"
android:gravity="center_vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadiusBottomRight:"
android:textColor="@android:color/white" />
style="@style/tv_label_example"
android:text="BottomRightRadius:" />

<SeekBar
android:id="@+id/seekbar_radius_bottom_right"
android:layout_width="230dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
style="@style/seekbar_example" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
2 changes: 2 additions & 0 deletions RCLayout/app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
<color name="colorPrimary">#E39E83</color>
<color name="colorPrimaryDark">#E39E83</color>
<color name="colorAccent">#E39E83</color>
<color name="colorCheckedColor">#48B7FD</color>
<color name="colorPressedColor">#A97661</color>
</resources>
13 changes: 13 additions & 0 deletions RCLayout/app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,17 @@
<item name="colorAccent">@color/colorAccent</item>
</style>


<style name="tv_label_example">
<item name="android:layout_centerVertical">true</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">@android:color/white</item>
</style>

<style name="seekbar_example">
<item name="android:layout_width">220dp</item>
<item name="android:layout_alignParentRight">true</item>
<item name="android:layout_height">wrap_content</item>
</style>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Checkable;
import android.widget.RelativeLayout;

import com.gcssloop.widget.helper.RCHelper;
Expand All @@ -35,7 +36,11 @@
* 作用:圆角相对布局
* 作者:GcsSloop
*/
public class RCRelativeLayout extends RelativeLayout {
public class RCRelativeLayout extends RelativeLayout implements Checkable {
private final int[] CHECKED_STATE_SET = {android.R.attr.state_checked};
private boolean mChecked;
private OnCheckedChangeListener mOnCheckedChangeListener;

RCHelper mRCHelper;

public RCRelativeLayout(Context context) {
Expand Down Expand Up @@ -172,4 +177,54 @@ public int getStrokeColor() {
return mRCHelper.mStrokeColor;
}


@Override
public int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (isChecked()) {
mergeDrawableStates(drawableState, CHECKED_STATE_SET);
}
return drawableState;
}

@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
if (mRCHelper.mStrokeColorStateList != null && mRCHelper.mStrokeColorStateList.isStateful()) {
int stateColor = mRCHelper.mStrokeColorStateList.getColorForState(getDrawableState(), mRCHelper.mStrokeColor);

setStrokeColor(stateColor);
}
}

@Override
public void setChecked(boolean checked) {
if (mChecked != checked) {
mChecked = checked;
refreshDrawableState();
if (mOnCheckedChangeListener != null) {
mOnCheckedChangeListener.onCheckedChanged(this, mChecked);
}
}
}

@Override
public boolean isChecked() {
return mChecked;
}

@Override
public void toggle() {
setChecked(!mChecked);
}

public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
this.mOnCheckedChangeListener = listener;
}


public interface OnCheckedChangeListener {

void onCheckedChanged(RCRelativeLayout layout, boolean isChecked);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@


import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
Expand All @@ -49,6 +50,7 @@ public class RCHelper {
public Paint mPaint; // 画笔
public boolean mRoundAsCircle = false; // 圆形
public int mStrokeColor; // 描边颜色
public ColorStateList mStrokeColorStateList;// 描边颜色的状态
public int mStrokeWidth; // 描边半径
public boolean mClipBackground; // 是否剪裁背景
public Region mAreaRegion; // 内容区域
Expand All @@ -58,7 +60,8 @@ public class RCHelper {
public void initAttrs(Context context, AttributeSet attrs) {
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RCAttrs);
mRoundAsCircle = ta.getBoolean(R.styleable.RCAttrs_round_as_circle, false);
mStrokeColor = ta.getColor(R.styleable.RCAttrs_stroke_color, Color.WHITE);
mStrokeColor = Color.WHITE;
mStrokeColorStateList = ta.getColorStateList(R.styleable.RCAttrs_stroke_color);
mStrokeWidth = ta.getDimensionPixelSize(R.styleable.RCAttrs_stroke_width, 0);
mClipBackground = ta.getBoolean(R.styleable.RCAttrs_clip_background, false);
int roundCorner = ta.getDimensionPixelSize(R.styleable.RCAttrs_round_corner, 0);
Expand Down

0 comments on commit a283b5d

Please sign in to comment.