2017-04-10 14 views
0

私のアプリではたくさんのアニメーションを使用しています。これらすべてのアニメーションをxmlファイルに作成します。すべて正常に動作していますが、私は便利な方法でコードを書いています。ここでアンドロイドでクラスを使用してビューにアニメーションを追加する

は、ダミーのコード例です:

RES /アニメーション(ディレクトリ)

type1_anim1.xml 
    type1_anim2.xml 
    type1_anim3.xml 
    type1_anim4.xml 

    type2_anim1.xml 
    type2_anim2.xml 
    type2_anim3.xml 
    type2_anim4.xml 

MainAvtivity.java

// Reset of the code 
    public void button1(View view){ 
      Animation anim1= AnimationUtils.loadAnimation(this, R.anim.type1_anim1); 
      Animation anim2= AnimationUtils.loadAnimation(this, R.anim.type1_anim2); 
      Animation anim3= AnimationUtils.loadAnimation(this, R.anim.type1_anim3); 
      Animation anim4= AnimationUtils.loadAnimation(this, R.anim.type1_anim4); 

     view1.startAnimation(anim1); 
     view2.startAnimation(anim2); 
     view3.startAnimation(anim3); 
     view4.startAnimation(anim4); 

    } 
    public void button2(View view){ 
      Animation anim1= AnimationUtils.loadAnimation(this, R.anim.type2_anim1); 
      Animation anim2= AnimationUtils.loadAnimation(this, R.anim.type2_anim2); 
      Animation anim3= AnimationUtils.loadAnimation(this, R.anim.type2_anim3); 
      Animation anim4= AnimationUtils.loadAnimation(this, R.anim.type2_anim4); 

     view1.startAnimation(anim1); 
     view2.startAnimation(anim2); 
     view3.startAnimation(anim3); 
     view4.startAnimation(anim4); 

    } 

今私は、上記のような何かをやっています私はこのようなものが欲しい。

ことが可能であるどのようにCustomAnimation.java

public class CustomAnimation{ 
    public anim1(){ 
      // here goes all animations of type1 e.g type1_anim1.xml etc 
    } 
    public anim2(){ 
      // here goes all animations of type2 e.g type2_anim1.xml etc 
    } 
} 

MainActivity.java

public void button1(View view){ 
     anim1(); 
    } 
    public void button2(View view){ 
     anim2(); 
    } 

+0

コードで実行する必要があるアニメーションのタイプは何ですか? (すなわち、フェード、ズーム) –

+0

を作成し、アニメーション化するためにビューとコンテキストを渡します。 – Krish

答えて

1

私は、XMLアニメーションを使用して単純なカスタムアニメーションクラスを記述、

CustomAnimation.java

import android.content.Context; 
import android.view.View; 
import android.view.animation.Animation; 
import android.view.animation.AnimationUtils; 

/** 
* Created by Magesh on 4/10/2017. 
*/ 

public class CustomAnimation 
{ 
    private static CustomAnimation mThis = new CustomAnimation(); 
    public enum AnimationType { 
     FadeIn, ZoomIn, Blink 
    } 
    private CustomAnimation() 
    { 

    } 

    public void startAnimation(Context context, AnimationType animationType, View view) 
    { 
     Animation animation = null; 
     switch (animationType) 
     { 
      case FadeIn: 
      { 
       animation = AnimationUtils.loadAnimation(context, R.anim.fade_in); 
      } 
      break; 
      case ZoomIn: 
      { 
       animation = AnimationUtils.loadAnimation(context, R.anim.zoom_in); 
      } 
      break; 
      case Blink: 
      { 
       animation = AnimationUtils.loadAnimation(context, R.anim.blink); 
      } 
      break; 
     } 

     view.startAnimation(animation); 
    } 

    public static CustomAnimation getThis() 
    { 
     return mThis; 
    } 

} 

あなたは、単に出力のスクリーンショットは、以下のような活動から

import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class MainActivity extends AppCompatActivity { 

    private TextView mTextView; 
    private Button mBtnClick; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mTextView = (TextView) findViewById(R.id.textView); 
     mBtnClick = (Button) findViewById(R.id.button); 
     mBtnClick.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       CustomAnimation customAnimation = CustomAnimation.getThis(); 
       customAnimation.startAnimation(getApplicationContext(), CustomAnimation.AnimationType.Blink, mTextView); 
      } 
     }); 

    } 
} 

を、このメソッドを呼び出すことができます。

enter image description here

+0

私はjavaではないxmlでアニメーションをしたいだけです。Javaクラスを使用して実装したいだけです。 –

+0

@FiverrProjects今、私の答えを更新しました。上記のコードを使用することができます –

+0

渡すビューは良い方法ですか?ビューをcustomAnimationに渡しているためです。ヒープメモリの問題はありますか? –

関連する問題