最近总结一下android的一些东西,毕竟基础不牢地动山摇。本篇主要涉及Animation,对Tween和Frame动画做些总结。
Tween动画即补间动画,主要主持4种效果,缩放、透明度变化、旋转、平移,也可以组合起来使用(set/AnimationSet),在xml中对应的标签和java 代码中体现如下
一般在xml文件中配置Tween动画,它们的公有属性如下:
scale标签是缩放动画,可以实现动态调控件尺寸的效果,有下面几个属性:
放到代码中,ScaleAnimation有下面几个构造函数:
alpha标签主要用来管理透明度:
所对应的构造函数为:
旋转标签
Rotate标签所具有的XML属性有:
对应的构造函数有:
平移标签
这些属性所对应的构造函数为:
用来定义动画速率如何变化
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000" android:fillAfter="true" >
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"/>
<scale android:fromXScale="0.0" android:fromYScale="0.0" android:pivotX="50%" android:pivotY="50%" android:toXScale="1.4" android:toYScale="1.4"/>
<rotate android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:toDegrees="720"/>
<translate android:fromXDelta="0.0" android:fromYDelta="0" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:toXDelta="200" android:toYDelta="200"/>
</set>
private ImageView ivShow;
private Animation setAnimation;
……
private void startSetnimation() {
setAnimation = AnimationUtils.loadAnimation(this, R.anim.setanimtion);
ivShow.startAnimation(setAnimation);
}
这里也对图片设置了点击监听,但是使用动画后的位置点击没有效果,但是点击原来位置的空白处仍有点击触发,这也体现了以前旧的动画的不足之处,就是属性并未改变,比如这里的监听,后面的属性动画正好解决了这一点。
其逐帧动画使用的还是比较多的,类似幻灯片方式来先编写好Drawable,然后代码中调用start()以及stop()开始或停止播放动画,当然我们也可以在Java代码中创建逐帧动画,关键标签为animation-list
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
<item android:drawable="@mipmap/progress_1" android:duration="200"/>
<item android:drawable="@mipmap/progress_2" android:duration="200"/>
<item android:drawable="@mipmap/progress_3" android:duration="200"/>
<item android:drawable="@mipmap/progress_4" android:duration="200"/>
<item android:drawable="@mipmap/progress_5" android:duration="200"/>
<item android:drawable="@mipmap/progress_6" android:duration="200"/>
<item android:drawable="@mipmap/progress_7" android:duration="200"/>
<item android:drawable="@mipmap/progress_8" android:duration="60"/>
</animation-list>
通常在很多自定义的view中使用这种方式,比如加载动画,有一些的转圈圈就是一帧帧动画
这个给出一个customprogressDialog
/* * 自定义旋转对话框 */
public class CustomProgressDialog extends Dialog {
private static CustomProgressDialog customProgressDialog = null;
public CustomProgressDialog(Context context) {
super(context);
}
public CustomProgressDialog(Context context, int theme) {
super(context, theme);
}
public static CustomProgressDialog createDialog(Context context) {
customProgressDialog = new CustomProgressDialog(context,
R.style.CustomProgressDialog);
customProgressDialog.setContentView(R.layout.customprogressdialog);
customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
return customProgressDialog;
}
public void onWindowFocusChanged(boolean hasFocus) {
if (customProgressDialog == null) {
return;
}
ImageView imageView = (ImageView) customProgressDialog
.findViewById(R.id.loadingImageView);
AnimationDrawable animationDrawable = (AnimationDrawable) imageView
.getBackground();
animationDrawable.start();
}
public CustomProgressDialog setTitile(String strTitle) {
return customProgressDialog;
}
public CustomProgressDialog setMessage(String strMessage) {
TextView tvMsg = (TextView) customProgressDialog
.findViewById(R.id.id_tv_loadingmsg);
if (tvMsg != null) {
tvMsg.setText(strMessage);
}
return customProgressDialog;
}
}
其中customProgressDialog.setContentView(R.layout.customprogressdialog);
的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" >
<ImageView android:id="@+id/loadingImageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@anim/progress_round" />
<TextView android:id="@+id/id_tv_loadingmsg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:textSize="20dp" />
</LinearLayout>
imageView使用到了anim中的定义的帧动画
这个dialog的转圈就是通过8张图片通过帧动画来处理的。
代码传送门点击
参考:
http://blog.csdn.net/harvic880925/article/details/39996643