這篇文章主要介紹了Java如何實現幀動畫,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
具體如下:
1、效果圖

2、幀動畫的簡要代碼
private ImageView bgAnimView;
private AnimationDrawable mAnimationDrawable;
//初始化
mAnimationDrawable = new AnimationDrawable();
bgAnimView = new ImageView(mContext);
bgAnimView.setBackgroundDrawable(getAnimationDrawable(mAnimationDrawable));
params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.topMargin = Util.Div(176 + 58);
params.gravity = Gravity.CENTER_HORIZONTAL;
addView(bgAnimView, params);
private AnimationDrawable getAnimationDrawable(AnimationDrawable mAnimationDrawable) {
int duration = 50;
mAnimationDrawable.addFrame(mContext.getResources().getDrawable(R.drawable.loading1), duration);
mAnimationDrawable.addFrame(mContext.getResources().getDrawable(R.drawable.loading2), duration);
mAnimationDrawable.addFrame(mContext.getResources().getDrawable(R.drawable.loading3), duration);
mAnimationDrawable.setOneShot(false);
return mAnimationDrawable;
}
//動畫開始
public void animLoadingStart() {
this.setVisibility(View.VISIBLE);
if (mAnimationDrawable != null) {
mAnimationDrawable.start();
}
}
//動畫結束
public void animLoadingEnd() {
if (mAnimationDrawable != null) {
mAnimationDrawable.stop();
}3、擴展:
//X軸平移
public void animY(int y, int nextY, int duration) {
LinearInterpolator ll = new LinearInterpolator(); //勻速
ObjectAnimator animator = ObjectAnimator.ofFloat(yourView, "translationY", 0, 300);//300若為負值,就是向上平移
animator.setDuration(duration);
animator.setInterpolator(ll);
animator.start();
}
//Y軸平移
public void animX(int x, int nextX, int duration) {
LinearInterpolator ll = new LinearInterpolator();
ObjectAnimator animator = ObjectAnimator.ofFloat(yourView, "translationX", x, nextX);
animator.setDuration(duration);
animator.setInterpolator(ll);
animator.start();
}
//縱向壓縮0.5倍
LinearInterpolator ll = new LinearInterpolator();//勻速
ScaleAnimation scaleAnimation = new ScaleAnimation(1, 1, 1, 0.5f);//默認從(0,0)
scaleAnimation.setDuration(500);
scaleAnimation.setInterpolator(ll);
scaleAnimation.setFillAfter(true);
chartView.startAnimation(scaleAnimation);
//橫向壓縮0.5倍
LinearInterpolator ll = new LinearInterpolator();
ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.5f, 1, 1);//默認從(0,0)
scaleAnimation.setDuration(500);
scaleAnimation.setInterpolator(ll);
scaleAnimation.setFillAfter(true);
chartView.startAnimation(scaleAnimation);感謝你能夠認真閱讀完這篇文章,希望小編分享的“Java如何實現幀動畫”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。