下面列出了android.graphics.drawable.AnimationDrawable#getFrame() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
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;
}
@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();
}
}
@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();
}
}