本篇文章主要介绍了"Android动画坐标系详解",主要涉及到Android方面的内容,对于Android开发感兴趣的同学可以参考一下:
前段时间写dialog底部弹出动画的时候明明觉得自己写的是对的,但效果却不对.相信很多人在写View动画的时候都遇到过这种情况.今天本文就对症下药,跟大家聊聊A...
前段时间写dialog底部弹出动画的时候明明觉得自己写的是对的,但效果却不对.相信很多人在写View动画的时候都遇到过这种情况.今天本文就对症下药,跟大家聊聊Animation动画坐标系,至于Animation动画的使用一般都会这里不过多赘言.
Animation 动画一共有四个
1. AlphaAnimation
2. ScaleAnimation
3. RotateAnimation
4. TranslateAnimation
而之所以会出现动画效果与所写不符的主要原因是对Animation.RELATIVE_TO_PARENT,Animation.RELATIVE_TO_SELF,Animation.ABSOLUTE这三个属性的理解不够,下面将详细讲解
首先需要了解动画的原点是相对于该控件也就是自身的左上角.
Animation.ABSOLUTE:指的绝对坐标(单位像素),假如100,就是相对于原点正方向偏移100个像素.
Animation.RELATIVE_TO_SELF:指的是相对于自己.在该类型下值为float类型,比如0.5f,就是相对于原点正方向偏移自身控件百分之五十长度.
Animation.RELATIVE_TO_PARENT:指的是相对于父类.在该类型下值为float类型,比如0.5f,就是相对于原点正方向偏移父控件百分之五十长度.
(ps:这尼玛不太懂啊…楼主太逗了… 别急后面有例子验证^_^)
这里直接用TranslateAnimation动画来验证我们上面的结论
先上布局文件
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.zly.www.animationdemo.MainActivity"><Button
android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="开始"android:onClick="start"/><ImageView
android:id="@+id/iv"android:layout_width="200dp"android:layout_height="200dp"android:background="#000"android:layout_centerInParent="true"/>RelativeLayout>
很简单一个按钮开始动画,一个ImageView展示动画效果
布局展示如下

activity代码
package com.zly.www.animationdemo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
publicclassMainActivityextendsAppCompatActivity {private ImageView iv;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
}
publicvoidstart(View v){
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF,0,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0,
Animation.RELATIVE_TO_SELF,0
);
translateAnimation.setDuration(1000);
translateAnimation.setFillAfter(true);
iv.startAnimation(translateAnimation);
}
}
这里根据控制变量法来分别验证说明
1.Animation.RELATIVE_TO_SELF
我们要分析的核心如下