Java Code Examples for android.graphics.drawable.AnimationDrawable#getFrame()

The following examples show how to use android.graphics.drawable.AnimationDrawable#getFrame() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NumberProgressBar.java    From Android_Skin_2.0 with Apache License 2.0 5 votes vote down vote up
private Drawable tileifyAnimationDrawable(AnimationDrawable wrapped) {
	NumberAnimationDrawable drawable = new NumberAnimationDrawable();
	drawable.setOneShot(wrapped.isOneShot());
	final int N = wrapped.getNumberOfFrames();
	for (int i = 0; i < N; i++) {
		Drawable frame = wrapped.getFrame(i);
		int duration = wrapped.getDuration(i);
		drawable.addFrame(frame, duration);
	}
	return drawable;
}
 
Example 2
Source File: PointerIcon.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private void loadResource(Context context, Resources resources, @XmlRes int resourceId) {
    final XmlResourceParser parser = resources.getXml(resourceId);
    final int bitmapRes;
    final float hotSpotX;
    final float hotSpotY;
    try {
        XmlUtils.beginDocument(parser, "pointer-icon");

        final TypedArray a = resources.obtainAttributes(
                parser, com.android.internal.R.styleable.PointerIcon);
        bitmapRes = a.getResourceId(com.android.internal.R.styleable.PointerIcon_bitmap, 0);
        hotSpotX = a.getDimension(com.android.internal.R.styleable.PointerIcon_hotSpotX, 0);
        hotSpotY = a.getDimension(com.android.internal.R.styleable.PointerIcon_hotSpotY, 0);
        a.recycle();
    } catch (Exception ex) {
        throw new IllegalArgumentException("Exception parsing pointer icon resource.", ex);
    } finally {
        parser.close();
    }

    if (bitmapRes == 0) {
        throw new IllegalArgumentException("<pointer-icon> is missing bitmap attribute.");
    }

    Drawable drawable;
    if (context == null) {
        drawable = resources.getDrawable(bitmapRes);
    } else {
        drawable = context.getDrawable(bitmapRes);
    }
    if (drawable instanceof AnimationDrawable) {
        // Extract animation frame bitmaps.
        final AnimationDrawable animationDrawable = (AnimationDrawable) drawable;
        final int frames = animationDrawable.getNumberOfFrames();
        drawable = animationDrawable.getFrame(0);
        if (frames == 1) {
            Log.w(TAG, "Animation icon with single frame -- simply treating the first "
                    + "frame as a normal bitmap icon.");
        } else {
            // Assumes they have the exact duration.
            mDurationPerFrame = animationDrawable.getDuration(0);
            mBitmapFrames = new Bitmap[frames - 1];
            final int width = drawable.getIntrinsicWidth();
            final int height = drawable.getIntrinsicHeight();
            for (int i = 1; i < frames; ++i) {
                Drawable drawableFrame = animationDrawable.getFrame(i);
                if (!(drawableFrame instanceof BitmapDrawable)) {
                    throw new IllegalArgumentException("Frame of an animated pointer icon "
                            + "must refer to a bitmap drawable.");
                }
                if (drawableFrame.getIntrinsicWidth() != width ||
                    drawableFrame.getIntrinsicHeight() != height) {
                    throw new IllegalArgumentException("The bitmap size of " + i + "-th frame "
                            + "is different. All frames should have the exact same size and "
                            + "share the same hotspot.");
                }
                BitmapDrawable bitmapDrawableFrame = (BitmapDrawable) drawableFrame;
                mBitmapFrames[i - 1] = getBitmapFromDrawable(bitmapDrawableFrame);
            }
        }
    }
    if (!(drawable instanceof BitmapDrawable)) {
        throw new IllegalArgumentException("<pointer-icon> bitmap attribute must "
                + "refer to a bitmap drawable.");
    }

    BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
    final Bitmap bitmap = getBitmapFromDrawable(bitmapDrawable);
    validateHotSpot(bitmap, hotSpotX, hotSpotY);
    // Set the properties now that we have successfully loaded the icon.
    mBitmap = bitmap;
    mHotSpotX = hotSpotX;
    mHotSpotY = hotSpotY;
}
 
Example 3
Source File: CropImageView.java    From YImagePicker with Apache License 2.0 4 votes vote down vote up
@Override
public void setImageDrawable(Drawable drawable) {
    super.setImageDrawable(drawable);

    if (drawable == null) {
        hasDrawable = false;
        return;
    }
    if (!hasSize(drawable)) {
        return;
    }

    hasDrawable = true;
    if (originalBitmap == null) {
        if (drawable instanceof BitmapDrawable) {
            originalBitmap = ((BitmapDrawable) drawable).getBitmap();
        } else if (drawable instanceof AnimationDrawable) {
            AnimationDrawable drawable1 = (AnimationDrawable) drawable;
            Drawable drawable2 = drawable1.getFrame(0);
            if (drawable2 instanceof BitmapDrawable) {
                originalBitmap = ((BitmapDrawable) drawable2).getBitmap();
            }
        }
    }

    if (onImageLoadListener != null) {
        onImageLoadListener.onImageLoaded(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        onImageLoadListener = null;
    }

    if (restoreInfo != null) {
        mScaleType = restoreInfo.getScaleType();
        mCropRect = restoreInfo.mWidgetRect;
        aspectX = (int) restoreInfo.mCropX;
        aspectY = (int) restoreInfo.mCropY;
        initBase();
        post(new Runnable() {
            @Override
            public void run() {
                restoreCrop();
            }
        });
    } else {
        initBase();
    }
}
 
Example 4
Source File: CropImageView.java    From YImagePicker with Apache License 2.0 4 votes vote down vote up
@Override
public void setImageDrawable(Drawable drawable) {
    super.setImageDrawable(drawable);

    if (drawable == null) {
        hasDrawable = false;
        return;
    }
    if (!hasSize(drawable)) {
        return;
    }

    hasDrawable = true;
    if (originalBitmap == null) {
        if (drawable instanceof BitmapDrawable) {
            originalBitmap = ((BitmapDrawable) drawable).getBitmap();
        } else if (drawable instanceof AnimationDrawable) {
            AnimationDrawable drawable1 = (AnimationDrawable) drawable;
            Drawable drawable2 = drawable1.getFrame(0);
            if (drawable2 instanceof BitmapDrawable) {
                originalBitmap = ((BitmapDrawable) drawable2).getBitmap();
            }
        }
    }

    if (onImageLoadListener != null) {
        onImageLoadListener.onImageLoaded(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        onImageLoadListener = null;
    }

    if (restoreInfo != null) {
        mScaleType = restoreInfo.getScaleType();
        mCropRect = restoreInfo.mWidgetRect;
        aspectX = (int) restoreInfo.mCropX;
        aspectY = (int) restoreInfo.mCropY;
        initBase();
        post(new Runnable() {
            @Override
            public void run() {
                restoreCrop();
            }
        });
    } else {
        initBase();
    }
}