2017-04-10 4 views
0

내 앱에서 많은 애니메이션을 사용하고 있습니다. xml 파일에이 모든 애니메이션을 만듭니다. 모든 것이 잘 작동하지만 유용한 방식으로 코드를 작성하려고합니다. 여기 android의 클래스를 사용하여 뷰에 애니메이션 추가

더미 코드 예입니다 :

해상도/ANIM (디렉토리)

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

자바에서 xml 애니메이션을 원한다. 그냥 Java 클래스를 사용하여 구현하고 싶다. –

+0

@FiverrProjects 이제 내 대답을 업데이트했다. 위의 코드를 사용할 수있다. –

+0

전달보기가 좋은 방법인가? 뷰를 customAnimation에 전달하기 때문입니다. 힙 메모리 문제가 있습니까? –