Example usage for android.graphics.drawable Drawable isStateful

List of usage examples for android.graphics.drawable Drawable isStateful

Introduction

In this page you can find the example usage for android.graphics.drawable Drawable isStateful.

Prototype

public boolean isStateful() 

Source Link

Document

Indicates whether this drawable will change its appearance based on state.

Usage

From source file:com.landenlabs.all_devtool.IconBaseFragment.java

/**
 * Display icon (drawable) information/*from  w  w  w  .j  a  va  2 s  .c o  m*/
 *
 * @param iconInfo
 */
private void showIconDialog(IconInfo iconInfo) {
    Drawable iconD = iconInfo.getDrawable();
    String iconType = iconD.getClass().getSimpleName();

    LayoutInflater inflater = m_context.getLayoutInflater();
    final View dialogLayout = inflater.inflate(R.layout.icon_dlg, null);

    View shareBtn = dialogLayout.findViewById(R.id.icon_dlg_share);
    shareBtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Utils.shareScreen(dialogLayout, "iconDetail", null);
        }
    });

    final TextView imageName = Ui.viewById(dialogLayout, R.id.icon_dlg_name);
    final TextView imageSize = Ui.viewById(dialogLayout, R.id.icon_dlg_size);
    final TextView imageType = Ui.viewById(dialogLayout, R.id.icon_dlg_type);
    final TextView imageExtra = Ui.viewById(dialogLayout, R.id.icon_dlg_extra);

    imageName.setText(iconInfo.fieldStr());
    imageSize.setText(String.format("Size: %d x %d", iconD.getIntrinsicWidth(), iconD.getIntrinsicHeight()));
    imageType.setText(iconType);

    final ImageView imageView = Ui.viewById(dialogLayout, R.id.icon_dlg_image);
    // imageView.setImageDrawable(iconD);
    boolean hasStates = iconD.isStateful();

    final View stateTitle = dialogLayout.findViewById(R.id.icon_dlg_state_title);
    stateTitle.setVisibility(hasStates ? View.VISIBLE : View.GONE);

    final TableRow row1 = Ui.viewById(dialogLayout, R.id.icon_dlg_state_row1);
    row1.removeAllViews();

    final TableRow row2 = Ui.viewById(dialogLayout, R.id.icon_dlg_state_row2);
    row2.removeAllViews();

    boolean showRows = false;
    String extraInfo = "";

    if (hasStates) {
        extraInfo = "StateFul";
        showRows = true;

        StateListDrawable stateListDrawable = (StateListDrawable) iconD;
        Set<Drawable> stateIcons = new HashSet<Drawable>();
        showStateIcon(imageView, row1, row2, stateListDrawable, android.R.attr.state_enabled, "Enabled",
                stateIcons);
        showStateIcon(imageView, row1, row2, stateListDrawable, android.R.attr.state_pressed, "Pressed",
                stateIcons);
        showStateIcon(imageView, row1, row2, stateListDrawable, android.R.attr.state_checked, "Checked",
                stateIcons);
        showStateIcon(imageView, row1, row2, stateListDrawable, android.R.attr.state_selected, "Selected",
                stateIcons);
    }

    if (iconType.equals(LayerDrawable.class.getSimpleName())) {
        showRows = true;
        LayerDrawable layerDrawable = (LayerDrawable) iconD;
        int layerCnt = layerDrawable.getNumberOfLayers();
        extraInfo = String.format(Locale.getDefault(), "Layers:%d", layerCnt);
        for (int layerIdx = 0; layerIdx < Math.min(layerCnt, 3); layerIdx++) {
            showLayerIcon(imageView, row1, row2, layerDrawable.getDrawable(layerIdx), layerIdx);
        }
    } else if (iconType.equals(AnimationDrawable.class.getSimpleName())) {
        final AnimationDrawable animationDrawable = (AnimationDrawable) iconD;
        extraInfo = String.format(Locale.getDefault(), "Frames:%d", animationDrawable.getNumberOfFrames());
        showRows = true;
        showAnimationBtns(imageView, animationDrawable, row1, row2);

        // Can't control animation at this time, drawable not rendered yet.
        // animationDrawable.stop();
    }

    row1.setVisibility(showRows ? View.VISIBLE : View.GONE);
    row2.setVisibility(showRows ? View.VISIBLE : View.GONE);

    imageExtra.setText(extraInfo);
    imageView.setImageDrawable(iconD);

    dialogLayout.findViewById(R.id.icon_dlg_whiteBtn).setOnClickListener(new View.OnClickListener() {
        @SuppressWarnings("deprecation")
        @Override
        public void onClick(View v) {
            imageView.setBackgroundDrawable(v.getBackground());
        }
    });

    dialogLayout.findViewById(R.id.icon_dlg_grayBtn).setOnClickListener(new View.OnClickListener() {
        @SuppressWarnings("deprecation")
        @Override
        public void onClick(View v) {
            imageView.setBackgroundDrawable(v.getBackground());
        }
    });

    dialogLayout.findViewById(R.id.icon_dlg_blackBtn).setOnClickListener(new View.OnClickListener() {
        @SuppressWarnings("deprecation")
        @Override
        public void onClick(View v) {
            imageView.setBackgroundDrawable(v.getBackground());
        }
    });

    dialogLayout.findViewById(R.id.icon_dlg_squaresBtn).setOnClickListener(new View.OnClickListener() {
        @SuppressWarnings("deprecation")
        @Override
        public void onClick(View v) {
            imageView.setBackgroundDrawable(v.getBackground());
        }
    });

    AlertDialog.Builder builder = new AlertDialog.Builder(m_context);
    builder.setView(dialogLayout);

    builder.setMessage("Icon").setCancelable(false).setPositiveButton("Close",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // if this button is clicked, close
                    dialog.cancel();
                }
            });

    builder.show();
}

From source file:administrator.example.com.myscrollview.VerticalViewPager.java

@Override
protected void drawableStateChanged() {
    super.drawableStateChanged();
    final Drawable d = mMarginDrawable;
    if (d != null && d.isStateful()) {
        d.setState(getDrawableState());/*  w ww  . jav a2s.  c o m*/
    } /* end of if */
}

From source file:cnedu.ustcjd.widget.MultiSlider.java

private void setThumbDrawable(Thumb thumb, Drawable thumbDrawable, int thumbColor) {
    requireNonNull(thumbDrawable);//w ww .  j a va2 s  .  com
    Drawable nThumbDrawable = getTintedDrawable(thumbDrawable.getConstantState().newDrawable(), thumbColor);
    nThumbDrawable.setCallback(this);

    // Assuming the thumb drawable is symmetric, set the thumb offset
    // such that the thumb will hang halfway off either edge of the
    // progress bar.
    thumb.setThumbOffset(thumbDrawable.getIntrinsicWidth() / 2);

    // If we're updating get the new states
    if (thumb.getThumb() != null && (nThumbDrawable.getIntrinsicWidth() != thumb.getThumb().getIntrinsicWidth()
            || nThumbDrawable.getIntrinsicHeight() != thumb.getThumb().getIntrinsicHeight())) {
        requestLayout();
    }
    thumb.setThumb(nThumbDrawable);

    invalidate();
    if (nThumbDrawable != null && nThumbDrawable.isStateful()) {
        // Note that if the states are different this won't work.
        // For now, let's consider that an app bug.
        int[] state = getDrawableState();
        nThumbDrawable.setState(state);
    }
}

From source file:VerticalViewPager.java

@Override
protected void drawableStateChanged() {
    super.drawableStateChanged();
    final Drawable d = mMarginDrawable;
    if (d != null && d.isStateful()) {
        d.setState(getDrawableState());/*from w w  w. java 2  s.  c  o m*/
    }
}

From source file:android.support.design.widget.CoordinatorLayout.java

@Override
protected void drawableStateChanged() {
    super.drawableStateChanged();

    final int[] state = getDrawableState();
    boolean changed = false;

    Drawable d = mStatusBarBackground;
    if (d != null && d.isStateful()) {
        changed |= d.setState(state);/*  ww w  .  ja v  a2s.  c om*/
    }

    if (changed) {
        invalidate();
    }
}

From source file:example.luojing.androidsourceanalysis.ViewPager.java

/**
 * drawable???,?PageMarginDrawable//from  w  ww  .  j av  a2 s  .  c  om
 */
@Override
protected void drawableStateChanged() {
    super.drawableStateChanged();
    final Drawable d = mMarginDrawable;
    if (d != null && d.isStateful()) {
        d.setState(getDrawableState());
    }
}

From source file:io.apptik.widget.MultiSlider.java

/**
 * Sets the thumb drawable for all thumbs
 * <p/>//www.ja v a 2  s.co m
 * If the thumb is a valid drawable (i.e. not null), half its width will be
 * used as the new thumb offset (@see #setThumbOffset(int)).
 *
 * @param thumb Drawable representing the thumb
 */
private void setThumbDrawables(Drawable thumb, Drawable range, Drawable range1, Drawable range2) {
    if (thumb == null)
        return;
    boolean needUpdate;
    Drawable rangeDrawable;

    // This way, calling setThumbDrawables again with the same bitmap will result in
    // it recalculating thumbOffset (if for example it the bounds of the
    // drawable changed)
    int curr = 0;
    int padding = 0;
    for (Thumb mThumb : mThumbs) {
        curr++;
        if (mThumb.getThumb() != null && thumb != mThumb.getThumb()) {
            mThumb.getThumb().setCallback(null);
            needUpdate = true;
        } else {
            needUpdate = false;
        }

        if (curr == 1 && range1 != null) {
            rangeDrawable = getTintedDrawable(range1,
                    a.getColor(io.apptik.widget.R.styleable.MultiSlider_range1Color, 0));
        } else if (curr == 2 && range2 != null) {
            rangeDrawable = getTintedDrawable(range2,
                    a.getColor(io.apptik.widget.R.styleable.MultiSlider_range2Color, 0));
        } else {
            rangeDrawable = getTintedDrawable(range.getConstantState().newDrawable(),
                    a.getColor(io.apptik.widget.R.styleable.MultiSlider_rangeColor, 0));
        }

        mThumb.setRange(rangeDrawable);

        Drawable newDrawable = getTintedDrawable(thumb.getConstantState().newDrawable(),
                a.getColor(io.apptik.widget.R.styleable.MultiSlider_thumbColor, 0));
        newDrawable.setCallback(this);

        // Assuming the thumb drawable is symmetric, set the thumb offset
        // such that the thumb will hang halfway off either edge of the
        // progress bar.
        mThumb.setThumbOffset(thumb.getIntrinsicWidth() / 2);

        padding = Math.max(padding, mThumb.getThumbOffset());
        // If we're updating get the new states
        if (needUpdate && (newDrawable.getIntrinsicWidth() != mThumb.getThumb().getIntrinsicWidth()
                || newDrawable.getIntrinsicHeight() != mThumb.getThumb().getIntrinsicHeight())) {
            requestLayout();
        }
        mThumb.setThumb(newDrawable);

        if (needUpdate) {
            invalidate();
            if (thumb != null && thumb.isStateful()) {
                // Note that if the states are different this won't work.
                // For now, let's consider that an app bug.
                int[] state = getDrawableState();
                thumb.setState(state);
            }

        }
    }
    setPadding(padding, getPaddingTop(), padding, getPaddingBottom());

}