Code example for Drawable

Methods: setCallbacksetFilterBitmap

0
	private void init() { 
		final AnimatedRotateState state = mState;
		mIncrement = 360.0f / state.mFramesCount;
		final Drawable drawable = state.mDrawable;
		if ( drawable != null ) {
			drawable.setFilterBitmap( true );
			if ( drawable instanceof BitmapDrawable ) {
				( (BitmapDrawable) drawable ).setAntiAlias( true );
			} 
		} 
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see android.graphics.drawable.Drawable#draw(android.graphics.Canvas) 
	 */ 
	@Override 
	public void draw( Canvas canvas ) {
		int saveCount = canvas.save();
 
		final AnimatedRotateState st = mState;
		final Drawable drawable = st.mDrawable;
		final Rect bounds = drawable.getBounds();
 
		int w = bounds.right - bounds.left;
		int h = bounds.bottom - bounds.top;
 
		float px = st.mPivotXRel ? ( w * st.mPivotX ) : st.mPivotX;
		float py = st.mPivotYRel ? ( h * st.mPivotY ) : st.mPivotY;
 
		canvas.rotate( mCurrentDegrees, px + bounds.left, py + bounds.top );
 
		drawable.draw( canvas );
 
		canvas.restoreToCount( saveCount );
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see android.graphics.drawable.Animatable#start() 
	 */ 
	@Override 
	public void start() { 
		if ( !mRunning ) {
			mRunning = true;
			nextFrame(); 
		} 
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see android.graphics.drawable.Animatable#stop() 
	 */ 
	@Override 
	public void stop() { 
		mRunning = false;
		unscheduleSelf( this );
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see android.graphics.drawable.Animatable#isRunning() 
	 */ 
	@Override 
	public boolean isRunning() { 
		return mRunning;
	} 
 
	/** 
	 * Next frame. 
	 */ 
	private void nextFrame() { 
		unscheduleSelf( this );
		scheduleSelf( this, SystemClock.uptimeMillis() + mState.mFrameDuration );
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see java.lang.Runnable#run() 
	 */ 
	@Override 
	public void run() { 
		mCurrentDegrees += mIncrement;
		if ( mCurrentDegrees > ( 360.0f - mIncrement ) ) {
			mCurrentDegrees = 0.0f;
		} 
		invalidateSelf();
		nextFrame(); 
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see android.graphics.drawable.Drawable#setVisible(boolean, boolean) 
	 */ 
	@Override 
	public boolean setVisible( boolean visible, boolean restart ) {
		mState.mDrawable.setVisible( visible, restart );
		boolean changed = super.setVisible( visible, restart );
		if ( visible ) {
			if ( changed || restart ) {
				mCurrentDegrees = 0.0f;
				nextFrame(); 
			} 
		} else { 
			unscheduleSelf( this );
		} 
		return changed;
	} 
 
	/** 
	 * Returns the drawable rotated by this RotateDrawable. 
	 *  
	 * @return the drawable 
	 */ 
	public Drawable getDrawable() {
		return mState.mDrawable;
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see android.graphics.drawable.Drawable#getChangingConfigurations() 
	 */ 
	@Override 
	public int getChangingConfigurations() { 
		return super.getChangingConfigurations() | mState.mChangingConfigurations | mState.mDrawable.getChangingConfigurations();
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see android.graphics.drawable.Drawable#setAlpha(int) 
	 */ 
	@Override 
	public void setAlpha( int alpha ) {
		mState.mDrawable.setAlpha( alpha );
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see android.graphics.drawable.Drawable#setColorFilter(android.graphics.ColorFilter) 
	 */ 
	@Override 
	public void setColorFilter( ColorFilter cf ) {
		mState.mDrawable.setColorFilter( cf );
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see android.graphics.drawable.Drawable#getOpacity() 
	 */ 
	@Override 
	public int getOpacity() { 
		return mState.mDrawable.getOpacity();
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see android.graphics.drawable.Drawable.Callback#invalidateDrawable(android.graphics.drawable.Drawable) 
	 */ 
	@Override 
	public void invalidateDrawable( Drawable who ) {
		if ( Constants.ANDROID_SDK > 10 ) { 
 
			Callback callback;
			try { 
				callback = (Callback) ReflectionUtils.invokeMethod( this, "getCallback" );
			} catch ( ReflectionException e ) {
				return; 
			} 
			if ( callback != null ) {
				callback.invalidateDrawable( this );
			} 
		} 
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see android.graphics.drawable.Drawable.Callback#scheduleDrawable(android.graphics.drawable.Drawable, java.lang.Runnable, 
	 * long) 
	 */ 
	@Override 
	public void scheduleDrawable( Drawable who, Runnable what, long when ) {
		if ( Constants.ANDROID_SDK > 10 ) { 
			Callback callback;
			try { 
				callback = (Callback) ReflectionUtils.invokeMethod( this, "getCallback" );
			} catch ( ReflectionException e ) {
				return; 
			} 
			if ( callback != null ) {
				callback.scheduleDrawable( this, what, when );
			} 
		} 
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see android.graphics.drawable.Drawable.Callback#unscheduleDrawable(android.graphics.drawable.Drawable, java.lang.Runnable) 
	 */ 
 
	@Override 
	public void unscheduleDrawable( Drawable who, Runnable what ) {
		if ( Constants.ANDROID_SDK > 10 ) { 
			Callback callback;
			try { 
				callback = (Callback) ReflectionUtils.invokeMethod( this, "getCallback" );
			} catch ( ReflectionException e ) {
				return; 
			} 
			if ( callback != null ) {
				callback.unscheduleDrawable( this, what );
			} 
		} 
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see android.graphics.drawable.Drawable#getPadding(android.graphics.Rect) 
	 */ 
	@Override 
	public boolean getPadding( Rect padding ) {
		return mState.mDrawable.getPadding( padding );
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see android.graphics.drawable.Drawable#isStateful() 
	 */ 
	@Override 
	public boolean isStateful() { 
		return mState.mDrawable.isStateful();
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see android.graphics.drawable.Drawable#onBoundsChange(android.graphics.Rect) 
	 */ 
	@Override 
	protected void onBoundsChange( Rect bounds ) {
		mState.mDrawable.setBounds( bounds.left, bounds.top, bounds.right, bounds.bottom );
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see android.graphics.drawable.Drawable#getIntrinsicWidth() 
	 */ 
	@Override 
	public int getIntrinsicWidth() { 
		return mState.mDrawable.getIntrinsicWidth();
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see android.graphics.drawable.Drawable#getIntrinsicHeight() 
	 */ 
	@Override 
	public int getIntrinsicHeight() { 
		return mState.mDrawable.getIntrinsicHeight();
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see android.graphics.drawable.Drawable#getConstantState() 
	 */ 
	@Override 
	public ConstantState getConstantState() {
		if ( mState.canConstantState() ) {
			mState.mChangingConfigurations = getChangingConfigurations();
			return mState;
		} 
		return null; 
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see android.graphics.drawable.Drawable#inflate(android.content.res.Resources, org.xmlpull.v1.XmlPullParser, 
	 * android.util.AttributeSet) 
	 */ 
	@Override 
	public void inflate( Resources r, XmlPullParser parser, AttributeSet attrs ) throws XmlPullParserException, IOException {}
 
	/** 
	 * Sets the frames count. 
	 *  
	 * @param framesCount 
	 *           the new frames count 
	 */ 
	public void setFramesCount( int framesCount ) {
		mState.mFramesCount = framesCount;
		mIncrement = 360.0f / mState.mFramesCount;
	} 
 
	/** 
	 * Sets the frames duration. 
	 *  
	 * @param framesDuration 
	 *           the new frames duration 
	 */ 
	public void setFramesDuration( int framesDuration ) {
		mState.mFrameDuration = framesDuration;
	} 
 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see android.graphics.drawable.Drawable#mutate() 
	 */ 
	@Override 
	public Drawable mutate() {
		if ( !mMutated && super.mutate() == this ) {
			mState.mDrawable.mutate();
			mMutated = true;
		} 
		return this;
	} 
 
	final static class AnimatedRotateState extends Drawable.ConstantState {
 
		Drawable mDrawable;
		int mChangingConfigurations;
		boolean mPivotXRel;
		float mPivotX;
		boolean mPivotYRel;
		float mPivotY;
		int mFrameDuration;
		int mFramesCount;
		private boolean mCanConstantState;
		private boolean mCheckedConstantState;
 
		/** 
		 * Instantiates a new animated rotate state. 
		 *  
		 * @param source 
		 *           the source 
		 * @param owner 
		 *           the owner 
		 * @param res 
		 *           the res 
		 */ 
		public AnimatedRotateState( AnimatedRotateState source, AnimatedRotateDrawable owner, Resources res ) {
			if ( source != null ) {
				if ( res != null ) {
					mDrawable = source.mDrawable.getConstantState().newDrawable( res );
				} else { 
					mDrawable = source.mDrawable.getConstantState().newDrawable();
				} 
				mDrawable.setCallback( owner );
				mPivotXRel = source.mPivotXRel;
				mPivotX = source.mPivotX;
				mPivotYRel = source.mPivotYRel;
				mPivotY = source.mPivotY;
				mFramesCount = source.mFramesCount;