use of android.graphics.drawable.StateListDrawable in project JustAndroid by chinaltz.
the class SegmentedGroup method updateBackground.
private void updateBackground(View view) {
if (!isInEditMode()) {
int checked = mLayoutSelector.getSelected();
int unchecked = mLayoutSelector.getUnselected();
//Set text color
ColorStateList colorStateList = new ColorStateList(new int[][] { { -android.R.attr.state_checked }, { android.R.attr.state_checked } }, new int[] { mTintColor, mCheckedTextColor });
((Button) view).setTextColor(colorStateList);
//Redraw with tint color
Drawable checkedDrawable = resources.getDrawable(checked).mutate();
Drawable uncheckedDrawable = resources.getDrawable(unchecked).mutate();
((GradientDrawable) checkedDrawable).setColor(mTintColor);
((GradientDrawable) checkedDrawable).setStroke(mMarginDp, mTintColor);
((GradientDrawable) uncheckedDrawable).setStroke(mMarginDp, mTintColor);
((GradientDrawable) uncheckedDrawable).setColor(mUnCheckedTintColor);
//Set proper radius
((GradientDrawable) checkedDrawable).setCornerRadii(mLayoutSelector.getChildRadii(view));
((GradientDrawable) uncheckedDrawable).setCornerRadii(mLayoutSelector.getChildRadii(view));
GradientDrawable maskDrawable = (GradientDrawable) resources.getDrawable(unchecked).mutate();
maskDrawable.setStroke(mMarginDp, mTintColor);
maskDrawable.setColor(mUnCheckedTintColor);
maskDrawable.setCornerRadii(mLayoutSelector.getChildRadii(view));
int maskColor = Color.argb(50, Color.red(mTintColor), Color.green(mTintColor), Color.blue(mTintColor));
maskDrawable.setColor(maskColor);
LayerDrawable pressedDrawable = new LayerDrawable(new Drawable[] { uncheckedDrawable, maskDrawable });
Drawable[] drawables = { uncheckedDrawable, checkedDrawable };
TransitionDrawable transitionDrawable = new TransitionDrawable(drawables);
if (((RadioButton) view).isChecked()) {
transitionDrawable.reverseTransition(0);
}
StateListDrawable stateListDrawable = new StateListDrawable();
stateListDrawable.addState(new int[] { -android.R.attr.state_checked, android.R.attr.state_pressed }, pressedDrawable);
stateListDrawable.addState(StateSet.WILD_CARD, transitionDrawable);
mDrawableMap.put(view.getId(), transitionDrawable);
//Set button background
if (Build.VERSION.SDK_INT >= 16) {
view.setBackground(stateListDrawable);
} else {
view.setBackgroundDrawable(stateListDrawable);
}
super.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
TransitionDrawable current = mDrawableMap.get(checkedId);
current.reverseTransition(200);
if (mLastCheckId != 0) {
TransitionDrawable last = mDrawableMap.get(mLastCheckId);
if (last != null)
last.reverseTransition(200);
}
mLastCheckId = checkedId;
if (mCheckedChangeListener != null) {
mCheckedChangeListener.onCheckedChanged(group, checkedId);
}
}
});
}
}
use of android.graphics.drawable.StateListDrawable in project little-bear-dictionary by daimajia.
the class ProgressBar method tileify.
private Drawable tileify(Drawable drawable, boolean clip) {
if (drawable instanceof LayerDrawable) {
LayerDrawable background = (LayerDrawable) drawable;
final int N = background.getNumberOfLayers();
Drawable[] outDrawables = new Drawable[N];
for (int i = 0; i < N; i++) {
int id = background.getId(i);
outDrawables[i] = tileify(background.getDrawable(i), id == R.id.progress || id == R.id.secondaryProgress);
}
LayerDrawable newBg = new LayerDrawable(outDrawables);
for (int i = 0; i < N; i++) {
newBg.setId(i, background.getId(i));
}
return newBg;
} else if (drawable instanceof StateListDrawable) {
StateListDrawable in = (StateListDrawable) drawable;
StateListDrawable out = new StateListDrawable();
int numStates = ReflectHelper.invoke(in, "getStateCount", int.class);
for (int i = 0; i < numStates; i++) {
out.addState(ReflectHelper.invoke(in, "getStateSet", int[].class, i), tileify(ReflectHelper.invoke(in, "getStateDrawable", Drawable.class, i), clip));
}
return out;
} else if (drawable instanceof BitmapDrawable) {
final Bitmap tileBitmap = ((BitmapDrawable) drawable).getBitmap();
if (mSampleTile == null) {
mSampleTile = tileBitmap;
}
final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape());
final BitmapShader bitmapShader = new BitmapShader(tileBitmap, Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
shapeDrawable.getPaint().setShader(bitmapShader);
return clip ? new ClipDrawable(shapeDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL) : shapeDrawable;
}
return drawable;
}
use of android.graphics.drawable.StateListDrawable in project android_frameworks_base by crdroidandroid.
the class IconUtilities method createIconDrawable.
public Drawable createIconDrawable(Drawable src) {
Bitmap scaled = createIconBitmap(src);
StateListDrawable result = new StateListDrawable();
result.addState(new int[] { android.R.attr.state_focused }, new BitmapDrawable(createSelectedBitmap(scaled, false)));
result.addState(new int[] { android.R.attr.state_pressed }, new BitmapDrawable(createSelectedBitmap(scaled, true)));
result.addState(new int[0], new BitmapDrawable(scaled));
result.setBounds(0, 0, mIconTextureWidth, mIconTextureHeight);
return result;
}
use of android.graphics.drawable.StateListDrawable in project android-toolbox by Knickedi.
the class TabManagerView method generateDefaultTabProgrammatically.
private View generateDefaultTabProgrammatically() {
DefaultLayoutSetup setup = mDefaultLayoutSetup;
FrameLayout imageContainer = new FrameLayout(getContext());
LinearLayout.LayoutParams icp = new LinearLayout.LayoutParams(setup.image + setup.overlayOffset * 2, LayoutParams.WRAP_CONTENT);
icp.gravity = Gravity.CENTER_HORIZONTAL;
imageContainer.setLayoutParams(icp);
ProgressBar busy = new ProgressBar(getContext(), null, android.R.attr.progressBarStyleSmall);
FrameLayout.LayoutParams bp = new FrameLayout.LayoutParams(setup.overlay, setup.overlay + setup.border);
busy.setLayoutParams(bp);
busy.setId(android.R.id.icon1);
busy.setVisibility(View.GONE);
busy.setPadding(0, 0, 0, setup.border);
ImageView status = new ImageView(getContext());
FrameLayout.LayoutParams sp = new FrameLayout.LayoutParams(setup.overlay, setup.overlay + setup.border);
sp.gravity = Gravity.RIGHT | Gravity.TOP;
status.setLayoutParams(sp);
status.setId(android.R.id.icon2);
status.setVisibility(View.GONE);
status.setPadding(0, 0, 0, setup.border);
ImageView image = new ImageView(getContext());
FrameLayout.LayoutParams ip = new FrameLayout.LayoutParams(setup.image, setup.image + setup.border + setup.overlayOffset);
ip.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
image.setLayoutParams(ip);
image.setId(android.R.id.icon);
image.setVisibility(View.GONE);
image.setPadding(0, setup.overlayOffset, 0, setup.border);
imageContainer.addView(image);
imageContainer.addView(busy);
imageContainer.addView(status);
LinearLayout tab = new LinearLayout(getContext());
tab.setOrientation(LinearLayout.VERTICAL);
tab.setGravity(Gravity.BOTTOM);
tab.setPadding(setup.border, setup.border, setup.border, 0);
TextView title = new TextView(getContext());
LinearLayout.LayoutParams tp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tp.gravity = Gravity.CENTER_HORIZONTAL;
tp.bottomMargin = setup.border;
title.setLayoutParams(tp);
title.setEllipsize(TruncateAt.MARQUEE);
title.setSingleLine(true);
title.setId(android.R.id.title);
title.setVisibility(View.GONE);
title.setTextColor(new ColorStateList(new int[][] { { android.R.attr.state_selected }, { android.R.attr.state_pressed }, {} }, setup.titleColors));
StateListDrawable background = new StateListDrawable();
background.addState(new int[] { android.R.attr.state_selected }, new GradientDrawable(setup.selectedOrientation, setup.selectedColors));
background.addState(new int[] { android.R.attr.state_pressed }, new GradientDrawable(setup.pressedOrientation, setup.pressedColors));
background.addState(new int[] {}, new GradientDrawable(setup.normalOrientation, setup.noramlColors));
tab.addView(imageContainer);
tab.addView(title);
tab.setBackgroundDrawable(background);
return tab;
}
use of android.graphics.drawable.StateListDrawable in project android-toolbox by Knickedi.
the class HiddenQuickActionSetup method setupQuickActionTouchListener.
private void setupQuickActionTouchListener() {
mTouchListener = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int a = event.getAction();
if (a == MotionEvent.ACTION_DOWN) {
if (isHiddenViewCovered()) {
return false;
}
Drawable drawable = ((ImageView) ((ViewGroup) v).getChildAt(0)).getDrawable();
if (drawable instanceof StateListDrawable) {
drawable = ((StateListDrawable) drawable).getCurrent();
}
mIndicatorImage.setImageDrawable(drawable);
mClickedActionView = v;
if (mIndicatorDelay == 0) {
mIndicatorStart.run();
} else if (mIndicatorDelay > 0 && ((ActionInfo) v.getTag()).description != null) {
mPopupDelayHandler.postDelayed(mIndicatorStart, mIndicatorDelay);
}
v.setPressed(true);
v.invalidate();
} else if (a == MotionEvent.ACTION_UP || a == MotionEvent.ACTION_CANCEL) {
if (a == MotionEvent.ACTION_UP) {
if (mCloseSwipeableOnQuickAction) {
closeHiddenView();
}
if (mQuickActionListener != null) {
mQuickActionListener.onQuickAction(getCurrentListView(), getCurrentSwipeableHiddenView(), getCurrentPosition(), ((ActionInfo) v.getTag()).id);
}
}
mPopupDelayHandler.removeCallbacks(mIndicatorStart);
mIndicatorPopup.dismiss();
v.setPressed(false);
v.invalidate();
}
return true;
}
};
}
Aggregations