List of usage examples for android.graphics.drawable StateListDrawable StateListDrawable
public StateListDrawable()
From source file:com.androguide.apkreator.MainActivity.java
private StateListDrawable getColouredTouchFeedback() { StateListDrawable states = new StateListDrawable(); states.addState(new int[] { android.R.attr.state_pressed }, new ColorDrawable(Color.parseColor(getPluginColor()))); states.addState(new int[] { android.R.attr.state_focused }, new ColorDrawable(Color.parseColor(getPluginColor()))); states.addState(new int[] {}, getResources().getDrawable(android.R.color.transparent)); return states; }
From source file:com.nickandjerry.dynamiclayoutinflator.DynamicLayoutInflator.java
@SuppressLint("NewApi") private static void applyAttributes(View view, Map<String, String> attrs, ViewGroup parent) { if (viewRunnables == null) createViewRunnables();// w w w .j a v a 2 s .c om ViewGroup.LayoutParams layoutParams = view.getLayoutParams(); int layoutRule; int marginLeft = 0, marginRight = 0, marginTop = 0, marginBottom = 0, paddingLeft = 0, paddingRight = 0, paddingTop = 0, paddingBottom = 0; boolean hasCornerRadius = false, hasCornerRadii = false; for (Map.Entry<String, String> entry : attrs.entrySet()) { String attr = entry.getKey(); if (viewRunnables.containsKey(attr)) { viewRunnables.get(attr).apply(view, entry.getValue(), parent, attrs); continue; } if (attr.startsWith("cornerRadius")) { hasCornerRadius = true; hasCornerRadii = !attr.equals("cornerRadius"); continue; } layoutRule = NO_LAYOUT_RULE; boolean layoutTarget = false; switch (attr) { case "id": String idValue = parseId(entry.getValue()); if (parent != null) { DynamicLayoutInfo info = getDynamicLayoutInfo(parent); int newId = highestIdNumberUsed++; view.setId(newId); info.nameToIdNumber.put(idValue, newId); } break; case "width": case "layout_width": switch (entry.getValue()) { case "wrap_content": layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT; break; case "fill_parent": case "match_parent": layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT; break; default: layoutParams.width = DimensionConverter.stringToDimensionPixelSize(entry.getValue(), view.getResources().getDisplayMetrics(), parent, true); break; } break; case "height": case "layout_height": switch (entry.getValue()) { case "wrap_content": layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT; break; case "fill_parent": case "match_parent": layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT; break; default: layoutParams.height = DimensionConverter.stringToDimensionPixelSize(entry.getValue(), view.getResources().getDisplayMetrics(), parent, false); break; } break; case "layout_gravity": if (parent != null && parent instanceof LinearLayout) { ((LinearLayout.LayoutParams) layoutParams).gravity = parseGravity(entry.getValue()); } else if (parent != null && parent instanceof FrameLayout) { ((FrameLayout.LayoutParams) layoutParams).gravity = parseGravity(entry.getValue()); } break; case "layout_weight": if (parent != null && parent instanceof LinearLayout) { ((LinearLayout.LayoutParams) layoutParams).weight = Float.parseFloat(entry.getValue()); } break; case "layout_below": layoutRule = RelativeLayout.BELOW; layoutTarget = true; break; case "layout_above": layoutRule = RelativeLayout.ABOVE; layoutTarget = true; break; case "layout_toLeftOf": layoutRule = RelativeLayout.LEFT_OF; layoutTarget = true; break; case "layout_toRightOf": layoutRule = RelativeLayout.RIGHT_OF; layoutTarget = true; break; case "layout_alignBottom": layoutRule = RelativeLayout.ALIGN_BOTTOM; layoutTarget = true; break; case "layout_alignTop": layoutRule = RelativeLayout.ALIGN_TOP; layoutTarget = true; break; case "layout_alignLeft": case "layout_alignStart": layoutRule = RelativeLayout.ALIGN_LEFT; layoutTarget = true; break; case "layout_alignRight": case "layout_alignEnd": layoutRule = RelativeLayout.ALIGN_RIGHT; layoutTarget = true; break; case "layout_alignParentBottom": layoutRule = RelativeLayout.ALIGN_PARENT_BOTTOM; break; case "layout_alignParentTop": layoutRule = RelativeLayout.ALIGN_PARENT_TOP; break; case "layout_alignParentLeft": case "layout_alignParentStart": layoutRule = RelativeLayout.ALIGN_PARENT_LEFT; break; case "layout_alignParentRight": case "layout_alignParentEnd": layoutRule = RelativeLayout.ALIGN_PARENT_RIGHT; break; case "layout_centerHorizontal": layoutRule = RelativeLayout.CENTER_HORIZONTAL; break; case "layout_centerVertical": layoutRule = RelativeLayout.CENTER_VERTICAL; break; case "layout_centerInParent": layoutRule = RelativeLayout.CENTER_IN_PARENT; break; case "layout_margin": marginLeft = marginRight = marginTop = marginBottom = DimensionConverter .stringToDimensionPixelSize(entry.getValue(), view.getResources().getDisplayMetrics()); break; case "layout_marginLeft": marginLeft = DimensionConverter.stringToDimensionPixelSize(entry.getValue(), view.getResources().getDisplayMetrics(), parent, true); break; case "layout_marginTop": marginTop = DimensionConverter.stringToDimensionPixelSize(entry.getValue(), view.getResources().getDisplayMetrics(), parent, false); break; case "layout_marginRight": marginRight = DimensionConverter.stringToDimensionPixelSize(entry.getValue(), view.getResources().getDisplayMetrics(), parent, true); break; case "layout_marginBottom": marginBottom = DimensionConverter.stringToDimensionPixelSize(entry.getValue(), view.getResources().getDisplayMetrics(), parent, false); break; case "padding": paddingBottom = paddingLeft = paddingRight = paddingTop = DimensionConverter .stringToDimensionPixelSize(entry.getValue(), view.getResources().getDisplayMetrics()); break; case "paddingLeft": paddingLeft = DimensionConverter.stringToDimensionPixelSize(entry.getValue(), view.getResources().getDisplayMetrics()); break; case "paddingTop": paddingTop = DimensionConverter.stringToDimensionPixelSize(entry.getValue(), view.getResources().getDisplayMetrics()); break; case "paddingRight": paddingRight = DimensionConverter.stringToDimensionPixelSize(entry.getValue(), view.getResources().getDisplayMetrics()); break; case "paddingBottom": paddingBottom = DimensionConverter.stringToDimensionPixelSize(entry.getValue(), view.getResources().getDisplayMetrics()); break; } if (layoutRule != NO_LAYOUT_RULE && parent instanceof RelativeLayout) { if (layoutTarget) { int anchor = idNumFromIdString(parent, parseId(entry.getValue())); ((RelativeLayout.LayoutParams) layoutParams).addRule(layoutRule, anchor); } else if (entry.getValue().equals("true")) { ((RelativeLayout.LayoutParams) layoutParams).addRule(layoutRule); } } } // TODO: this is a giant mess; come up with a simpler way of deciding what to draw for the background if (attrs.containsKey("background") || attrs.containsKey("borderColor")) { String bgValue = attrs.containsKey("background") ? attrs.get("background") : null; if (bgValue != null && bgValue.startsWith("@drawable/")) { view.setBackground(getDrawableByName(view, bgValue)); } else if (bgValue == null || bgValue.startsWith("#") || bgValue.startsWith("@color")) { if (view instanceof Button || attrs.containsKey("pressedColor")) { int bgColor = parseColor(view, bgValue == null ? "#00000000" : bgValue); int pressedColor; if (attrs.containsKey("pressedColor")) { pressedColor = parseColor(view, attrs.get("pressedColor")); } else { pressedColor = adjustBrightness(bgColor, 0.9f); } GradientDrawable gd = new GradientDrawable(); gd.setColor(bgColor); GradientDrawable pressedGd = new GradientDrawable(); pressedGd.setColor(pressedColor); if (hasCornerRadii) { float radii[] = new float[8]; for (int i = 0; i < CORNERS.length; i++) { String corner = CORNERS[i]; if (attrs.containsKey("cornerRadius" + corner)) { radii[i * 2] = radii[i * 2 + 1] = DimensionConverter.stringToDimension( attrs.get("cornerRadius" + corner), view.getResources().getDisplayMetrics()); } gd.setCornerRadii(radii); pressedGd.setCornerRadii(radii); } } else if (hasCornerRadius) { float cornerRadius = DimensionConverter.stringToDimension(attrs.get("cornerRadius"), view.getResources().getDisplayMetrics()); gd.setCornerRadius(cornerRadius); pressedGd.setCornerRadius(cornerRadius); } if (attrs.containsKey("borderColor")) { String borderWidth = "1dp"; if (attrs.containsKey("borderWidth")) { borderWidth = attrs.get("borderWidth"); } int borderWidthPx = DimensionConverter.stringToDimensionPixelSize(borderWidth, view.getResources().getDisplayMetrics()); gd.setStroke(borderWidthPx, parseColor(view, attrs.get("borderColor"))); pressedGd.setStroke(borderWidthPx, parseColor(view, attrs.get("borderColor"))); } StateListDrawable selector = new StateListDrawable(); selector.addState(new int[] { android.R.attr.state_pressed }, pressedGd); selector.addState(new int[] {}, gd); view.setBackground(selector); getDynamicLayoutInfo(view).bgDrawable = gd; } else if (hasCornerRadius || attrs.containsKey("borderColor")) { GradientDrawable gd = new GradientDrawable(); int bgColor = parseColor(view, bgValue == null ? "#00000000" : bgValue); gd.setColor(bgColor); if (hasCornerRadii) { float radii[] = new float[8]; for (int i = 0; i < CORNERS.length; i++) { String corner = CORNERS[i]; if (attrs.containsKey("cornerRadius" + corner)) { radii[i * 2] = radii[i * 2 + 1] = DimensionConverter.stringToDimension( attrs.get("cornerRadius" + corner), view.getResources().getDisplayMetrics()); } gd.setCornerRadii(radii); } } else if (hasCornerRadius) { float cornerRadius = DimensionConverter.stringToDimension(attrs.get("cornerRadius"), view.getResources().getDisplayMetrics()); gd.setCornerRadius(cornerRadius); } if (attrs.containsKey("borderColor")) { String borderWidth = "1dp"; if (attrs.containsKey("borderWidth")) { borderWidth = attrs.get("borderWidth"); } gd.setStroke( DimensionConverter.stringToDimensionPixelSize(borderWidth, view.getResources().getDisplayMetrics()), parseColor(view, attrs.get("borderColor"))); } view.setBackground(gd); getDynamicLayoutInfo(view).bgDrawable = gd; } else { view.setBackgroundColor(parseColor(view, bgValue)); } } } if (layoutParams instanceof ViewGroup.MarginLayoutParams) { ((ViewGroup.MarginLayoutParams) layoutParams).setMargins(marginLeft, marginTop, marginRight, marginBottom); } view.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom); view.setLayoutParams(layoutParams); }
From source file:ti.modules.titanium.ui.widget.tableview.TiTableView.java
public void enableCustomSelector() { Drawable currentSelector = getInternalListView().getSelector(); if (currentSelector != selector) { selector = new StateListDrawable(); TiTableViewSelector selectorDrawable = new TiTableViewSelector(listView); selector.addState(new int[] { android.R.attr.state_pressed }, selectorDrawable); listView.setSelector(selector);//from ww w . j av a2 s .com } }
From source file:com.uzmap.pkg.uzmodules.UISearchBar.SearchBarActivity.java
public static StateListDrawable addStateDrawable(int nomalColor, int pressColor) { StateListDrawable sd = new StateListDrawable(); sd.addState(new int[] { android.R.attr.state_pressed }, new ColorDrawable(pressColor)); sd.addState(new int[] { android.R.attr.state_focused }, new ColorDrawable(nomalColor)); sd.addState(new int[] {}, new ColorDrawable(nomalColor)); return sd;/*from ww w.j a va2 s. c o m*/ }
From source file:org.telegram.ui.ActionBar.Theme.java
public static Drawable createBarSelectorDrawable(int color, boolean masked) { Drawable drawable;// w ww . java2s . co m if (Build.VERSION.SDK_INT >= 21) { Drawable maskDrawable = null; if (masked) { maskPaint.setColor(0xffffffff); maskDrawable = new Drawable() { @Override public void draw(Canvas canvas) { android.graphics.Rect bounds = getBounds(); canvas.drawCircle(bounds.centerX(), bounds.centerY(), AndroidUtilities.dp(18), maskPaint); } @Override public void setAlpha(int alpha) { } @Override public void setColorFilter(ColorFilter colorFilter) { } @Override public int getOpacity() { return PixelFormat.UNKNOWN; } }; } ColorStateList colorStateList = new ColorStateList(new int[][] { new int[] {} }, new int[] { color }); return new RippleDrawable(colorStateList, null, maskDrawable); } else { StateListDrawable stateListDrawable = new StateListDrawable(); stateListDrawable.addState(new int[] { android.R.attr.state_pressed }, new ColorDrawable(color)); stateListDrawable.addState(new int[] { android.R.attr.state_focused }, new ColorDrawable(color)); stateListDrawable.addState(new int[] { android.R.attr.state_selected }, new ColorDrawable(color)); stateListDrawable.addState(new int[] { android.R.attr.state_activated }, new ColorDrawable(color)); stateListDrawable.addState(new int[] {}, new ColorDrawable(0x00000000)); return stateListDrawable; } }
From source file:com.javielinux.tweettopics2.TweetTopicsActivity.java
public void refreshTheme() { layoutBackgroundApp.setBackgroundColor( Color.parseColor("#" + themeManager.getStringColor("color_background_new_status"))); themeManager.setColors();//w ww . j ava 2 s . c o m layoutBackgroundBar.setBackgroundDrawable( ImageUtils.createBackgroundDrawable(this, themeManager.getColor("color_top_bar"), false, 0)); layoutBackgroundColumnsBarContainer.setBackgroundDrawable( ImageUtils.createBackgroundDrawable(this, themeManager.getColor("color_top_bar"), false, 0)); StateListDrawable statesButtonBg = new StateListDrawable(); statesButtonBg.addState(new int[] { android.R.attr.state_pressed }, ImageUtils .createBackgroundDrawable(this, themeManager.getColor("color_button_press_default"), false, 0)); statesButtonBg.addState(new int[] { -android.R.attr.state_pressed }, ImageUtils.createBackgroundDrawable(this, themeManager.getColor("color_top_bar"), false, 0)); imgBarAvatarBg.setBackgroundDrawable(statesButtonBg); StateListDrawable statesButton = new StateListDrawable(); statesButton.addState(new int[] { android.R.attr.state_pressed }, ImageUtils.createBackgroundDrawable(this, themeManager.getColor("color_button_press_default"), false, 0)); statesButton.addState(new int[] { -android.R.attr.state_pressed }, ImageUtils.createBackgroundDrawable(this, themeManager.getColor("color_top_bar"), false, 0)); imgNewStatus.setBackgroundDrawable(statesButton); float size = getResources().getDimension(R.dimen.actionbar_height) - Utils.dip2px(this, 12); imgBarCounter.setBackgroundDrawable(new BitmapDrawable(getResources(), ImageUtils.getBackgroundBitmapInBubble(this, Color.RED, Utils.TYPE_RECTANGLE, size, size))); //(findViewById(R.id.tweettopics_bar_divider1)).setBackgroundDrawable(ImageUtils.createBackgroundDrawable(this, themeManager.getColor("color_top_bar"), false, 0)); //(findViewById(R.id.tweettopics_bar_divider2)).setBackgroundDrawable(ImageUtils.createBackgroundDrawable(this, themeManager.getColor("color_top_bar"), false, 0)); Drawable d = new ColorDrawable(android.R.color.transparent); StateListDrawable states = new StateListDrawable(); states.addState(new int[] { android.R.attr.state_pressed }, d); states.addState(new int[] { android.R.attr.state_window_focused }, d); states.addState(new int[] { android.R.attr.state_pressed }, d); states.addState(new int[] { android.R.attr.state_selected }, d); states.addState(new int[] { android.R.attr.color }, new ColorDrawable(themeManager.getColor("color_indicator_text"))); indicator.setBackgroundDrawable(states); indicator.setTextSize(getResources().getDimension(R.dimen.text_size_title_page_indicator)); }
From source file:com.initialxy.cordova.themeablebrowser.ThemeableBrowser.java
private void setButtonImages(View view, BrowserButton buttonProps, int disabledAlpha) { Drawable normalDrawable = null;// w w w. j a va 2 s .c o m Drawable disabledDrawable = null; Drawable pressedDrawable = null; CharSequence description = view.getContentDescription(); if (buttonProps.image != null || buttonProps.wwwImage != null) { try { normalDrawable = getImage(buttonProps.image, buttonProps.wwwImage, buttonProps.wwwImageDensity); ViewGroup.LayoutParams params = view.getLayoutParams(); params.width = normalDrawable.getIntrinsicWidth(); params.height = normalDrawable.getIntrinsicHeight(); } catch (Resources.NotFoundException e) { emitError(ERR_LOADFAIL, String.format("Image for %s, %s, failed to load", description, buttonProps.image)); } catch (IOException ioe) { emitError(ERR_LOADFAIL, String.format("Image for %s, %s, failed to load", description, buttonProps.wwwImage)); } } else { emitWarning(WRN_UNDEFINED, String.format("Image for %s is not defined. Button will not be shown", description)); } if (buttonProps.imagePressed != null || buttonProps.wwwImagePressed != null) { try { pressedDrawable = getImage(buttonProps.imagePressed, buttonProps.wwwImagePressed, buttonProps.wwwImageDensity); } catch (Resources.NotFoundException e) { emitError(ERR_LOADFAIL, String.format("Pressed image for %s, %s, failed to load", description, buttonProps.imagePressed)); } catch (IOException e) { emitError(ERR_LOADFAIL, String.format("Pressed image for %s, %s, failed to load", description, buttonProps.wwwImagePressed)); } } else { emitWarning(WRN_UNDEFINED, String.format("Pressed image for %s is not defined.", description)); } if (normalDrawable != null) { // Create the disabled state drawable by fading the normal state // drawable. Drawable.setAlpha() stopped working above Android 4.4 // so we gotta bring out some bitmap magic. Credit goes to: // http://stackoverflow.com/a/7477572 Bitmap enabledBitmap = ((BitmapDrawable) normalDrawable).getBitmap(); Bitmap disabledBitmap = Bitmap.createBitmap(normalDrawable.getIntrinsicWidth(), normalDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(disabledBitmap); Paint paint = new Paint(); paint.setAlpha(disabledAlpha); canvas.drawBitmap(enabledBitmap, 0, 0, paint); Resources activityRes = cordova.getActivity().getResources(); disabledDrawable = new BitmapDrawable(activityRes, disabledBitmap); } StateListDrawable states = new StateListDrawable(); if (pressedDrawable != null) { states.addState(new int[] { android.R.attr.state_pressed }, pressedDrawable); } if (normalDrawable != null) { states.addState(new int[] { android.R.attr.state_enabled }, normalDrawable); } if (disabledDrawable != null) { states.addState(new int[] {}, disabledDrawable); } setBackground(view, states); }
From source file:edu.mit.viral.shen.DroidFish.java
private final void setButtonData(ImageButton button, int bWidth, int bHeight, int svgResId, SVG touched) { SVG svg = SVGParser.getSVGFromResource(getResources(), svgResId); button.setBackgroundDrawable(new SVGPictureDrawable(svg)); StateListDrawable sld = new StateListDrawable(); sld.addState(new int[] { android.R.attr.state_pressed }, new SVGPictureDrawable(touched)); button.setImageDrawable(sld);/* w ww.j a v a2 s. com*/ LayoutParams lp = button.getLayoutParams(); lp.height = bHeight; lp.width = bWidth; button.setLayoutParams(lp); button.setPadding(0, 0, 0, 0); button.setScaleType(ScaleType.FIT_XY); }