如何添加一个滚动的背景帧一帧动画? [英] How do I add a scrolling background to Frame-by-Frame Animation?
问题描述
我有一个Android应用程序中的工作帧一帧动画。
这很好 - 但我将如何添加一个滚动的背景,这个应用程序,从而使动画运行在后台?
我可以找到大量的示例分别实施2个进程 - 却不能在一起。
任何帮助将是AP preciated!
这里是我的code为动漫活动。
进口的java.util.ArrayList;进口android.content.Context;
进口android.graphics.Bitmap;
进口android.graphics.Canvas;
进口android.graphics.drawable.BitmapDrawable;
进口android.graphics.drawable.Drawable;
进口android.util.AttributeSet;
进口android.util.Log;
进口android.view.SurfaceView;
进口android.widget.ImageView;/ **
*
*
* /
公共类MyAnimationView扩展的ImageView
{
私有静态最后弦乐TAG =AnimationTest:AnimationView
私人语境mContext = NULL; 私有静态最终诠释DELAY = 100; //毫秒帧之间的延迟 私人布尔mIsPlaying = FALSE;
私人布尔mStartPlaying = FALSE; 私人的ArrayList<&位图GT; mBitmapList =新的ArrayList<&位图GT;(); 私人INT play_frame = 0;
私人长期last_tick = 0; 公共MyAnimationView(上下文的背景下,ATTRS的AttributeSet)
{
超(背景下,ATTRS);
mContext =背景;
} @覆盖
保护无效的onDraw(帆布C)
{
Log.d(TAG的onDraw称为);
如果(mStartPlaying)
{
Log.d(TAG,开始动画...);
play_frame = 0;
mStartPlaying = FALSE;
mIsPlaying = TRUE;
postInvalidate();
}
否则,如果(mIsPlaying)
{ 如果(play_frame&GT = mBitmapList.size())
{
mIsPlaying = FALSE;
}
其他
{
很长一段时间=(System.currentTimeMillis的() - last_tick);
INT draw_x = 0;
INT draw_y = 0;
如果(时间> = DELAY)//延时时间已经过去了。设置下架
{
last_tick = System.currentTimeMillis的();
c.drawBitmap(mBitmapList.get(play_frame),draw_x,draw_y,NULL);
play_frame ++;
postInvalidate();
}
否则//还是延迟之内。重绘当前帧
{
c.drawBitmap(mBitmapList.get(play_frame),draw_x,draw_y,NULL);
postInvalidate();
}
}
}
} / *理想情况下,这应该是在后台线程* /
公共无效loadAnimation(字符串preFIX,INT将为nframes)
{
mBitmapList.clear();
为(中间体X = 0; X&下;将为nframes; X ++)
{
字符串名称= preFIX +_+ X;
Log.d(TAG,加载动画帧:+姓名);
INT RES_ID = mContext.getResources()则getIdentifier(姓名,绘制,mContext.getPackageName());
BitmapDrawable D =(BitmapDrawable)mContext.getResources()getDrawable(RES_ID)。
mBitmapList.add(d.getBitmap());
}
} 公共无效playAnimation()
{
mStartPlaying = TRUE;
postInvalidate();
}
}
draw_x和draw_y是您的位图位置的变量。如果更改此两个变量(或只是其中之一),你可以移动(滚动)你的图片在屏幕上。
I have a working frame-by-frame animation inside an Android application. That's fine - but how would I add a scrolling background to this application, so that the animation run over the background? I can find plenty of examples for implementing the 2 processes separately - but NOT together. Any help would be appreciated! Here's my code for the animation Activity..
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceView;
import android.widget.ImageView;
/**
*
*
*/
public class MyAnimationView extends ImageView
{
private static final String TAG = "AnimationTest:AnimationView";
private Context mContext = null;
private static final int DELAY = 100; //delay between frames in milliseconds
private boolean mIsPlaying = false;
private boolean mStartPlaying = false;
private ArrayList<Bitmap> mBitmapList = new ArrayList<Bitmap>();
private int play_frame = 0;
private long last_tick = 0;
public MyAnimationView(Context context, AttributeSet attrs)
{
super(context, attrs);
mContext = context;
}
@Override
protected void onDraw(Canvas c)
{
Log.d(TAG, "onDraw called");
if (mStartPlaying)
{
Log.d(TAG, "starting animation...");
play_frame = 0;
mStartPlaying = false;
mIsPlaying = true;
postInvalidate();
}
else if (mIsPlaying)
{
if (play_frame >= mBitmapList.size())
{
mIsPlaying = false;
}
else
{
long time = (System.currentTimeMillis() - last_tick);
int draw_x = 0;
int draw_y = 0;
if (time >= DELAY) //the delay time has passed. set next frame
{
last_tick = System.currentTimeMillis();
c.drawBitmap(mBitmapList.get(play_frame), draw_x, draw_y, null);
play_frame++;
postInvalidate();
}
else //still within delay. redraw current frame
{
c.drawBitmap(mBitmapList.get(play_frame), draw_x, draw_y, null);
postInvalidate();
}
}
}
}
/*ideally this should be in a background thread*/
public void loadAnimation(String prefix, int nframes)
{
mBitmapList.clear();
for (int x = 0; x < nframes; x++)
{
String name = prefix + "_" + x;
Log.d(TAG, "loading animation frame: " + name);
int res_id = mContext.getResources().getIdentifier(name, "drawable", mContext.getPackageName());
BitmapDrawable d = (BitmapDrawable) mContext.getResources().getDrawable(res_id);
mBitmapList.add(d.getBitmap());
}
}
public void playAnimation()
{
mStartPlaying = true;
postInvalidate();
}
}
"draw_x" and "draw_y" are the variables for your bitmap position. If you change this two variables (or just one of them), you can move (scroll) your picture over the screen.
这篇关于如何添加一个滚动的背景帧一帧动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!