0

Right now I have a AnimationDrawable that runs 3 different pictures making it basically a GIF, what I want is every time a new picture is played it runs a function.

My MainActivity

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Initialization of variables
        character = findViewById(R.id.Character);
        Button left = findViewById(R.id.left);
        Button right = findViewById(R.id.right);
        final Animation animation = new Animation(character, R.id.Character);

        //add animation
        animation.addAnimation(getResources().getDrawable(R.drawable.try1, null));
        animation.addAnimation(getResources().getDrawable(R.drawable.try2, null));
        animation.addAnimation(getResources().getDrawable(R.drawable.try3, null));
        animation.stichAnimations();

        //Left button function
        left.setOnTouchListener(new RepeatListener(400, 100, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                animation.startAnimation()
            }
        }));
}

Animation class

    private ImageView imageAnim;
    private AnimationDrawable animation = new AnimationDrawable();;
    private int number;
    private int id;
    private ArrayList<Drawable> scenes = new ArrayList<>();

    public Animation(ImageView imageAnim, int id){
        this.imageAnim = imageAnim;
        this.id = id;
    }

    public void addAnimation(Drawable drawable){
        animation.addFrame(drawable, 500);
    }

    public void stichAnimations(){
        imageAnim.findViewById(id);
        animation.setOneShot(true);
        imageAnim.setImageDrawable(animation);
    }

    public void startAnimation(){
        animation.start();
        imageAnim.setX(imageAnim.getX() - 5);
    }
}

So basically how would I make it so each time a new picture is shown in the gif imageAnim.setX(imageAnim.getX() - 5) moves the picture to the left

My end goal is to make it look like the character is walking

0 Answers0