2015-12-11 66 views
0

我想調用一個動畫方法,我在按鈕按下不同的類中指定。我無法找到有關如何操作的信息。我只是尋找正確的方向,或者知道這是否可能。如何從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); 
    } 

} 

感謝

+0

你想在類CarClass和MainActivity類中做什麼,讓我們說得更清楚 – ResolutioN

+0

我希望CarClass包含所有的動畫代碼,並且在MainActivity中我想要做的就是調用CarClass中的方法來爲其設置動畫效果。所以在onClickListener中,我只能使用test.animate();.我希望這更清楚。 – Alex

+0

你的代碼是錯誤的,你不能擴展一個Activity並且像這樣創建它的實例CarClass test = new CarClass(); – ResolutioN

回答

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); 
     } 

    } 
+0

問題是,我必須將animate()函數放入不同的包中。這就是爲什麼我問是否有可能這樣做。我很欣賞這個幫助! – Alex

+0

是的,它可能只是創建一個類,並通過構造函數的紅色,藍色,綠色和移動動畫到該類。創建一個實例並在onClick – ResolutioN

+0

上調用該動畫方法真棒,我會嘗試。謝謝 – Alex

相關問題