FileDocCategorySizeDatePackage
RotateDrawable.javaAPI DocAndroid 1.5 API10128Wed May 06 22:42:00 BST 2009android.graphics.drawable

RotateDrawable

public class RotateDrawable extends Drawable implements Drawable.Callback

A Drawable that can rotate another Drawable based on the current level value. The start and end angles of rotation can be controlled to map any circular arc to the level values range.

It can be defined in an XML file with the <rotate> element.

attr
ref android.R.styleable#RotateDrawable_visible
attr
ref android.R.styleable#RotateDrawable_fromDegrees
attr
ref android.R.styleable#RotateDrawable_toDegrees
attr
ref android.R.styleable#RotateDrawable_pivotX
attr
ref android.R.styleable#RotateDrawable_pivotY
attr
ref android.R.styleable#RotateDrawable_drawable

Fields Summary
private static final float
MAX_LEVEL
private RotateState
mState
private boolean
mMutated
Constructors Summary
public RotateDrawable()

Create a new rotating drawable with an empty state.


                  
      
        this(null);
    
private RotateDrawable(RotateState rotateState)

Create a new rotating drawable with the specified state. A copy of this state is used as the internal state for the newly created drawable.

param
rotateState the state for this drawable

        mState = new RotateState(rotateState, this);
    
Methods Summary
public voiddraw(android.graphics.Canvas canvas)

        int saveCount = canvas.save();

        Rect bounds = mState.mDrawable.getBounds();

        int w = bounds.right - bounds.left;
        int h = bounds.bottom - bounds.top;

        final RotateState st = mState;
        
        float px = st.mPivotXRel ? (w * st.mPivotX) : st.mPivotX;
        float py = st.mPivotYRel ? (h * st.mPivotY) : st.mPivotY;

        canvas.rotate(st.mCurrentDegrees, px, py);

        st.mDrawable.draw(canvas);

        canvas.restoreToCount(saveCount);
    
public intgetChangingConfigurations()

        return super.getChangingConfigurations()
                | mState.mChangingConfigurations
                | mState.mDrawable.getChangingConfigurations();
    
public ConstantStategetConstantState()

        if (mState.canConstantState()) {
            mState.mChangingConfigurations = super.getChangingConfigurations();
            return mState;
        }
        return null;
    
public DrawablegetDrawable()
Returns the drawable rotated by this RotateDrawable.

        return mState.mDrawable;
    
public intgetIntrinsicHeight()

        return mState.mDrawable.getIntrinsicHeight();
    
public intgetIntrinsicWidth()

        return mState.mDrawable.getIntrinsicWidth();
    
public intgetOpacity()

        return mState.mDrawable.getOpacity();
    
public booleangetPadding(android.graphics.Rect padding)

        return mState.mDrawable.getPadding(padding);
    
public voidinflate(android.content.res.Resources r, org.xmlpull.v1.XmlPullParser parser, android.util.AttributeSet attrs)


        TypedArray a = r.obtainAttributes(attrs,
                com.android.internal.R.styleable.RotateDrawable);

        super.inflateWithAttributes(r, parser, a,
                com.android.internal.R.styleable.RotateDrawable_visible);
        
        TypedValue tv = a.peekValue(com.android.internal.R.styleable.RotateDrawable_pivotX);
        boolean pivotXRel = tv.type == TypedValue.TYPE_FRACTION;
        float pivotX = pivotXRel ? tv.getFraction(1.0f, 1.0f) : tv.getFloat();
        
        tv = a.peekValue(com.android.internal.R.styleable.RotateDrawable_pivotY);
        boolean pivotYRel = tv.type == TypedValue.TYPE_FRACTION;
        float pivotY = pivotYRel ? tv.getFraction(1.0f, 1.0f) : tv.getFloat();
        
        float fromDegrees = a.getFloat(
                com.android.internal.R.styleable.RotateDrawable_fromDegrees, 0.0f);
        float toDegrees = a.getFloat(
                com.android.internal.R.styleable.RotateDrawable_toDegrees, 360.0f);

        toDegrees = Math.max(fromDegrees, toDegrees);

        int res = a.getResourceId(
                com.android.internal.R.styleable.RotateDrawable_drawable, 0);
        Drawable drawable = null;
        if (res > 0) {
            drawable = r.getDrawable(res);
        }

        a.recycle();
        
        int outerDepth = parser.getDepth();
        int type;
        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT &&
               (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {

            if (type != XmlPullParser.START_TAG) {
                continue;
            }

            if ((drawable = Drawable.createFromXmlInner(r, parser, attrs)) == null) {
                Log.w("drawable", "Bad element under <rotate>: "
                        + parser .getName());
            }
        }

        if (drawable == null) {
            Log.w("drawable", "No drawable specified for <rotate>");
        }

        mState.mDrawable = drawable;
        mState.mPivotXRel = pivotXRel;
        mState.mPivotX = pivotX;
        mState.mPivotYRel = pivotYRel;
        mState.mPivotY = pivotY;
        mState.mFromDegrees = mState.mCurrentDegrees = fromDegrees;
        mState.mToDegrees = toDegrees;

        if (drawable != null) {
            drawable.setCallback(this);
        }
    
public voidinvalidateDrawable(Drawable who)

        if (mCallback != null) {
            mCallback.invalidateDrawable(this);
        }
    
public booleanisStateful()

        return mState.mDrawable.isStateful();
    
public Drawablemutate()

        if (!mMutated && super.mutate() == this) {
            mState.mDrawable.mutate();
            mMutated = true;
        }
        return this;
    
protected voidonBoundsChange(android.graphics.Rect bounds)

        mState.mDrawable.setBounds(bounds.left, bounds.top,
                bounds.right, bounds.bottom);
    
protected booleanonLevelChange(int level)

        mState.mDrawable.setLevel(level);
        onBoundsChange(getBounds());

        mState.mCurrentDegrees = mState.mFromDegrees +
                (mState.mToDegrees - mState.mFromDegrees) *
                        ((float) level / MAX_LEVEL);

        invalidateSelf();
        return true;
    
protected booleanonStateChange(int[] state)

        boolean changed = mState.mDrawable.setState(state);
        onBoundsChange(getBounds());
        return changed;
    
public voidscheduleDrawable(Drawable who, java.lang.Runnable what, long when)

        if (mCallback != null) {
            mCallback.scheduleDrawable(this, what, when);
        }
    
public voidsetAlpha(int alpha)

        mState.mDrawable.setAlpha(alpha);
    
public voidsetColorFilter(android.graphics.ColorFilter cf)

        mState.mDrawable.setColorFilter(cf);
    
public booleansetVisible(boolean visible, boolean restart)

        mState.mDrawable.setVisible(visible, restart);
        return super.setVisible(visible, restart);
    
public voidunscheduleDrawable(Drawable who, java.lang.Runnable what)

        if (mCallback != null) {
            mCallback.unscheduleDrawable(this, what);
        }