我在Android应用程序中有一个工作的逐帧动画。 这很好 - 但是我如何为这个应用程序添加滚动背景,以便动画在后台运行? 我可以找到大量的例子来分别实现这两个过程 - 但不能在一起。 任何帮助,将不胜感激! 这是动画活动的代码..
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();
}
}
答案 0 :(得分:0)
&#34; draw_x&#34;和&#34; draw_y&#34;是您的位图位置的变量。如果更改这两个变量(或只是其中一个变量),则可以在屏幕上移动(滚动)图片。