使用android中的Class将视图添加到视图中

时间:2017-04-10 14:42:56

标签: android android-animation

我在我的应用中使用了很多动画。我在xml个文件中创建了所有这些动画。一切正常,但我想以有用的方式编写代码。

这是虚拟代码示例:

Res / 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();
  }

怎么可能。

1 个答案:

答案 0 :(得分:1)

我使用xml Animation编写简单的自定义动画类

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

}

您可以从下面的Activity中调用此方法,

    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