渐变动画淡入淡出(显示隐藏时可用)
private TextView mContentView;
private Button mLoadingView;
private Button mChangeBtn;
private int mShortAnimationDuration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContentView = (TextView) findViewById(R.id.content);
mLoadingView = (Button) findViewById(R.id.loading_spinner);
mChangeBtn = (Button) findViewById(R.id.change_btn);
// Initially hide the content view.
mContentView.setVisibility(View.GONE);
// Retrieve and cache the system's default "short" animation time.
mShortAnimationDuration = getResources().getInteger(
android.R.integer.config_longAnimTime);
mChangeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mLoadingView.isShown()){
crossfade(mLoadingView, mContentView);
return;
}
crossfade(mContentView, mLoadingView);
}
});
}
/**
*
* @param view1 淡出view
* @param view2 淡入view
*/
private void crossfade(final View view1, View view2) {
// Set the content view to 0% opacity but visible, so that it is visible
// (but fully transparent) during the animation.
view2.setAlpha(0f);
view2.setVisibility(View.VISIBLE);
// Animate the content view to 100% opacity, and clear any animation
// listener set on the view.
view2.animate()
.alpha(1f)
.setDuration(mShortAnimationDuration)
.setListener(null);
// Animate the loading view to 0% opacity. After the animation ends,
// set its visibility to GONE as an optimization step (it won't
// participate in layout passes, etc.)
view1.animate()
.alpha(0f)
.setDuration(mShortAnimationDuration)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
view1.setVisibility(View.GONE);
}
});
}
帧动画:Frame Animation
- 帧动画则是通过顺序播放事先准备好的图像来产生动画效果,和电影类似。主要是通过AnimationDrawable类来实现的。
通过xml布局文件实现Frame Animation动画
android:drawable[drawable]//加载Drawable对象
android:duration[long]//每一帧动画的持续时间(单位ms)
android:oneshot[boolean]//动画是否只运行一次,true运行一次,false重复运行
android:visible[boolean]//Drawable对象的初始能见度状态,true可见,false不可见(默认为false)
- 使用xml来实现Frame动画时,首先可以通过setBackgroundResource()方法来加载xml动画布局文件,然后通过getBackground()方法获得动画,再通过setBackgroundDrawable()方法设置要显示的动画,最后通过start()方法来播放动画即可。
imageView.setBackgroundResource(R.anim.anim_frame_loading);
AnimDrawable = (AnimationDrawable) imageView.getBackground();
AnimDrawable.start();
- 定义动画文件
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/loading_01" android:duration="80" />
<item android:drawable="@drawable/loading_02" android:duration="80" />
<item android:drawable="@drawable/loading_03" android:duration="80" />
<item android:drawable="@drawable/loading_04" android:duration="80" />
<item android:drawable="@drawable/loading_05" android:duration="80" />
<item android:drawable="@drawable/loading_06" android:duration="80" />
<item android:drawable="@drawable/loading_07" android:duration="80" />
<item android:drawable="@drawable/loading_08" android:duration="80" />
</animation-list>