下面列出了android.graphics.drawable.StateListDrawable#setState() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onPrepareOptionsMenu (Menu menu)
{
super.onPrepareOptionsMenu(menu);
MenuItem mi = menu.findItem(R.id.confirm);
mi.setVisible(_state.containsKey(ARG_ADD_EXISTING_LOCATION));
mi.setTitle(_state.getBoolean(ARG_ADD_EXISTING_LOCATION) ? R.string.add_container : R.string.create_new_container);
boolean enabled = checkParams();
mi.setEnabled(enabled);
@SuppressWarnings("deprecation") StateListDrawable sld = (StateListDrawable) getActivity().getResources().getDrawable(R.drawable.ic_menu_done);
if(sld!=null)
{
sld.setState(enabled ? new int[]{android.R.attr.state_enabled} : new int[0]);
mi.setIcon(sld.getCurrent());
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
void onActionDown() {
if (mUsingStyle) {
mBackgroundDrawable = getBackground();
}
if (mBackgroundDrawable instanceof StateListDrawable) {
StateListDrawable drawable = (StateListDrawable) mBackgroundDrawable;
drawable.setState(new int[]{android.R.attr.state_pressed});
} else if (Util.hasLollipop() && mBackgroundDrawable instanceof RippleDrawable) {
RippleDrawable ripple = (RippleDrawable) mBackgroundDrawable;
ripple.setState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed});
ripple.setHotspot(getMeasuredWidth() / 2, getMeasuredHeight() / 2);
ripple.setVisible(true, true);
}
// setPressed(true);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
void onActionUp() {
if (mUsingStyle) {
mBackgroundDrawable = getBackground();
}
if (mBackgroundDrawable instanceof StateListDrawable) {
StateListDrawable drawable = (StateListDrawable) mBackgroundDrawable;
drawable.setState(new int[]{});
} else if (Util.hasLollipop() && mBackgroundDrawable instanceof RippleDrawable) {
RippleDrawable ripple = (RippleDrawable) mBackgroundDrawable;
ripple.setState(new int[]{});
ripple.setHotspot(getMeasuredWidth() / 2, getMeasuredHeight() / 2);
ripple.setVisible(true, true);
}
// setPressed(false);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
void onActionDown() {
if (mUsingStyle) {
mBackgroundDrawable = getBackground();
}
if (mBackgroundDrawable instanceof StateListDrawable) {
StateListDrawable drawable = (StateListDrawable) mBackgroundDrawable;
drawable.setState(new int[]{android.R.attr.state_pressed});
} else if (Util.hasLollipop() && mBackgroundDrawable instanceof RippleDrawable) {
RippleDrawable ripple = (RippleDrawable) mBackgroundDrawable;
ripple.setState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed});
ripple.setHotspot(getMeasuredWidth() / 2, getMeasuredHeight() / 2);
ripple.setVisible(true, true);
}
setPressed(true);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
void onActionUp() {
if (mUsingStyle) {
mBackgroundDrawable = getBackground();
}
if (mBackgroundDrawable instanceof StateListDrawable) {
StateListDrawable drawable = (StateListDrawable) mBackgroundDrawable;
drawable.setState(new int[]{});
} else if (Util.hasLollipop() && mBackgroundDrawable instanceof RippleDrawable) {
RippleDrawable ripple = (RippleDrawable) mBackgroundDrawable;
ripple.setState(new int[]{});
ripple.setHotspot(getMeasuredWidth() / 2, getMeasuredHeight() / 2);
ripple.setVisible(true, true);
}
setPressed(false);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
void onActionDown() {
if (mUsingStyle) {
mBackgroundDrawable = getBackground();
}
if (mBackgroundDrawable instanceof StateListDrawable) {
StateListDrawable drawable = (StateListDrawable) mBackgroundDrawable;
drawable.setState(new int[]{android.R.attr.state_pressed});
} else if (Util.hasLollipop() && mBackgroundDrawable instanceof RippleDrawable) {
RippleDrawable ripple = (RippleDrawable) mBackgroundDrawable;
ripple.setState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed});
ripple.setHotspot(getMeasuredWidth() / 2, getMeasuredHeight() / 2);
ripple.setVisible(true, true);
}
// setPressed(true);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
void onActionUp() {
if (mUsingStyle) {
mBackgroundDrawable = getBackground();
}
if (mBackgroundDrawable instanceof StateListDrawable) {
StateListDrawable drawable = (StateListDrawable) mBackgroundDrawable;
drawable.setState(new int[]{});
} else if (Util.hasLollipop() && mBackgroundDrawable instanceof RippleDrawable) {
RippleDrawable ripple = (RippleDrawable) mBackgroundDrawable;
ripple.setState(new int[]{});
ripple.setHotspot(getMeasuredWidth() / 2, getMeasuredHeight() / 2);
ripple.setVisible(true, true);
}
// setPressed(false);
}
private void setBookMarkIcon(@NonNull MenuItem item) {
TypedValue typedvalueattr = new TypedValue();
getActivity().getTheme().resolveAttribute(R.attr.menuBookmarkIcon, typedvalueattr, true);
StateListDrawable stateListDrawable = (StateListDrawable) getResources().getDrawable(typedvalueattr.resourceId);
int[] state = {item.isChecked() ? android.R.attr.state_checked : -android.R.attr.state_checked};
stateListDrawable.setState(state);
item.setIcon(stateListDrawable.getCurrent());
item.setTitle(item.isChecked() ? R.string.action_remove_book_mark : R.string.action_add_book_mark);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
void onActionDown() {
if (mBackgroundDrawable instanceof StateListDrawable) {
StateListDrawable drawable = (StateListDrawable) mBackgroundDrawable;
drawable.setState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed});
} else if (Util.hasLollipop()) {
RippleDrawable ripple = (RippleDrawable) mBackgroundDrawable;
ripple.setState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed});
ripple.setHotspot(calculateCenterX(), calculateCenterY());
ripple.setVisible(true, true);
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
void onActionUp() {
if (mBackgroundDrawable instanceof StateListDrawable) {
StateListDrawable drawable = (StateListDrawable) mBackgroundDrawable;
drawable.setState(new int[]{android.R.attr.state_enabled});
} else if (Util.hasLollipop()) {
RippleDrawable ripple = (RippleDrawable) mBackgroundDrawable;
ripple.setState(new int[]{android.R.attr.state_enabled});
ripple.setHotspot(calculateCenterX(), calculateCenterY());
ripple.setVisible(true, true);
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
void onActionDown() {
if (mBackgroundDrawable instanceof StateListDrawable) {
StateListDrawable drawable = (StateListDrawable) mBackgroundDrawable;
drawable.setState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed});
} else if (Util.hasLollipop()) {
RippleDrawable ripple = (RippleDrawable) mBackgroundDrawable;
ripple.setState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed});
ripple.setHotspot(calculateCenterX(), calculateCenterY());
ripple.setVisible(true, true);
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
void onActionUp() {
if (mBackgroundDrawable instanceof StateListDrawable) {
StateListDrawable drawable = (StateListDrawable) mBackgroundDrawable;
drawable.setState(new int[]{android.R.attr.state_enabled});
} else if (Util.hasLollipop()) {
RippleDrawable ripple = (RippleDrawable) mBackgroundDrawable;
ripple.setState(new int[]{android.R.attr.state_enabled});
ripple.setHotspot(calculateCenterX(), calculateCenterY());
ripple.setVisible(true, true);
}
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
if (mImageView != null) {
Drawable d = mImageView.getDrawable();
if (d instanceof StateListDrawable) {
StateListDrawable sld = (StateListDrawable) d;
sld.setState(getDrawableState());
sld.invalidateSelf();
}
}
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
Drawable d = getDrawable();
if (d instanceof StateListDrawable) {
StateListDrawable sld = (StateListDrawable) d;
sld.setState(getDrawableState());
sld.invalidateSelf();
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
void onActionDown() {
if (mBackgroundDrawable instanceof StateListDrawable) {
StateListDrawable drawable = (StateListDrawable) mBackgroundDrawable;
drawable.setState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed});
} else if (Util.hasLollipop()) {
RippleDrawable ripple = (RippleDrawable) mBackgroundDrawable;
ripple.setState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed});
ripple.setHotspot(calculateCenterX(), calculateCenterY());
ripple.setVisible(true, true);
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
void onActionUp() {
if (mBackgroundDrawable instanceof StateListDrawable) {
StateListDrawable drawable = (StateListDrawable) mBackgroundDrawable;
drawable.setState(new int[]{android.R.attr.state_enabled});
} else if (Util.hasLollipop()) {
RippleDrawable ripple = (RippleDrawable) mBackgroundDrawable;
ripple.setState(new int[]{android.R.attr.state_enabled});
ripple.setHotspot(calculateCenterX(), calculateCenterY());
ripple.setVisible(true, true);
}
}
public void setState(int [] state) {
if (mDrawable instanceof StateListDrawable) {
StateListDrawable d = (StateListDrawable) mDrawable;
d.setState(state);
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mFirstDraw) drawBackground();
if (mBackgroundDrawable != null) mBackgroundDrawable.draw(canvas);
if (isInEditMode()) return;
Drawable itemDefault, itemSlide;
StateListDrawable stateListDrawable;
if (!mSlide) {
int distance, minIndex = 0, minDistance = Integer.MAX_VALUE;
for (int i = 0; i < getCount(); i++) {
distance = Math.abs(mModIsHorizontal ? mAnchor[i][0] - mCurrentX : mAnchor[i][1] - mCurrentY);
if (minDistance > distance) {
minIndex = i;
minDistance = distance;
}
}
setCurrentItem(minIndex);
stateListDrawable = mAdapter.getItem(minIndex);
} else {
mSlide = false;
mCurrentX = mAnchor[mCurrentItem][0];
mCurrentY = mAnchor[mCurrentItem][1];
if (mFirstDraw) {
mSlideX = mLastX = mCurrentX;
}
stateListDrawable = mAdapter.getItem(mCurrentItem);
mIsFirstSelect = true;
}
stateListDrawable.setState(mState);
itemDefault = stateListDrawable.getCurrent();
for (int i = 0; i < getCount(); i++) {
if (i == mCurrentItem) {
// continue; //
mPaint.setColor(mAdapter.getTextColor(mCurrentItem));
canvas.drawText(mAdapter.getText(i), mAnchor[i][0], mAnchor[i][1] + mAnchorHeight * 3 / 2 + mTextMargin, mPaint);
}else {
mPaint.setColor(mTextColor);
canvas.drawText(mAdapter.getText(i), mAnchor[i][0], mAnchor[i][1] + mAnchorHeight * 3 / 2 + mTextMargin, mPaint);
}
stateListDrawable = mAdapter.getItem(i);
stateListDrawable.setState(STATE_NORMAL);
itemSlide = stateListDrawable.getCurrent();
itemSlide.setBounds(
mAnchor[i][0] - mPlaceHolderWidth,
mAnchor[i][1] - mPlaceHolderHeight,
mAnchor[i][0] + mPlaceHolderWidth,
mAnchor[i][1] + mPlaceHolderHeight
);
itemSlide.draw(canvas);
}
itemDefault.setBounds(
mSlideX - mAnchorWidth,
mPivotY + mAbsoluteY / 2 - mAnchorHeight,
mSlideX + mAnchorWidth,
mPivotY + mAbsoluteY / 2 + mAnchorHeight
);
itemDefault.draw(canvas);
setFirstDraw(false);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mFirstDraw) configure();
if (mBackgroundDrawable != null) mBackgroundDrawable.draw(canvas);
if (isInEditMode()) return;
Drawable itemOff;
Drawable itemOn;
StateListDrawable stateListDrawable;
int count = getCount();
if (!mUpdateFromPosition) {
int distance;
int minIndex = 0;
int minDistance = Integer.MAX_VALUE;
for (int i = 0; i < count; i++) {
distance = Math.abs(mModeIsHorizontal ? mAnchors[i][0] - mCurrentX : mAnchors[i][1] - mCurrentY);
if (minDistance > distance) {
minIndex = i;
minDistance = distance;
}
}
setCurrentItem(minIndex);
stateListDrawable = mAdapter.getItem(minIndex);
} else {
mUpdateFromPosition = false;
mCurrentX = mAnchors[mCurrentItem][0];
mCurrentY = mAnchors[mCurrentItem][1];
stateListDrawable = mAdapter.getItem(mCurrentItem);
}
stateListDrawable.setState(mState);
itemOn = stateListDrawable.getCurrent();
for (int i = 0; i < count; i++) {
if (!mDrawOnOff && i == mCurrentItem) continue;
stateListDrawable = mAdapter.getItem(i);
stateListDrawable.setState(STATE_NORMAL);
itemOff = stateListDrawable.getCurrent();
itemOff.setBounds(
mAnchors[i][0] - mItemHalfWidth,
mAnchors[i][1] - mItemHalfHeight,
mAnchors[i][0] + mItemHalfWidth,
mAnchors[i][1] + mItemHalfHeight);
itemOff.draw(canvas);
}
itemOn.setBounds(
mCurrentX - mItemHalfWidth,
mCurrentY - mItemHalfHeight,
mCurrentX + mItemHalfWidth,
mCurrentY + mItemHalfHeight);
itemOn.draw(canvas);
setFirstDraw(false);
}