java图片16帧动画_Java实现帧动画的实例代码
本文講述了Java實現(xiàn)幀動畫的實例代碼。分享給大家供大家參考,具體如下:
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();
}
}
//動畫結(jié)束
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);
總結(jié)
以上是生活随笔為你收集整理的java图片16帧动画_Java实现帧动画的实例代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: netbeans缺少java文件夹_Ja
- 下一篇: php 替换某一行,PHP中如何替换换行