Java 在Android中为滚动背景添加动画
我已经在Android中创建了两个独立的项目,一个是运行马奔跑的动画,另一个是滚动背景。两者都工作得很好-但是因为它们执行单独的活动,我如何在一个项目中同时运行它们??在滚动背景上的马的动画? 以下是我的独立项目代码: FrameAnimateActivityJava 在Android中为滚动背景添加动画,java,android,2d-games,Java,Android,2d Games,我已经在Android中创建了两个独立的项目,一个是运行马奔跑的动画,另一个是滚动背景。两者都工作得很好-但是因为它们执行单独的活动,我如何在一个项目中同时运行它们??在滚动背景上的马的动画? 以下是我的独立项目代码: FrameAnimateActivity import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.vie
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.view.Menu;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
public class FrameAnimatedActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_frame_animated);
ImageView gyroView = (ImageView) findViewById(R.id.gyro);
//set the animation drawable as background
gyroView.setBackgroundResource(R.drawable.gyro_animation);
//create an animation drawable using the background
AnimationDrawable gyroAnimation = (AnimationDrawable) gyroView.getBackground();
//start the animation
gyroAnimation.start();
ImageView img_animation = (ImageView) findViewById(R.id.gyro);
TranslateAnimation animation = new TranslateAnimation(0.0f, 400.0f,
0.0f, 0.0f); // new TranslateAnimation(xFrom,xTo, yFrom,yTo)
animation.setDuration(5000); // animation duration
animation.setRepeatCount(5); // animation repeat count
animation.setRepeatMode(2); // repeat animation (left to right, right to left )
//animation.setFillAfter(true);
img_animation.startAnimation(animation); // start animation
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.frame_animated, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MovingBackGround(this));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
以及滚动背景的代码:
main活动
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.view.Menu;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
public class FrameAnimatedActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_frame_animated);
ImageView gyroView = (ImageView) findViewById(R.id.gyro);
//set the animation drawable as background
gyroView.setBackgroundResource(R.drawable.gyro_animation);
//create an animation drawable using the background
AnimationDrawable gyroAnimation = (AnimationDrawable) gyroView.getBackground();
//start the animation
gyroAnimation.start();
ImageView img_animation = (ImageView) findViewById(R.id.gyro);
TranslateAnimation animation = new TranslateAnimation(0.0f, 400.0f,
0.0f, 0.0f); // new TranslateAnimation(xFrom,xTo, yFrom,yTo)
animation.setDuration(5000); // animation duration
animation.setRepeatCount(5); // animation repeat count
animation.setRepeatMode(2); // repeat animation (left to right, right to left )
//animation.setFillAfter(true);
img_animation.startAnimation(animation); // start animation
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.frame_animated, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MovingBackGround(this));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
MovingBackground.java
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class MovingBackGround extends SurfaceView implements
SurfaceHolder.Callback {
private Bitmap backGround;
public MovingBackGround(Context context) {
super(context);
backGround = BitmapFactory.decodeResource(context.getResources(),
R.drawable.ab);
setWillNotDraw(false);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
doDrawRunning(canvas);
invalidate();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
/**
* Draws current state of the game Canvas.
*/
private int mBGFarMoveX = 0;
private int mBGNearMoveX = 0;
private void doDrawRunning(Canvas canvas) {
// decrement the far background
mBGFarMoveX = mBGFarMoveX - 1;
// decrement the near background
mBGNearMoveX = mBGNearMoveX - 4;
// calculate the wrap factor for matching image draw
int newFarX = backGround.getWidth() - (-mBGFarMoveX);
// if we have scrolled all the way, reset to start
if (newFarX <= 0) {
mBGFarMoveX = 0;
// only need one draw
canvas.drawBitmap(backGround, mBGFarMoveX, 0, null);
} else {
// need to draw original and wrap
canvas.drawBitmap(backGround, mBGFarMoveX, 0, null);
canvas.drawBitmap(backGround, newFarX, 0, null);
}
}
}
导入android.content.Context;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.graphics.Canvas;
导入android.view.SurfaceHolder;
导入android.view.SurfaceView;
公共类MovingBackGround扩展了SurfaceView实现
SurfaceHolder,回拨{
私有位图背景;
公共移动背景(上下文){
超级(上下文);
后台=BitmapFactory.decodeResource(context.getResources(),
R.drawable.ab);
setWillNotDraw(假);
}
@凌驾
受保护的void onDraw(画布){
super.onDraw(帆布);
doDrawRunning(canvas);
使无效();
}
@凌驾
公共无效表面更改(表面文件夹持有者,整型格式,整型宽度,
整数高度){
}
@凌驾
已创建的公共空白表面(表面持有人){
}
@凌驾
公共空间表面覆盖(表面覆盖物持有人){
}
/**
*绘制游戏画布的当前状态。
*/
私有int mBGFarMoveX=0;
私有int-mBGNearMoveX=0;
私有void dodrawsrunning(画布){
//减少远背景
mBGFarMoveX=mBGFarMoveX-1;
//减少近背景
mBGNearMoveX=mBGNearMoveX-4;
//计算匹配图像绘制的包裹因子
int newFarX=backGround.getWidth()-(-mBGFarMoveX);
//如果我们一直滚动,请重置为开始
如果(newFarX创建一个布局,其中包括MovingBackGround视图和在_R.layout.activity\u frame\u动画中显示的视图(您也可以向后面提到的布局添加MovingBackGround视图xml标记)。创建一个包含上述MovingBackGround视图类和您的上述活动的项目。由于您似乎对视图和活动的概念不太熟悉,我建议您阅读android文档:,抱歉-新项目的顺序不清楚-这是否应该是一个主活动,同时包含FrameAnimate和Animate的内容视图dActivity和MovingBackground类???或其他订单??谢谢!