Created
March 11, 2016 00:56
-
-
Save msdx/7f23531c8289fd287a15 to your computer and use it in GitHub Desktop.
添加控件状态
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2015. Xi'an iRain IOT Technology Service CO., Ltd. All Rights Reserved. | |
*/ | |
package com.parkingwang.widget; | |
import android.content.Context; | |
import android.content.res.ColorStateList; | |
import android.content.res.TypedArray; | |
import android.graphics.Canvas; | |
import android.graphics.Paint; | |
import android.graphics.RectF; | |
import android.graphics.drawable.Drawable; | |
import android.graphics.drawable.StateListDrawable; | |
import android.util.AttributeSet; | |
import android.widget.Checkable; | |
import android.widget.TextView; | |
import com.parkingwang.app.R; | |
/** | |
* 锁车的View | |
* | |
* @author [email protected] | |
* @version 0.1 2015-09-17 | |
*/ | |
public class ToggleView extends TextView implements Checkable { | |
private static final int[] CHECKED_STATE_SET = {android.R.attr.state_checked}; | |
private StateListDrawable stateListDrawable; | |
private ColorStateList colorStateList; | |
private String offText; | |
private String onText; | |
private int drawableHeight; | |
private boolean isChecked; | |
private RectF rect = new RectF(); | |
private Paint paint; | |
public ToggleView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ToggleView); | |
stateListDrawable = (StateListDrawable) a.getDrawable(R.styleable.ToggleView_android_foreground); | |
colorStateList = a.getColorStateList(R.styleable.ToggleView_pwColor); | |
offText = a.getString(R.styleable.ToggleView_pwOffText); | |
onText = a.getString(R.styleable.ToggleView_pwOnText); | |
drawableHeight = a.getDimensionPixelSize(R.styleable.ToggleView_pwDrawableHeight, 0); | |
a.recycle(); | |
paint = new Paint(Paint.ANTI_ALIAS_FLAG); | |
paint.setStyle(Paint.Style.FILL); | |
paint.setStrokeCap(Paint.Cap.ROUND); | |
} | |
@Override | |
protected void onDraw(Canvas canvas) { | |
rect.set(0, 0, getWidth(), getHeight()); | |
paint.setColor(colorStateList.getColorForState(getDrawableState(), colorStateList.getDefaultColor())); | |
int radius = getHeight() / 2; | |
canvas.drawRoundRect(rect, radius, radius, paint); | |
super.onDraw(canvas); | |
} | |
@Override | |
public void setSelected(boolean selected) { | |
super.setSelected(selected); | |
} | |
@Override | |
protected void drawableStateChanged() { | |
super.drawableStateChanged(); | |
if (stateListDrawable != null) { | |
int[] myDrawableState = getDrawableState(); | |
stateListDrawable.setState(myDrawableState); | |
Drawable drawable = stateListDrawable.getCurrent(); | |
if(drawableHeight != 0) { | |
drawable.setBounds(0, 0, drawable.getIntrinsicWidth() * drawableHeight / drawable.getIntrinsicHeight(), drawableHeight); | |
} else { | |
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); | |
} | |
if (isChecked || isSelected()) { | |
setCompoundDrawables(drawable, null, null, null); | |
} else { | |
setCompoundDrawables(null, null, drawable, null); | |
} | |
} | |
setText(isChecked ? onText : offText); | |
} | |
@Override | |
protected int[] onCreateDrawableState(int extraSpace) { | |
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1); | |
if (isChecked) { | |
mergeDrawableStates(drawableState, CHECKED_STATE_SET); | |
} | |
return drawableState; | |
} | |
@Override | |
public void setChecked(boolean checked) { | |
if (isChecked != checked) { | |
isChecked = checked; | |
refreshDrawableState(); | |
} | |
} | |
@Override | |
public boolean isChecked() { | |
return isChecked; | |
} | |
@Override | |
public void toggle() { | |
setChecked(!isChecked); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment