我正在尝试调用我在按下按钮的其他类中指定的动画方法。我找不到有关如何做的很多信息。我只是想朝着正确的方向努力,或者知道它是否可能。
代码:
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);
}
}
由于
答案 0 :(得分:0)
您应该编写如下代码:
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);
}
}