Canvas animations - android

I've looked around for help on this but i can't find any answers. Currently, when i want to animate a ball, within a surfaceview, i call a method that:
Sets an animation variable in the surfaceview to a custom class extending AnimationModule (which extends AnimationSet) which changes properties of the ball on every draw
This is then called on every draw until the AnimationModule reaches its maxCount at which point it calls its inherited AnimationListener.onAnimationEnd
The surfaceview receives this callback and then declares the animation variable null so that it doesn't call it on every draw any more
This is working sort of alright at the moment but feels really bad and clunky - I'd really like to be using a tried and tested method that others use as i'm very new to animations.
The other issue with this is that I have to use animations that extend view.animation, so ScaleAnimation, TranslateAnimation etc. It's all well and good for moving the ball around the screen, but when i come to more complicated things like animating it's perspective with canvas.polyToPoly I have to create a new class extending view.animation just for that case or make do without it.
AnimationModule class:
public abstract class AnimationModule extends AnimationSet{
protected AnimationListener listener;
protected CreatureView attacker;
protected CreatureView defender;
protected BattleCanvas canvas;
protected long increment=0;
protected long individualIncrement=0;
protected float interpolation=0;
private boolean finished=false;
private ArrayList<Animation> animations = new ArrayList<Animation>();
private int currentAnimation=0;
private long totalTime=0;
public interface OnAnimationFinishedListener{
public void OnAnimationFinished();
}
public AnimationModule(boolean shareInterpolator){
super(shareInterpolator);
init();
}
public void setCanvas(BattleCanvas canvas){
this.canvas=canvas;
}
public void setCreatures(CreatureView attacker, CreatureView defender){
this.attacker=attacker;
this.defender=defender;
Log.i("anim","attacker :"+String.valueOf(attacker.getCreature().getBaseName()));
}
public AnimationModule(boolean shareInterpolator, AnimationController controller){
super(shareInterpolator);
}
private void init(){
setFillAfter(true);
}
public boolean isFinished(){
return finished;
}
#Override
public void addAnimation(Animation a){
Log.i("Animation","adding animation");
animations.add(a);
computeDuration();
//super.addAnimation(a);
finished=false;
}
#Override
public void setAnimationListener(AnimationListener a){
super.setAnimationListener(a);
this.listener=a;
}
#Override
public boolean getTransformation(long increment, Transformation transform){
//super.getTransformation(increment, transform);
if(animations.size()>currentAnimation)
animations.get(currentAnimation).getTransformation(individualIncrement, transform);
return true;
}
public void draw(){
}
public void update(){
if(animations.size()>0){
Log.i("Animation",String.valueOf(individualIncrement));
Log.i("Animation",String.valueOf((animations.get(currentAnimation).getRepeatCount()+1)));
Log.i("Animation",String.valueOf(animations.get(currentAnimation).getDuration()));
if(individualIncrement>animations.get(currentAnimation).getDuration()*(animations.get(currentAnimation).getRepeatCount()+1)){
if(animations.size()>currentAnimation+1){
currentAnimation++;
individualIncrement=0;
}else{
finished();
return;
}
}
Log.i("module","on animation "+String.valueOf(currentAnimation));
increment();
if(increment>=getDuration()){
finished();
}
}
}
protected long computeDuration(){
long duration=0;
Iterator<Animation> iterator = animations.iterator();
while (iterator.hasNext()) {
Animation a = iterator.next();
duration=duration+a.computeDurationHint();
}
totalTime=duration;
setDuration(duration);
Log.i("Animation","duration set to "+String.valueOf(duration));
return duration;
}
private void finished(){
finished=true;
if(listener!=null)
listener.onAnimationEnd(this);
}
private void increment(){
increment++;
individualIncrement++;
if(getDuration()!=0){
float far = increment/getDuration();
interpolation=far;
}
else
interpolation=0;
}
}
example animation extending AnimationModule:
class BiteAnimation extends AnimationModule{
private float fromSy;
private float fromSx;
private ColorAnimation flash;
public BiteAnimation(boolean shareInterpolator){
super(shareInterpolator);
}
#Override
public void setCanvas(BattleCanvas canvas){
super.setCanvas(canvas);
fromSy=defender.getSy();
fromSx=defender.getSx();
ScaleAnimation scale = new ScaleAnimation(fromSx,fromSx,fromSy,fromSy/2, defender.size/2, defender.size/2);
scale.setDuration(50);
scale.setRepeatMode(2);
scale.setRepeatCount(1);
addAnimation(scale);
flash = new ColorAnimation(defender.getAdd(), Color.WHITE, ColorAnimation.TYPE_ADD, defender);
flash.setDuration(50);
flash.setRepeatMode(2);
flash.setRepeatCount(1);
}
#Override
public void update() {
super.update();
Transformation t = new Transformation();
Log.i("Animation","increment: "+String.valueOf(individualIncrement));
getTransformation(individualIncrement, t);
defender.getMatrix().postConcat(t.getMatrix());
flash.getTransformation(individualIncrement, null);
}
}
as you can see, the custom animationModule just applies it's transformation to the objet it's transforming's canvas(in this case a defender). This canvas is then used when drawing the defender on the surfaceview.
Like i said, this feels really awful and i could do with some help on how i should implement object animations.

Related

How to change gamestates in LibGdx using stacks

I am making a game in android like flappy bird and I am able to go from the menu state to the playState but when the game is over and when I try to get the menu state back it just gives me the background or sometimes a white screen.
public class FlappyDemo extends ApplicationAdapter {//ApplicatoonListener
public static final int width=480;
public static final int height=800;
public static final String title="Flappy Bird";
public GameStateManager gsm;
/*private*/public SpriteBatch spriteBatch;
public void create () {
spriteBatch=new SpriteBatch();
gsm=new GameStateManager();
gsm.push(new MenuState(gsm));
Gdx.gl.glClearColor(1,1,1,1);
}
#Override
public void render () {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
gsm.update(Gdx.graphics.getDeltaTime());
gsm.render(spriteBatch);
}
}
public abstract class State {
OrthographicCamera cam;
Vector3 mouse;
GameStateManager gsm;
public State(GameStateManager gsm){
this.gsm=gsm;
mouse=new Vector3();
cam = new OrthographicCamera();
}
public abstract void handleInput();
public abstract void update(float dt);
public abstract void render(SpriteBatch sb);
public abstract void dispose();
}
public class GameStateManager {
private Stack<State> states;
public GameStateManager(){
states=new Stack<State>();
}
public void push(State state){
states.push(state);
}
public void pop(){
states.pop();
}
public void set(State state){
states.pop().dispose();
states.push(state);
}
public void update(float dt){
states.peek().update(dt);
}
public void render(SpriteBatch sb){
states.peek().render(sb);
}
}
public class MenuState extends State {
Texture background;
Texture playBtn;
public MenuState(GameStateManager gsm) {
super(gsm);
background=new Texture("bg.png");
playBtn=new Texture("buttonflappy.jpg");
}
#Override
public void handleInput() {
if(Gdx.input.isTouched()) {
gsm.push(new PlayState(gsm));
}
}
#Override
public void update(float dt) {
handleInput();
}
#Override
public void render(SpriteBatch sb) {
sb.begin();
sb.draw(background,0,0, 1100,1800);
sb.draw(playBtn,1100/2, 1800/2);
sb.end();
}
#Override
public void dispose() {
playBtn.dispose();
background.dispose();
}
}
so first I call the push method and pass it the MENUSTATE and than in the menustate I call the push method and pass it the PlayState to start the game and in the PLayState I have got the gameplay, cameras,viewports. Now when I say that if the bird hits the tube or the ground the menustate should be called again all I get is a green screen which is the background of the playstate.
if (tube.collide(bird.getBirdBound())) {
gsm.set(new MenuState(gsm));
}
if (bird.getPosition().y <= ground.getHeight() + offset) {
gsm.set(new MenuState(gsm));
}
this is the calling method to the gamestate manager when the game ends and the menustate should be called.
So can anyone tell me how can I get the menustate back instead of getting the background color i.e green.
Really appreciate if anyone could help.
You do not clear the previously rendered color to the framebuffer. I see you only call glClearColor on create. Try this render method:
#Override
public void render () {
Gdx.gl.glClearColor(1,1,1,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
gsm.update(Gdx.graphics.getDeltaTime());
gsm.render(spriteBatch);
}
I finally got it the problem was that I was not changing the camera position. SO what I did was that in the menustate I used the camera and set its position cam.setToOrtho(false,1100,1800) the position where I had drawn the background and updated the camera so now when in the playstate I poped the current state and went back to to the menustate my camera was pointing at the menustate insted of the green color.

android how to make view object with implement runnable open three diffrent time

Hello I'm trying to make view object I have in my app to run three times and i can find how to do it or if it possible or not. any help will be appreciated.
I tried to do it this way but its not working
my thread object:
public class SlotMachineSlot extends View implements Runnable {
veriable...
public SlotMachineSlot(Context context, int x, int y, Bitmap SOURCE_BITMAP) {
super(context);
constructor...
}
public void spin(int newPlace) {
class methoud
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
draw the objact
}
#Override
public void run() {
while (true) {
if (spinning) {
thread function.
}
}
the code i use to start the threads
public Thread[] thread = new Thread[3];
for (int i=0;i<3;i++){
slots[i] = new SlotMachinSlot();
thread[i] = new Thread(slots[i]);
thread[i].start();
}
sorry I didn't had a full understanding of what is a thread and how to use it the way I wanted to use it is bad and shouldn't be done and may cause problem.

Using GLTextureView instead of GLSurfaceView in WallpaperService

I'm trying to create video wallpaper using MediaPlayer. Also I want to use own shaders to apply visual effects on video. I've figured out that it is possible if use TextureView. Here is an example which works perfect, but for Activity only. I need the same functionality for WallpaperService. So, I tried to replace GLSurfaceView by GLTextureView class. I had a class which works great:
public final class GLWallpaperService extends WallpaperService {
#Override
public Engine onCreateEngine() {
return new WallpaperEngine();
}
private final class WallpaperEngine extends Engine implements
SharedPreferences.OnSharedPreferenceChangeListener {
// Slightly modified GLSurfaceView.
private WallpaperGLSurfaceView mGLSurfaceView;
private SharedPreferences mPreferences;
private EngineCore mRenderer;
#Override
public void onCreate(SurfaceHolder surfaceHolder) {
super.onCreate(surfaceHolder);
mRenderer = new EngineCore();
mRenderer.setContext(GLWallpaperService.this);
mGLSurfaceView = new WallpaperGLSurfaceView(GLWallpaperService.this);
mGLSurfaceView.setEGLContextClientVersion(2);
mGLSurfaceView.setRenderer(mRenderer);
mGLSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
mGLSurfaceView.onPause();
}
#Override
public void onDestroy() {
super.onDestroy();
mGLSurfaceView.onDestroy();
mGLSurfaceView = null;
mRenderer = null;
}
#Override
public void onVisibilityChanged(boolean visible) {
super.onVisibilityChanged(visible);
if (visible) {
mGLSurfaceView.onResume();
} else {
mGLSurfaceView.onPause();
}
}
/**
* Lazy as I am, I din't bother using GLWallpaperService (found on
* GitHub) project for wrapping OpenGL functionality into my wallpaper
* service. Instead I am using GLSurfaceView and trick it into hooking
* into Engine provided SurfaceHolder instead of SurfaceView provided
* one GLSurfaceView extends.
*/
private final class WallpaperGLSurfaceView extends GLSurfaceView {
public WallpaperGLSurfaceView(Context context) {
super(context);
}
#Override
public SurfaceHolder getHolder() {
return WallpaperEngine.this.getSurfaceHolder();
}
/**
* Should be called once underlying Engine is destroyed. Calling
* onDetachedFromWindow() will stop rendering thread which is lost
* otherwise.
*/
public void onDestroy() {
super.onDetachedFromWindow();
}
}
}
}
And I got new one by replacing GLSurfaceView on GLTextureView:
public final class GLTextureWallpaperService extends WallpaperService {
#Override
public Engine onCreateEngine() {
return new WallpaperEngine();
}
private final class WallpaperEngine extends Engine {
// Slightly modified GLSurfaceView.
private WallpaperGLTextureView mGLTextureView;
private EngineRenderer mRenderer;
#Override
public void onCreate(SurfaceHolder surfaceHolder) {
super.onCreate(surfaceHolder);
mRenderer = new EngineRenderer();
mRenderer.setContext(GLTextureWallpaperService.this);
mGLTextureView = new WallpaperGLTextureView(GLTextureWallpaperService.this);
mGLTextureView.setEGLContextClientVersion(2);
mGLTextureView.setRenderer(mRenderer);
mGLTextureView.setRenderMode(GLTextureView.RENDERMODE_CONTINUOUSLY);
mGLTextureView.onPause();
}
#Override
public void onDestroy() {
super.onDestroy();
mGLTextureView.onDestroy();
mGLTextureView = null;
mRenderer = null;
}
#Override
public void onVisibilityChanged(boolean visible) {
super.onVisibilityChanged(visible);
if (visible) {
mGLTextureView.onResume();
} else {
mGLTextureView.onPause();
}
}
/**
* Lazy as I am, I din't bother using GLWallpaperService (found on
* GitHub) project for wrapping OpenGL functionality into my wallpaper
* service. Instead I am using GLSurfaceView and trick it into hooking
* into Engine provided SurfaceHolder instead of SurfaceView provided
* one GLSurfaceView extends.
*/
private final class WallpaperGLTextureView extends GLTextureView {
public WallpaperGLTextureView(Context context) {
super(context);
}
//THIS IS NOT EXIST IN GLTEXTUREVIEW CLASS!!!
/*
#Override
public SurfaceHolder getHolder() {
Log.e("getHolder", "getHolder");
return WallpaperEngine.this.getSurfaceHolder();
}
*/
//TRIED TO CHANGE getHolder() BY THIS ONE - NO RESULTS!
#Override
public SurfaceTexture getSurfaceTexture() {
Log.e("getSurfaceTexture", "getSurfaceTexture");
return (SurfaceTexture) WallpaperEngine.this.getSurfaceHolder();
}
public void onDestroy() {
super.onDetachedFromWindow();
}
}
}
}
Renderer class:
public class EngineRenderer implements GLTextureView.Renderer {
Context context;
public void setContext(Context context){
this.context=context;
}
#Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
Log.e("ds", "-onSurfaceCreated--");
}
#Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
Log.e("ds", "-onSurfaceCreated--");
}
#Override
public void onDrawFrame(GL10 gl) {
GLES20.glClearColor(1, 0, 0, 1);
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
}
#Override
public void onSurfaceDestroyed(GL10 gl) {
}
}
I don't get any errors, just black screen. Renderer class is not drawn! It seems to me that GLTextureView doesn't have getHolder() function.
What should I do? Maybe there is another way to implement my goal.

InputProcessor of libgdx does not fire

I'm trying to get a simple libgdx project running on Android. Everything is fine, but my InputProcessor does not fire its events.
I implemented everything according to this tutorial:
http://code.google.com/p/libgdx-users/wiki/IntegratingAndroidNativeUiElements3TierProjectSetup#Code_Example
The first call of "showToast" works fine and is shown on my screen => The showToast-Method does work. Unfortunately, I can't fire any of the InputProcessor events. Even the debugger does not stop there, so they are definitely not called.
Edit: Here is the complete code. I only omitted the Calculator Class, since it works fine and should not be of any conern here. If anyone disagrees with that I can always add it, of course.
Surface Class in libgdx main project (Main class so to say)
public class Surface implements ApplicationListener {
ActionResolver actionResolver;
SpriteBatch spriteBatch;
Texture texture;
Calculator calculator;
public Surface(ActionResolver actionResolver) {
this.actionResolver = actionResolver;
}
#Override
public void create() {
spriteBatch = new SpriteBatch();
texture = new Texture(Gdx.files.internal("ship.png"));
calculator = new Calculator(texture);
actionResolver.showToast("Tap screen to open Activity");
Gdx.input.setInputProcessor(new InputProcessor() {
#Override
public boolean touchDown(int x, int y, int pointer, int button) {
actionResolver.showToast("touchDown");
actionResolver.showMyList();
return true;
}
// overriding all other interface-methods the same way
});
}
#Override
public void render() {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
calculator.update();
spriteBatch.begin();
calculator.draw(spriteBatch);
spriteBatch.end();
}
// Overriding resize, pause, resume, dispose without functionality
}
ActionResolver interface in libgdx main project
public interface ActionResolver {
public void showMyList();
public void showToast(String toastMessage);
}
Implementation of the ActionResolver interface within my Android project
public class ActionResolverImpl implements ActionResolver {
Handler uiThread;
Context appContext;
public ActionResolverImpl(Context appContext) {
uiThread = new Handler();
this.appContext = appContext;
}
#Override
public void showMyList() {
appContext.startActivity(new Intent(this.appContext, MyListActivity.class));
}
#Override
public void showToast(final String toastMessage) {
uiThread.post(new Runnable() {
#Override
public void run() {
Toast.makeText(appContext, toastMessage, Toast.LENGTH_SHORT).show();
}
});
}
}
Android Activity for inizializing the Suface-Class
public class AndroidActivity extends AndroidApplication {
ActionResolverImpl actionResolver;
#Override
public void onCreate(android.os.Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
actionResolver = new ActionResolverImpl(this);
initialize(new Surface(actionResolver), false);
}
}
I also implemented the InputProcessor in my Surface-class, but this should not (and did not) make any difference. Any ideas, what I'm missing?

Set Animation End Listener for Frame by Frame Animation using Surface view

I am running a frame by frame animation using sequential images in a surface view. I am declaring the activity by :
public class myView extends SurfaceView implements SurfaceHolder.Callback
{
public myView (Context paramContext, Listener paramListener)
{
super(paramContext);
getHolder().addCallback(this);
//Some Code
}
public void doDraw(Canvas paramCanvas, int imgpos)
{
// Animation from image source using InputStream
}
public boolean onTouchEvent(MotionEvent paramMotionEvent)
{
return super.onTouchEvent(paramMotionEvent);
}
public void surfaceChanged(SurfaceHolder paramSurfaceHolder, int paramInt1, int paramInt2, int paramInt3)
{
//Some Code
}
public void surfaceCreated(SurfaceHolder paramSurfaceHolder)
{
//Some code
}
public void surfaceDestroyed(SurfaceHolder paramSurfaceHolder)
{
//Some Code
}
}
Now I am trying to implement a Listener for the Animation End event for the above using the following code :
public static abstract interface Listener
{
public abstract void onAnimationEnd();
}
I am stuck up with the above code and can anyone suggest me how I can implement this interface in another activity, so that I can find out that the current animation is over in the Surface View Activity.
Thanks in Advance,
Tim
Add
setAnimationListener
to your animation. If you are assign the animation in the form of drawable in xml file check here to know how to get the animation drawable.

Categories

Resources