下面列出了怎么用android.graphics.drawable.AnimationDrawable的API类实例代码及写法,或者点击链接到github查看源代码。
public final void refreshing() {
if (null != mHeaderText) {
mHeaderText.setText(mRefreshingLabel);
}
if (mUseIntrinsicAnimation) {
((AnimationDrawable) mHeaderImage.getDrawable()).start();
} else {
// Now call the callback
refreshingImpl();
}
if (null != mSubHeaderText) {
mSubHeaderText.setVisibility(View.GONE);
}
}
private void init(Context context) {
mContext = context;
DisplayMetrics dm = getResources().getDisplayMetrics();
mScreenW = dm.widthPixels;
mHeadBmp = BitmapFactory.decodeResource(getResources(), R.drawable.load_more_1);
// mScrDistance = mScreenH / 3 - 46;
mHeadW = mHeadBmp.getWidth();
mHeadH = mHeadBmp.getHeight();
mInitLeftPX = mScreenW / 2 - mHeadW;
mInitRightPX = mScreenW / 2 + mHeadW;
mBmpL = mScreenW / 2 - mHeadBmp.getWidth() / 2;
mBmpT = 0;
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
mPaint.setColor(Color.RED);
mPaint.setStyle(Style.FILL);//设置非填充
mScroller = new Scroller(mContext, new DecelerateInterpolator());
mScroller1 = new Scroller(mContext, new DecelerateInterpolator());
mAniDraw = (AnimationDrawable) mAnimList.getDrawable();
}
public void notifyAnimStop() {
ImageView imageView = mData.get(mLastPlayPosition);
try {
if (imageView != null) {
AnimationDrawable anim = (AnimationDrawable) imageView.getDrawable();
anim.stop();
if (mIsSender) {
imageView.setImageResource(mSendDrawable);
} else {
imageView.setImageResource(mReceiveDrawable);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Convert a AnimationDrawable for use as a barberpole animation.
* Each frame of the animation is wrapped in a ClipDrawable and
* given a tiling BitmapShader.
*/
private Drawable tileifyIndeterminate(Drawable drawable) {
if (drawable instanceof AnimationDrawable) {
AnimationDrawable background = (AnimationDrawable) drawable;
final int N = background.getNumberOfFrames();
AnimationDrawable newBg = new AnimationDrawable();
newBg.setOneShot(background.isOneShot());
for (int i = 0; i < N; i++) {
Drawable frame = tileify(background.getFrame(i), true);
frame.setLevel(10000);
newBg.addFrame(frame, background.getDuration(i));
}
newBg.setLevel(10000);
drawable = newBg;
}
return drawable;
}
@Override
protected void onPageChanged(final int newPage, int previousPage) {
if (mContentAnimator != null) {
mContentAnimator.end();
}
ArrayList<Animator> animators = new ArrayList<>();
Animator fadeOut = createFadeOutAnimator(mContentView);
fadeOut.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
mContentView.setImageDrawable(getResources().getDrawable(pageImages[newPage]));
((AnimationDrawable) mContentView.getDrawable()).start();
}
});
animators.add(fadeOut);
animators.add(createFadeInAnimator(mContentView));
AnimatorSet set = new AnimatorSet();
set.playSequentially(animators);
set.start();
mContentAnimator = set;
}
public final void reset() {
if (null != mHeaderText) {
mHeaderText.setText(mPullLabel);
}
mHeaderImage.setVisibility(View.VISIBLE);
if (mUseIntrinsicAnimation) {
((AnimationDrawable) mHeaderImage.getDrawable()).stop();
} else {
// Now call the callback
resetImpl();
}
if (null != mSubHeaderText) {
if (TextUtils.isEmpty(mSubHeaderText.getText())) {
mSubHeaderText.setVisibility(View.GONE);
} else {
mSubHeaderText.setVisibility(View.VISIBLE);
}
}
}
@Override
public View getRefreshHeaderView() {
if (mRefreshHeaderView == null) {
mRefreshHeaderView = View.inflate(mContext, R.layout.view_refresh_header_normal, null);
mRefreshHeaderView.setBackgroundColor(Color.TRANSPARENT);
if (mRefreshViewBackgroundColorRes != -1) {
mRefreshHeaderView.setBackgroundResource(mRefreshViewBackgroundColorRes);
}
if (mRefreshViewBackgroundDrawableRes != -1) {
mRefreshHeaderView.setBackgroundResource(mRefreshViewBackgroundDrawableRes);
}
mHeaderStatusTv = (TextView) mRefreshHeaderView.findViewById(R.id.tv_normal_refresh_header_status);
mHeaderArrowIv = (ImageView) mRefreshHeaderView.findViewById(R.id.iv_normal_refresh_header_arrow);
mHeaderChrysanthemumIv = (ImageView) mRefreshHeaderView.findViewById(R.id.iv_normal_refresh_header_chrysanthemum);
mHeaderChrysanthemumAd = (AnimationDrawable) mHeaderChrysanthemumIv.getDrawable();
mHeaderStatusTv.setText(mPullDownRefreshText);
}
return mRefreshHeaderView;
}
/**
* 自己去调整
*/
private void initAnimation() {
// 获取ImageView上的动画背景
AnimationDrawable spinnerLive = (AnimationDrawable) mIvLive.getBackground();
// 开始动画
spinnerLive.start();
mIvImg = (ImageView) findViewById(R.id.img_img);
// 获取ImageView上的动画背景
AnimationDrawable spinnerImg = (AnimationDrawable) mIvImg.getBackground();
// 开始动画
spinnerImg.start();
}
/**
* Convert a AnimationDrawable for use as a barberpole animation.
* Each frame of the animation is wrapped in a ClipDrawable and
* given a tiling BitmapShader.
*/
private Drawable tileifyIndeterminate(Drawable drawable) {
if (drawable instanceof AnimationDrawable) {
AnimationDrawable background = (AnimationDrawable) drawable;
final int N = background.getNumberOfFrames();
AnimationDrawable newBg = new AnimationDrawable();
newBg.setOneShot(background.isOneShot());
for (int i = 0; i < N; i++) {
Drawable frame = tileify(background.getFrame(i), true);
frame.setLevel(10000);
newBg.addFrame(frame, background.getDuration(i));
}
newBg.setLevel(10000);
drawable = newBg;
}
return drawable;
}
@Override
public void onBindViewHolder(ExampleSentenceContentAdapter.ViewHolder holder, int position) {
final int pos = position;
StringBuilder sb = new StringBuilder();
sb.append(mDetailedWord.sentence.get(pos).Network_en).append("\n").append(mDetailedWord.sentence.get(pos).Network_cn);
if (pos != getItemCount() - 1) {
sb.append("\n");
}
holder.sentenceContent.setText(sb);
ImageView horn = holder.horn;
horn.setBackgroundResource(R.drawable.animation_horn_color);
mSentenceHornAnimations.add((AnimationDrawable) horn.getBackground());
horn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mSentenceHornAnimations.get(pos + 2).isRunning()) {
return;
}
stopAnimation();
mSentenceHornAnimations.get(pos + 2).start();
mPlayAudio.play(BaseApplication.getInstance(), mDetailedWord.sentence.get(pos).tts_mp3, 3);
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
imageView = findViewById(R.id.load_image);
AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable();
animationDrawable.start();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(intent);
finish();
}
}, 2000);
}
/**
* start the gradient anim
*/
public void startGradientAnimation(){
if(mView!=null){
mView.setBackgroundResource(mGradientAnimation);
mAnimationDrawable = (AnimationDrawable) mView.getBackground();
if(mAnimationDrawable!=null){
mView.setBackground(mAnimationDrawable);
mAnimationDrawable.setEnterFadeDuration(mDuration);
mAnimationDrawable.setExitFadeDuration(mDuration);
mView.post(new Runnable() {
@Override
public void run() {
mAnimationDrawable.start();
}
});
}
}
}
private void initViews() {
this.txt=(TextView)findViewById(R.id.loading_content_text);
this.progressBar=(RoundProgressBar)findViewById(R.id.roundProgressBar);
this.normal_loading_bar=(ImageView)findViewById(R.id.normal_loading);
this.txt.setText(msg);
this.normal_loading_bar.setBackgroundResource(R.drawable.loading_animation);
// 通过ImageView对象拿到背景显示的AnimationDrawable
mAnimation = (AnimationDrawable) normal_loading_bar.getBackground();
// 为了防止在onCreate方法中只显示第一帧的解决方案之一
this.normal_loading_bar.post(new Runnable() {
@Override
public void run() {
mAnimation.start();
}
});
this.normal_loading_bar.setVisibility(View.VISIBLE);
this.progressBar.setVisibility(View.GONE);
}
public final void refreshing() {
if (null != mHeaderText) {
mHeaderText.setText(mRefreshingLabel);
}
if (mUseIntrinsicAnimation) {
((AnimationDrawable) mHeaderImage.getDrawable()).start();
} else {
// Now call the callback
refreshingImpl();
}
if (null != mSubHeaderText) {
mSubHeaderText.setVisibility(View.GONE);
}
}
public final void reset() {
if (null != mHeaderText) {
mHeaderText.setText(mPullLabel);
}
mHeaderImage.setVisibility(View.VISIBLE);
if (mUseIntrinsicAnimation) {
((AnimationDrawable) mHeaderImage.getDrawable()).stop();
} else {
// Now call the callback
resetImpl();
}
if (null != mSubHeaderText) {
if (TextUtils.isEmpty(mSubHeaderText.getText())) {
mSubHeaderText.setVisibility(View.GONE);
} else {
mSubHeaderText.setVisibility(View.VISIBLE);
}
}
}
private void init() {
LayoutInflater.from(getContext()).inflate(R.layout.layout_boss_transfer, this);
mImg = (ImageView) findViewById(R.id.img);
mTemp = findViewById(R.id.line);
// mPb = (ProgressBar) findViewById(R.id.progress);
mPb = (ImageView) findViewById(R.id.progress);
mHandleView.getLocationInWindow(mLocation);
int sbh = Util.getStatusBarHeight(getContext());
mImg.setTranslationY(mLocation[1] - sbh);
mTemp.setTranslationY(mLocation[1] + mImg.getMeasuredHeight() / 2 + sbh);
mPb.setVisibility(GONE);
Bitmap bm = getViewImg(mHandleView);
if (bm != null) {
mImg.setImageBitmap(getViewImg(mHandleView));
}
AnimationDrawable ad = new AnimationDrawable();
ad.addFrame(getDrawable(R.mipmap.icon_refresh_left), 200);
ad.addFrame(getDrawable(R.mipmap.icon_refresh_center), 200);
ad.addFrame(getDrawable(R.mipmap.icon_refresh_right), 200);
mPb.setImageDrawable(ad);
ad.setOneShot(false);
ad.start();
}
/**
* Convert a AnimationDrawable for use as a barberpole animation.
* Each frame of the animation is wrapped in a ClipDrawable and
* given a tiling BitmapShader.
*/
private Drawable tileifyIndeterminate(Drawable drawable) {
if (drawable instanceof AnimationDrawable) {
AnimationDrawable background = (AnimationDrawable) drawable;
final int N = background.getNumberOfFrames();
AnimationDrawable newBg = new AnimationDrawable();
newBg.setOneShot(background.isOneShot());
for (int i = 0; i < N; i++) {
Drawable frame = tileify(background.getFrame(i), true);
frame.setLevel(10000);
newBg.addFrame(frame, background.getDuration(i));
}
newBg.setLevel(10000);
drawable = newBg;
}
return drawable;
}
private void setTypingAnimation(boolean start) {
if (actionBarLayer == null) {
return;
}
if (start) {
try {
actionBarLayer.setSubTitleIcon(R.drawable.typing_dots, OSUtilities.dp(4));
AnimationDrawable mAnim = (AnimationDrawable) actionBarLayer.getSubTitleIcon();
mAnim.setAlpha(200);
mAnim.start();
} catch (Exception e) {
FileLog.e("tmessages", e);
}
} else {
actionBarLayer.setSubTitleIcon(0, 0);
}
}
private Drawable getDrawableByState(Context context, int state) {
switch (state) {
case PlaybackStateCompat.STATE_NONE:
Drawable pauseDrawable = ContextCompat.getDrawable(context, R.drawable.ic_play);
DrawableCompat.setTintList(pauseDrawable, colorPlay);
return pauseDrawable;
case PlaybackStateCompat.STATE_PLAYING:
AnimationDrawable animation = (AnimationDrawable) ContextCompat.getDrawable(context, R.drawable.equalizer);
DrawableCompat.setTintList(animation, colorPlay);
animation.start();
return animation;
case PlaybackStateCompat.STATE_PAUSED:
Drawable playDrawable = ContextCompat.getDrawable(context, R.drawable.equalizer);
DrawableCompat.setTintList(playDrawable, colorPause);
return playDrawable;
default:
Drawable noneDrawable = ContextCompat.getDrawable(context, R.drawable.ic_play);
DrawableCompat.setTintList(noneDrawable, colorPlay);
return noneDrawable;
}
}
public void onClick(View v) {
int id = v.getId();
if (R.id.open == id) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)) {
Toast.makeText(this, "请在设置中开启弹窗权限", Toast.LENGTH_LONG).show();
return;
}
if (!Tools.isServiceRunning(this, FloatBallService.class.getName())) {
startService(new Intent(this, FloatBallService.class));
}
} else if (R.id.close == id) {
if (Tools.isServiceRunning(this, FloatBallService.class.getName())) {
stopService(new Intent(this, FloatBallService.class));
}
} else if (R.id.startAnim == id) {
wifi.setBackgroundResource(R.drawable.wifi_anim);
AnimationDrawable animation = (AnimationDrawable) wifi.getBackground();
animation.start();
} else if (R.id.stopAnim == id) {
stopAnimation();
}
}
public final void refreshing() {
if (null != mHeaderText) {
mHeaderText.setText(mRefreshingLabel);
}
if (mUseIntrinsicAnimation) {
((AnimationDrawable) mHeaderImage.getDrawable()).start();
} else {
// Now call the callback
refreshingImpl();
}
if (null != mSubHeaderText) {
mSubHeaderText.setVisibility(View.GONE);
}
}
public final void reset() {
if (this.mHeaderText != null) {
this.mHeaderText.setText(this.mPullLabel);
}
this.mHeaderImage.setVisibility(0);
if (this.mUseIntrinsicAnimation) {
((AnimationDrawable) this.mHeaderImage.getDrawable()).stop();
} else {
resetImpl();
}
if (this.mSubHeaderText == null) {
return;
}
if (TextUtils.isEmpty(this.mSubHeaderText.getText())) {
this.mSubHeaderText.setVisibility(8);
} else {
this.mSubHeaderText.setVisibility(0);
}
}
public final void reset() {
if (null != mHeaderText) {
mHeaderText.setText(mPullLabel);
}
mHeaderImage.setVisibility(View.VISIBLE);
if (mUseIntrinsicAnimation) {
((AnimationDrawable) mHeaderImage.getDrawable()).stop();
} else {
// Now call the callback
resetImpl();
}
if (null != mSubHeaderText) {
if (TextUtils.isEmpty(mSubHeaderText.getText())) {
mSubHeaderText.setVisibility(View.GONE);
} else {
mSubHeaderText.setVisibility(View.VISIBLE);
}
}
}
public final void refreshing() {
if (null != mHeaderText) {
mHeaderText.setText(mRefreshingLabel);
// mSubHeaderText.setText(mLastRefresh+sdf.format(date));
}
if (mUseIntrinsicAnimation) {
((AnimationDrawable) mHeaderImage.getDrawable()).start();
} else {
// Now call the callback
refreshingImpl();
}
// if (null != mSubHeaderText) {
// mSubHeaderText.setVisibility(View.GONE);
// }
}
public final void refreshing() {
if (null != mHeaderText) {
mHeaderText.setText(mRefreshingLabel);
}
if (mUseIntrinsicAnimation) {
((AnimationDrawable) mHeaderImage.getDrawable()).start();
} else {
// Now call the callback
refreshingImpl();
}
if (null != mSubHeaderText) {
mSubHeaderText.setVisibility(View.GONE);
}
}
@Override
protected void setImageDrawableInto(Drawable drawable, View view) {
((ImageView) view).setImageDrawable(drawable);
if (drawable instanceof AnimationDrawable) {
((AnimationDrawable) drawable).start();
}
}
@Override
public View initView() {
View view = View.inflate(context, R.layout.item_load_more, null);
load_empty = view.findViewById(R.id.load_empty);
load_error = view.findViewById(R.id.load_error);
load_more = view.findViewById(R.id.load_more);
iv_loadmore = (ImageView) view.findViewById(R.id.iv_loadmore);
background = (AnimationDrawable) iv_loadmore.getBackground();
background.start();
return view;
}
public MTRefreshCreator(Context context, ViewGroup parent) {
mHeader = LayoutInflater.from(context)
.inflate(R.layout.just_a_mt_refresh_header, parent, false);
mFirstStepView = (MTFirstView) mHeader.findViewById(R.id.first_view);
mSecondStepView = (MTSecondView) mHeader.findViewById(R.id.second_view);
mThirdStepView = (MTThirdView) mHeader.findViewById(R.id.third_view);
mHeaderText = (TextView) mHeader.findViewById(R.id.tv_pull_to_refresh);
mSecondStepView.setBackgroundResource(R.drawable.mt_pull_to_refresh_second_anim_in);
mThirdStepView.setBackgroundResource(R.drawable.mt_pull_to_refresh_third_anim);
//获取补间动画
mSecondAnimIn = (AnimationDrawable) mSecondStepView.getBackground();
mThirdAnim = (AnimationDrawable) mThirdStepView.getBackground();
}
private void init() {
setBackgroundResource(draw);
final AnimationDrawable frameAnimation = (AnimationDrawable) getBackground();
frameAnimation.setEnterFadeDuration(duration);
frameAnimation.setExitFadeDuration(duration);
post(new Runnable(){
public void run(){
frameAnimation.start();
}
});
}
public final void setLoadingDrawable(Drawable imageDrawable) {
// Set Drawable
mHeaderImage.setImageDrawable(imageDrawable);
mUseIntrinsicAnimation = (imageDrawable instanceof AnimationDrawable);
// Now call the callback
onLoadingDrawableSet(imageDrawable);
}