Code example for Drawable

Methods: getIntrinsicHeightgetIntrinsicWidthmutatesetBounds

0
        this(res, resId == 0 ? null : res.getDrawable(resId));
    } 
 
    public TargetDrawable(Resources res, Drawable drawable) {
        // Mutate the drawable so we can animate shared drawable properties. 
        mDrawable = drawable != null ? drawable.mutate() : null;
        resizeDrawables(); 
        setState(STATE_INACTIVE);
    } 
 
    public void setState(int [] state) {
        if (mDrawable instanceof StateListDrawable) {
            StateListDrawable d = (StateListDrawable) mDrawable;
            d.setState(state);
        } 
    } 
 
//    public boolean hasState(int [] state) { 
//        if (mDrawable instanceof StateListDrawable) { 
//            StateListDrawable d = (StateListDrawable) mDrawable; 
//            // TODO: this doesn't seem to work 
//            return d.getStateDrawableIndex(state) != -1; 
//        } 
//        return false; 
//    } 
 
    /** 
     * Returns true if the drawable is a StateListDrawable and is in the focused state. 
     * 
     * @return 
     */ 
    public boolean isActive() { 
        if (mDrawable instanceof StateListDrawable) {
            StateListDrawable d = (StateListDrawable) mDrawable;
            int[] states = d.getState();
            for (int i = 0; i < states.length; i++) {
                if (states[i] == android.R.attr.state_focused) {
                    return true; 
                } 
            } 
        } 
        return false; 
    } 
 
    /** 
     * Returns true if this target is enabled. Typically an enabled target contains a valid 
     * drawable in a valid state. Currently all targets with valid drawables are valid. 
     * 
     * @return 
     */ 
    public boolean isValid() { 
        return mDrawable != null;
    } 
 
    /** 
     * Makes drawables in a StateListDrawable all the same dimensions. 
     * If not a StateListDrawable, then justs sets the bounds to the intrinsic size of the 
     * drawable. 
     */ 
 
    private void resizeDrawables() { 
      if (mDrawable != null)
        mDrawable.setBounds(0, 0, mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight());
    } 
//    private void resizeDrawables() { 
//        if (mDrawable instanceof StateListDrawable) { 
//            StateListDrawable d = (StateListDrawable) mDrawable; 
//            int maxWidth = 0;