Android的动画可以分为视图动画和属性动画
1. View Animation(Tween Animation)----- 视图动画
不改变原有动画属性2. Drawable Animation(Frame Animation)----- 属性动画
代码编辑
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(this);
}
@Override
public void onClick(View v) {
//属性动画(旋转360度)
v.animate().rotation(360).setDuration(1000).start();
}
}
XML配置
res中Animator文件夹(没有就新建),配置动画文件 animate.xml:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(this);
}
@Override
public void onClick(View v) {
// //属性动画(旋转360度)
//-----1------代码编辑
// v.animate().rotation(360).setDuration(1000).start();
//-----2------XML编辑
//对象动画
ObjectAnimator animatior = (ObjectAnimator) AnimatorInflater.loadAnimator(this, R.animator.animate);
animatior.setTarget(v);
animatior.start();
}
}
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(this);
}
@Override
public void onClick(View v) {
// //属性动画(旋转360度)
//-----1------代码编辑
// v.animate().rotation(360).setDuration(1000).start();
//-----2------XML编辑
//对象动画
// ObjectAnimator animatior = (ObjectAnimator) AnimatorInflater.loadAnimator(this, R.animator.animate);
// animatior.setTarget(v);
// animatior.start();
//-----3------使用动画集合
AnimatorSet animator = (AnimatorSet) AnimatorInflater.loadAnimator(this,R.animator.animator_set);
animator.setTarget(v);
animator.start();
}
}
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(this);
}
@Override
public void onClick(View v) {
// //属性动画(旋转360度)
//-----1------代码编辑
// v.animate().rotation(360).setDuration(1000).start();
//-----2------XML编辑
//对象动画
// ObjectAnimator animatior = (ObjectAnimator) AnimatorInflater.loadAnimator(this, R.animator.animate);
// animatior.setTarget(v);
// animatior.start();
//-----3------使用动画集合----XML
// AnimatorSet animator = (AnimatorSet) AnimatorInflater.loadAnimator(this,R.animator.animator_set);
// animator.setTarget(v);
// animator.start();
//-----4-------旋转,相较于1,可订花程度较高-----代码编辑
ObjectAnimator.ofFloat(v,"rotation",0,90,90,360).setDuration(1000).start();
}
}
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(this);
}
@Override
public void onClick(View v) {
// //属性动画(旋转360度)
//-----1------代码编辑(1、2、4)
// v.animate().rotation(360).setDuration(1000).start();
//-----2------XML编辑(1、2、4)
//对象动画
// ObjectAnimator animatior = (ObjectAnimator) AnimatorInflater.loadAnimator(this, R.animator.animate);
// animatior.setTarget(v);
// animatior.start();
//-----3------使用动画集合----XML
// AnimatorSet animator = (AnimatorSet) AnimatorInflater.loadAnimator(this,R.animator.animator_set);
// animator.setTarget(v);
// animator.start();
//-----4-------旋转,相较于1,可订花程度较高-----代码编辑(1、2、4)
// ObjectAnimator.ofFloat(v,"rotation",0,90,90,360).setDuration(1000).start();
//-----5-------动画集合,设置动画顺序等--------代码编辑(3、5)
AnimatorSet set = new AnimatorSet();
set.setDuration(2000);
set.playSequentially(ObjectAnimator.ofFloat(v, "alpha", 0, 1),
ObjectAnimator.ofFloat(v, "translationY", 0, 200));
set.start();
}
}
具体代码点击
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getFragmentManager().beginTransaction().add(R.id.fragment, new FragmentOne()).commit();
}
}
二、新建 FragmentOne.java
public class FragmentOne extends Fragment implements View.OnClickListener {
private View rootView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_layout,null);
rootView.findViewById(R.id.btnAnotherFragment).setOnClickListener(this);
return rootView;
}
@Override
public void onClick(View v) {
getFragmentManager().beginTransaction()
.setCustomAnimations(R.animator.animator_enter,R.animator.animator_exit,R.animator.animator_enter,R.animator.animator_exit)
.addToBackStack("OtherFragment")
.replace(R.id.fragment, new OtherFragment())
.commit();
}
}
public class OtherFragment extends Fragment {
private View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_other,null);
return rootView;
}
}
//设置Fragment后退栈
@Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
res/animator 中新建 进入和退出 动画
animator_enter.xml
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//载入FragmentOne
getFragmentManager().beginTransaction().add(R.id.fragment, new FragmentOne()).commit();
}
}
public class FragmentOne extends Fragment implements View.OnClickListener {
private View rootView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_one, null);
//按钮监听
rootView.findViewById(R.id.button1).setOnClickListener(this);
return rootView;
}
/*两个Fragment顺时针转入转出*/
@Override
public void onClick(View v) {
getFragmentManager().beginTransaction()
.setCustomAnimations(R.animator.animator_two_enter, R.animator.animator_one_exit)
.replace(R.id.fragment, new FragmentTwo())
.commit();
}
}
这里 3D切换的效果其实是使用的旋转效果:FragmentTwo转入,FragmentOne转出,皆为顺时针。
FragmentTwo 的 enter动画效果,res/animator 下 animator_two_enter.xml,
public class FragmentTwo extends Fragment implements View.OnClickListener {
private View rootView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_two, null);
rootView.findViewById(R.id.button2).setOnClickListener(this);
return rootView;
}
/*两个Fragment逆时针转入转出*/
@Override
public void onClick(View v) {
getFragmentManager().beginTransaction()
.setCustomAnimations(R.animator.animator_one_enter, R.animator.animator_two_exit)
.replace(R.id.fragment, new FragmentOne())
.commit();
}
}
FragmentOne 的 enter 动画效果,animator_one_enter.xml
具体代码点击