如何从Android中的其他类调用动画方法?

亚历克斯

我试图调用在按钮按下时在其他类中指定的动画方法。我找不到有关如何执行此操作的太多信息。我只是在寻找正确方向的推动力,或者想知道是否有可能。

代码:

MainAcitivity.java

package com.example.alex.finalassignment;
import CarClass.CarClass;

import android.support.v7.app.AppCompatActivity;



import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity
{

     Button start;
     Button reset;
     Button exit;

    CarClass test = new CarClass();

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


        start = (Button) findViewById(R.id.start);
        reset = (Button) findViewById(R.id.reset);
        exit = (Button) findViewById(R.id.exit);

        if( getIntent().getBooleanExtra("Exit me", false)){
            finish();
        }

    }


    public void onClick(View view)
    {
        ImageView redcar= (ImageView) findViewById(R.id.img_animation1);
        ImageView bluecar= (ImageView) findViewById(R.id.img_animation2);
        ImageView greencar= (ImageView) findViewById(R.id.img_animation3);
        ImageView orangecar= (ImageView) findViewById(R.id.img_animation4);

        switch (view.getId())
        {
            case R.id.start:
            case R.id.reset:

                test.animate();
                /**Animation redMoveAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.car_anim);
                Animation blueMoveAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.car_anim2);
                Animation greenMoveAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.car_anim3);
                Animation orangeMoveAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.car_anim4);

                redcar.startAnimation(redMoveAnimation);
                redMoveAnimation.setFillAfter(true);

                bluecar.startAnimation(blueMoveAnimation);
                blueMoveAnimation.setFillAfter(true);

                greencar.startAnimation(greenMoveAnimation);
                greenMoveAnimation.setFillAfter(true);

                orangecar.startAnimation(orangeMoveAnimation);
                orangeMoveAnimation.setFillAfter(true);
                */
                break;
            case R.id.exit:

                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_HOME);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);

                break;
        }

    }






}

CarClass.java

package CarClass;

import android.app.Activity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

import com.example.alex.finalassignment.R;



public class CarClass extends Activity
{

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



        if( getIntent().getBooleanExtra("Exit me", false)){
            finish();
        }

    }
    public void animate()
    {
        ImageView redcar= (ImageView) findViewById(R.id.img_animation1);
        ImageView bluecar= (ImageView) findViewById(R.id.img_animation2);
        ImageView greencar= (ImageView) findViewById(R.id.img_animation3);
        ImageView orangecar= (ImageView) findViewById(R.id.img_animation4);

        Animation redMoveAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.car_anim);
        Animation blueMoveAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.car_anim2);
        Animation greenMoveAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.car_anim3);
        Animation orangeMoveAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.car_anim4);

        redcar.startAnimation(redMoveAnimation);
        redMoveAnimation.setFillAfter(true);

        bluecar.startAnimation(blueMoveAnimation);
        blueMoveAnimation.setFillAfter(true);

        greencar.startAnimation(greenMoveAnimation);
        greenMoveAnimation.setFillAfter(true);

        orangecar.startAnimation(orangeMoveAnimation);
        orangeMoveAnimation.setFillAfter(true);
    }

}

谢谢

兰卡

您应该这样编写代码:

    public class MainActivity extends AppCompatActivity implements OnClickListener
    {

         Button start;
         Button reset;
         Button exit;
         ImageView redcar;
         ImageView bluecar;
         ImageView greencar;
         ImageView orangecar;

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


            start = (Button) findViewById(R.id.start);
            reset = (Button) findViewById(R.id.reset);
            exit = (Button) findViewById(R.id.exit);

            redcar= (ImageView) findViewById(R.id.img_animation1);
            bluecar= (ImageView) findViewById(R.id.img_animation2);
            greencar= (ImageView) findViewById(R.id.img_animation3);
            orangecar= (ImageView) findViewById(R.id.img_animation4);

            start.setOnClickListener(this);
            reset.setOnClickListener(this); 
            exit.setOnClickListener(this);  
        }

        @Override
        public void onClick(View view)
        {

            switch (view.getId())
            {
                case R.id.start:
                case R.id.reset:
                    animate();
                    break;
                case R.id.exit:

                    finish();

                    break;
            }

        }


     public void animate() {
            Animation redMoveAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.car_anim);
            Animation blueMoveAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.car_anim2);
            Animation greenMoveAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.car_anim3);
            Animation orangeMoveAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.car_anim4);

            redcar.startAnimation(redMoveAnimation);
            redMoveAnimation.setFillAfter(true);

            bluecar.startAnimation(blueMoveAnimation);
            blueMoveAnimation.setFillAfter(true);

            greencar.startAnimation(greenMoveAnimation);
            greenMoveAnimation.setFillAfter(true);

            orangecar.startAnimation(orangeMoveAnimation);
            orangeMoveAnimation.setFillAfter(true);
        }

    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章