我想調用一個動畫方法,我在按鈕按下不同的類中指定。我無法找到有關如何操作的信息。我只是尋找正確的方向,或者知道這是否可能。如何從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);
}
}
感謝
你想在類CarClass和MainActivity類中做什麼,讓我們說得更清楚 – ResolutioN
我希望CarClass包含所有的動畫代碼,並且在MainActivity中我想要做的就是調用CarClass中的方法來爲其設置動畫效果。所以在onClickListener中,我只能使用test.animate();.我希望這更清楚。 – Alex
你的代碼是錯誤的,你不能擴展一個Activity並且像這樣創建它的實例CarClass test = new CarClass(); – ResolutioN