上面篇介绍了android中的帧动画,本篇主要是来时间android中的另一种动画补间动画,接着上一篇开头说的问题,补间动画只是view形式改变,自身的事件并未改变。
动画实例分析
- 补间动画TweenAnimation
接着上一篇文章的需求,实现loading的加载动画,我们用补间动画来实现
先准备好我们需要用的文件
首先,在styles.xml下定义的样式属性
<!--自定义dialog-->
<style name="CustomDialog" parent="@android:style/Theme.Dialog">
<!--提示框是否有边框-->
<item name="android:windowFrame">@null</item>
<!--提示框是否是浮动的-->
<item name="android:windowIsFloating">true</item>
<!--提示框无标题-->
<item name="android:windowNoTitle">true</item>
<!--给activity设置主题-->
<item name="android:windowContentOverlay">@null</item>
<!--提示框是动画类型-->
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<!--activity主窗口与软键盘的交互模式-->
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<!--是否充许对话框的背景变暗。为true则充许变暗-->
<item name="android:backgroundDimEnabled">true</item>
<!--提示框是滞是透明的-->
<item name="android:windowIsTranslucent">false</item>
</style>
其次我们需要在res文件加下创建出一个anim的文件夹来存放我们定义的动画属性loading_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<rotate
android:duration="1500"
android:fromDegrees="0"
android:interpolator="@android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="-1"
android:repeatMode="restart"
android:startOffset="-1"
android:toDegrees="+360"/>
</set>
这里我们用了一个rotate,动画旋转效果。
在创建dialog的时候createDialog(),绘制了一个view,这个view就是我们定义想要显示的布局customprogressdialog.xml,可根据需要再做修改
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_customdialog"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:minHeight="100dp"
android:minWidth="200dp"
android:padding="10dp"
android:background="#ffffff"
android:orientation="vertical">
<ImageView
android:id="@+id/loading_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/loading"/>
<TextView
android:id="@+id/id_tv_loadingmsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" loading"
android:textColor="#000"
android:textSize="15sp"/>
</LinearLayout>
最后我们这里借助一个dialog来实现loading动画,贴出来自定义的CoustomLoadDialog类
/**
* Created by melo on 2017/3/8.
* 自定义loading
*/
public class CoustomLoadDialog extends Dialog {
private Context context;
private CoustomLoadDialog customProgressDialog;
public CoustomLoadDialog(Context context) {
super(context);
this.context = context;
}
public CoustomLoadDialog(Context context, int theme) {
super(context, theme);
}
public CoustomLoadDialog createDialog() {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.customprogressdialog, null);// 得到加载view
LinearLayout layout = (LinearLayout) v.findViewById(R.id.layout_customdialog);// 加载布局
//xml中的ImageView
ImageView loadimage = (ImageView) v.findViewById(R.id.loading_image);
// 加载动画
Animation animation = AnimationUtils.loadAnimation(context, R.anim.loading_animation);
// 使用ImageView显示动画
loadimage.startAnimation(animation);
customProgressDialog = new CoustomLoadDialog(context, R.style.CustomDialog);
// 设置布局
customProgressDialog.setContentView(layout, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
return customProgressDialog;
}
/**
* setTitile 标题
*
* @param strTitle
* @return
*/
public CoustomLoadDialog setTitile(String strTitle) {
return customProgressDialog;
}
/**
* setMessage 提示内容
*
* @param strMessage
* @return
*/
public CoustomLoadDialog setMessage(String strMessage) {
TextView tvMsg = (TextView) customProgressDialog.findViewById(R.id.id_tv_loadingmsg);
if (tvMsg != null) {
tvMsg.setText(strMessage);
}
return customProgressDialog;
}
}
,在实例化dialog对象的时候就将动画启动
//xml中的ImageView
ImageView loadimage = (ImageView) v.findViewById(R.id.loading_image);
// 加载动画
Animation animation = AnimationUtils.loadAnimation(context, R.anim.loading_animation);
// 使用ImageView显示动画
loadimage.startAnimation(animation);
MainActivity的代码没什么,就一个dialog的生成和show()方法。
//补间动画
public class MainActivity extends AppCompatActivity {
protected CoustomLoadDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressDialog = new CoustomLoadDialog(this).createDialog();
progressDialog.show();
}
}
效果
那么使用补间动画的rotate实现了这个load的动画效果,那么之前说的那个问题又是怎么样的呢?下面我们通过一个translate平移动画来说明为什么补间动画只是view形式改变,自身的事件并未改变。
我简单写了一个平移的动画,我在点击图片初始位置的时候会响应点击事件,但是图片移动到别的位置的时候点击却没有响应,这就是说图片自身的时间并没有随着补间动画而改变响应位置