Search in sources :

Example 71 with StateListDrawable

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);
                }
            }
        });
    }
}
Also used : TransitionDrawable(android.graphics.drawable.TransitionDrawable) RadioGroup(android.widget.RadioGroup) LayerDrawable(android.graphics.drawable.LayerDrawable) Drawable(android.graphics.drawable.Drawable) TransitionDrawable(android.graphics.drawable.TransitionDrawable) StateListDrawable(android.graphics.drawable.StateListDrawable) GradientDrawable(android.graphics.drawable.GradientDrawable) ColorStateList(android.content.res.ColorStateList) RadioButton(android.widget.RadioButton) StateListDrawable(android.graphics.drawable.StateListDrawable) GradientDrawable(android.graphics.drawable.GradientDrawable) RadioButton(android.widget.RadioButton) Button(android.widget.Button) LayerDrawable(android.graphics.drawable.LayerDrawable)

Example 72 with StateListDrawable

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;
}
Also used : Bitmap(android.graphics.Bitmap) LayerDrawable(android.graphics.drawable.LayerDrawable) LayerDrawable(android.graphics.drawable.LayerDrawable) AnimationDrawable(android.graphics.drawable.AnimationDrawable) Drawable(android.graphics.drawable.Drawable) StateListDrawable(android.graphics.drawable.StateListDrawable) ClipDrawable(android.graphics.drawable.ClipDrawable) ShapeDrawable(android.graphics.drawable.ShapeDrawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) RotateDrawable(android.graphics.drawable.RotateDrawable) ShapeDrawable(android.graphics.drawable.ShapeDrawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) StateListDrawable(android.graphics.drawable.StateListDrawable) BitmapShader(android.graphics.BitmapShader) SuppressLint(android.annotation.SuppressLint) ClipDrawable(android.graphics.drawable.ClipDrawable)

Example 73 with StateListDrawable

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;
}
Also used : Bitmap(android.graphics.Bitmap) BitmapDrawable(android.graphics.drawable.BitmapDrawable) StateListDrawable(android.graphics.drawable.StateListDrawable)

Example 74 with StateListDrawable

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;
}
Also used : FrameLayout(android.widget.FrameLayout) ColorStateList(android.content.res.ColorStateList) TextView(android.widget.TextView) ImageView(android.widget.ImageView) StateListDrawable(android.graphics.drawable.StateListDrawable) ProgressBar(android.widget.ProgressBar) LinearLayout(android.widget.LinearLayout) GradientDrawable(android.graphics.drawable.GradientDrawable)

Example 75 with StateListDrawable

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;
        }
    };
}
Also used : OnTouchListener(android.view.View.OnTouchListener) ViewGroup(android.view.ViewGroup) Drawable(android.graphics.drawable.Drawable) StateListDrawable(android.graphics.drawable.StateListDrawable) StateListDrawable(android.graphics.drawable.StateListDrawable) ImageView(android.widget.ImageView) TextView(android.widget.TextView) View(android.view.View) AdapterView(android.widget.AdapterView) MotionEvent(android.view.MotionEvent)

Aggregations

StateListDrawable (android.graphics.drawable.StateListDrawable)166 Drawable (android.graphics.drawable.Drawable)43 LayerDrawable (android.graphics.drawable.LayerDrawable)24 BitmapDrawable (android.graphics.drawable.BitmapDrawable)21 GradientDrawable (android.graphics.drawable.GradientDrawable)21 ColorDrawable (android.graphics.drawable.ColorDrawable)17 Bitmap (android.graphics.Bitmap)13 TextView (android.widget.TextView)12 ShapeDrawable (android.graphics.drawable.ShapeDrawable)11 View (android.view.View)11 AnimationDrawable (android.graphics.drawable.AnimationDrawable)9 ClipDrawable (android.graphics.drawable.ClipDrawable)9 SuppressLint (android.annotation.SuppressLint)8 ColorStateList (android.content.res.ColorStateList)8 Paint (android.graphics.Paint)8 RippleDrawable (android.graphics.drawable.RippleDrawable)7 TargetApi (android.annotation.TargetApi)6 TypedArray (android.content.res.TypedArray)6 TextPaint (android.text.TextPaint)5 Button (android.widget.Button)5