A. android Studio 怎么创建动画配置xml文件
在Android
Studio菜单上,依次选择:
File
New
Xml
Layout
xml
File
Android
Studio
是一个Android开发环境,基于IntelliJ
IDEA.
类似
Eclipse
ADT,Android
Studio
提供了集成的
Android
开发工具用于开发和调试。
在IDEA的基础上,Android
Stu。详细的可以看看安卓巴士教程:http://www.apkbus.com/thread-318594-1-1.html
B. 求助一个android动画怎么写,音乐播放状态的动画
在Android3.0(即API Level11)以前,Android仅支持2种动画:分别是Frame Animation(逐帧动画)和Tween Animation(补间动画),在3.0之后Android支持了一种新的动画系统,称为:Property Animation(属性动画)。
一、Frame Animation:(逐帧动画)
这个很好理解,一帧帧的播放图片,利用人眼视觉残留原理,给我们带来动画的感觉。它的原理的GIF图片、电影播放原理一样。
1.定义逐帧动画比较简单,只要在中使用子元素定义所有播放帧即可。
(1) android:oneshot 设置是否仅播放一次
(2) android:drawable 设置每一帧图片
(3) android:ration 设置图片间切换间隔
2.习惯上把AnimationDrawable设置为ImageView的背景
android:background=@anim/frame_anim
然后我们就可以在java代码中获取AnimationDrawable对象了
AnimationDrawable anim = (AnimationDrawable)imageView.getBackground();
(需要注意的是,AnimationDrawable默认是不播放的,调用其start()方法开始播放,stop停止播放)
3.上面的动画文件是通过xml文件来配置的,如果你喜欢,也可以通过在java代码中创建AnimationDrawable对象,然后通过addFrame(Drawable frame, int ration)方法向动画添加帧,然后start()。。。
二、Tween Animation:(补间动画)
补间动画就是我们只需指定开始、结束的“关键帧“,而变化中的其他帧由系统来计算,不必自己一帧帧的去定义。
1. Android使用Animation代表抽象动画,包括四种子类:AlphaAnimation(透明度动画)、ScaleAnimation(缩放动画)、TranslateAnimation(位移动画)、RotateAnimation(透明度动画)。Android里面允许在java中创建Animation类对象,但是一般都会采用动画资源文件来定义动画,把界面与逻辑分离
<set android:interpolator="@android:anim/linear_interpolator" xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 定义透明度的变换 -->
<!-- 定义旋转变换 -->
<rotate android:ration="3000/" android:fromdegrees="0" android:pivotx="50%" android:pivoty="50%" android:todegrees="1800">
</rotate></alpha></set>
(一个set可以同时定义多个动画,一起执行。)
2. android:interpolator=@android:anim/linear_interpolator控制动画期间需要补入多少帧,简单来说就是控制动画速度,有些地方翻译为“插值“。Interpolator有几种实现类:LinearInterpolator、AccelerateInterpolator、、CycleInterpolator、DecelerateInterpolator,具体使用可以参考官方API Demo。
3. 定义好anim文件后,我们可以通过AnimationUtils工具类来加载它们,加载成功后返回一个Animation。然后就可以通过View的startAnimation(anim)开始执行动画了。
Animation anim = AnimationUtils.loadAnimation(this, R.anim.anim);
//设置动画结束后保留结束状态
anim.setFillAfter(true);
//设置插值效果
anim.setInterpolator(interpolator);
//对view执行动画
view. startAnimation(anim);
三、Property Animation:(属性动画)
属性动画,这个是在Android 3.0中才引进的,它可以直接更改我们对象的属性。在上面提到的Tween Animation中,只是更改View的绘画效果而View的真实属性是不改变的。假设你用Tween动画将一个Button从左边移到右边,无论你怎么点击移动后的Button,他都没有反应。而当你点击移动前Button的位置时才有反应,因为Button的位置属性木有改变。而Property Animation则可以直接改变View对象的属性值,这样可以让我们少做一些处理工作,提高效率与代码的可读性。
(1)ValueAnimator:包含Property Animation动画的所有核心功能,如动画时间,开始、结束属性值,相应时间属性值计算方法等。应用ValueAnimator有两个步骤
1计算属性值。
2根据属性值执行相应的动作,如改变对象的某一属性。
我们的主是第二步,需要实现ValueAnimator.onUpdateListener接口,这个接口只有一个函数onAnimationUpdate(),将要改变View对象属性的事情在该接口中do。
animation.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
//do your work
}
});
(2)ObjectAnimator:继承自ValueAnimator,要指定一个对象及该对象的一个属性,当属性值计算完成时自动设置为该对象的相应属性,即完成了Property Animation的全部两步操作。实际应用中一般都会用ObjectAnimator来改变某一对象的某一属性,但用ObjectAnimator有一定的限制,要想使用ObjectAnimator,应该满足以下条件:
1.对象应该有一个setter函数:set(驼峰命名法)
2如下面的例子,像ofFloat之类的工场方法,第一个参数为对象名,第二个为属性名,后面的参数为可变参数,如果values…参数只设置了一个值的话,那么会假定为目的值,属性值的变化范围为当前值到目的值,为了获得当前值,该对象要有相应属性的getter方法:get
3如果有getter方法,其应返回值类型应与相应的setter方法的参数类型一致。
ObjectAnimator oa=ObjectAnimator.ofFloat(tv, alpha, 0f, 1f);
oa.setDuration(3000);
oa.start();
如果不满足上面的条件,我们只能乖乖的使用ValueAnimator来创建动画。
(3)Animator.AnimatorListener:可以为Animator设置动画监听,需要重写下面四个方法。
onAnimationStart()
onAnimationEnd()
onAnimationRepeat()
onAnimationCancel()
这里我们也可以实现AnimatorListenerAdapter,他的好处是可以只用定义想监听的事件而不用实现每个函数却只定义一空函数体。如下:
anim.addListener(new AnimatorListenerAdapter() {
public void on AnimationEnd(Animator animation){
//do your work
}
});
(4)AnimationSet:可以组合多个动画共同工作
AnimatorSet bouncer = new AnimatorSet();
bouncer.play(anim1).before(anim2);
bouncer.play(anim2).with(anim3);
bouncer.play(anim2).with(anim4)
bouncer.play(anim5).after(amin2);
animatorSet.start();
上面的代码意思是: 首先播放anim1;同时播放anim2,anim3,anim4;最后播放anim5。
(5)TimeInterplator:与Tween中的interpolator类似。有以下几种
AccelerateInterpolator 加速,开始时慢中间加速
DecelerateInterpolator 减速,开始时快然后减速
先加速后减速,开始结束时慢,中间加速
AnticipateInterpolator 反向 ,先向相反方向改变一段再加速播放
反向加回弹,先向相反方向改变,再加速播放,会超出目的值然后缓慢移动至目的值
BounceInterpolator 跳跃,快到目的值时值会跳跃,如目的值100,后面的值可能依次为85,77,70,80,90,100
CycleIinterpolator 循环,动画循环一定次数,值的改变为一正弦函数:Math.sin(2 * mCycles * Math.PI * input)
LinearInterpolator 线性,线性均匀改变
OvershottInterpolator 回弹,最后超出目的值然后缓慢改变到目的值
TimeInterpolator 一个接口,允许你自定义interpolator,以上几个都是实现了这个接口
(6)Keyframes:可以让我们定义除了开始和结束以外的关键帧。KeyFrame是抽象类,要通过ofInt(),ofFloat(),ofObject()获得适当的KeyFrame,然后通过PropertyValuesHolder.ofKeyframe获得PropertyValuesHolder对象,如下:
Keyframe kf0 = Keyframe.ofInt(0, 400);
Keyframe kf1 = Keyframe.ofInt(0.25f, 200);
Keyframe kf2 = Keyframe.ofInt(0.5f, 400);
Keyframe kf4 = Keyframe.ofInt(0.75f, 100);
Keyframe kf3 = Keyframe.ofInt(1f, 500);
PropertyValuesHolder pvhRotation = PropertyValuesHolder.ofKeyframe(width, kf0, kf1, kf2, kf4, kf3);
ObjectAnimator rotationAnim = ObjectAnimator.ofPropertyValuesHolder(btn, pvhRotation);
上述代码的意思是:设置btn对象的width属性值使其:开始时 Width=400,动画开始1/4时 Width=200,动画开始1/2时 Width=400,动画开始3/4时 Width=100,动画结束时 Width=500。
(7)ViewPropertyAnimator:对一个View同时改变多种属性,非常推荐用这种。该类对多属性动画进行了优化,会合并一些invalidate()来减少刷新视图。而且使用起来非常简便,但是要求API LEVEL 12,即Android 3.1以上。仅需要一行代码即可完成水平、竖直移动
myView.animate().translationX(50f). translationY(100f);
(8)常需要改变的一些属性:
translationX,translationY: View相对于原始位置的偏移量
rotation,rotationX,rotationY: 旋转,rotation用于2D旋转角度,3D中用到后两个
scaleX,scaleY: 缩放比
x,y: View的最终坐标,是View的left,top位置加上translationX,translationY
alpha: 透明度
四、最后自己总结一下这三种动画的优缺点:
(1)Frame Animation(帧动画)主要用于播放一帧帧准备好的图片,类似GIF图片,优点是使用简单方便、缺点是需要事先准备好每一帧图片;
(2)Tween Animation(补间动画)仅需定义开始与结束的关键帧,而变化的中间帧由系统补上,优点是不用准备每一帧,缺点是只改变了对象绘制,而没有改变View本身属性。因此如果改变了按钮的位置,还是需要点击原来按钮所在位置才有效。
(3)Property Animation(属性动画)是3.0后推出的动画,优点是使用简单、降低实现的复杂度、直接更改对象的属性、几乎可适用于任何对象而仅非View类,缺点是需要3.0以上的API支持,限制较大!但是目前国外有个开源库,可以提供低版本支持!
C. 如何通过android官方api查看xml文件属性的相关说明
frameworks\base\core\res\res\values\attrs.xml这个可以看
另外就是xml的属性一般都有对应的java类,到这个java 类api 里面可以查看到属性
D. 如何使用旋转动画和帧动画实现自定义ProgressDialog
第一步:自定义progressDialog的样式 progress_dialog_layout.xml
[html] view plain
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/refreshing_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:scaleType="fitCenter"
android:src="@drawable/icon_progress_dialog" />
</RelativeLayout>
第二步:在styles.xml文件中定义ProgressDialog的主题为:无边框全透明背景
[html] view plain
<resources>
<!--自定义dialog背景全透明无边框theme -->
<style name="MyDialog" parent="android:style/Theme.Dialog">
<!--背景颜色及和透明程度-->
<item name="android:windowBackground">@android:color/transparent</item>
<!--是否去除标题 -->
<item name="android:windowNoTitle">true</item>
<!--是否去除边框-->
<item name="android:windowFrame">@null</item>
<!--是否浮现在activity之上-->
<item name="android:windowIsFloating">true</item>
<!--是否模糊-->
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
第三部:在anim文件夹下,定义旋转动画的样式,这里定义了动画旋转时间为0.7秒,从0到359度,若设置成360度在停止时会出现停顿现象。动画的插值器为先加速后减速,旋转的中心点为imageView的自身中心点。然后repeateCount为重复次数,这里设置为 -1 ,表示无限重复。动画progress_rotate.xml 如下:
[html] view plain
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!--设置动画渲染器为先加速在减速(开始速度最快 逐渐减慢)-->
<rotate
android:ration="700"
android:fromDegrees="0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="-1"
android:toDegrees="359" />
</set>
这里顺便讲解下动画的属性,加深下印象:
android:fromDegrees 起始的角度度数
android:toDegrees 结束的角度度数,负数表示逆时针,正数表示顺时针
android:pivotX 旋转中心的X坐标
浮点数或是百分比。浮点数表示相对于Object的左边缘,如5; 百分比表示相对于Object的左边缘,如5%; 另一种百分比表示相对于父容器的左边缘,如5%p; 一般设置为50%表示在Object中心
android:pivotY 旋转中心的Y坐标
浮点数或是百分比。浮点数表示相对于Object的上边缘,如5; 百分比表示相对于Object的上边缘,如5%; 另一种百分比表示相对于父容器的上边缘,如5%p; 一般设置为50%表示在Object中心
android:ration 表示从android:fromDegrees转动到android:toDegrees所花费的时间,单位为毫秒。可以用来计算速度。
android:interpolator表示变化率,但不是运行速度。一个插补属性,可以将动画效果设置为加速,减速,反复,反弹等。默认为开始和结束慢中间快,
android:startOffset 在调用start函数之后等待开始运行的时间,单位为毫秒,若为10,表示10ms后开始运行
android:repeatCount 重复的次数,默认为0,必须是int,可以为-1表示不停止
android:repeatMode 重复的模式,默认为restart,即重头开始重新运行,可以为reverse即从结束开始向前重新运行。在android:repeatCount大于0或为infinite时生效
android:detachWallpaper 表示是否在壁纸上运行
android:zAdjustment 表示被animated的内容在运行时在z轴上的位置,默认为normal。
normal保持内容当前的z轴顺序
top运行时在最顶层显示
bottom运行时在最底层显示
在操作开始之前调用
if (drawableAnim != null) {
imageView.startAnimation(drawableAnim);
}
操作完成时调用 drawableAnim.clearAnimation();
第四部:继承AlertDialog实现自定义的ProgressDialog,如下:d
[java] view plain
package com.example.myfirst.refreshdialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
/**
* 自定义过场动画,主要用户数据加载时,显示等待progress
* Created by 程果 on 2016/3/16.
*/
public class ProgressAlertDialog extends AlertDialog {
private ImageView progressImg;
//旋转动画
private Animation animation;
public ProgressAlertDialog(Context context) {
super(context, R.style.MyDialog);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progress_dialog_layout);
//点击imageview外侧区域,动画不会消失
setCanceledOnTouchOutside(false);
progressImg = (ImageView) findViewById(R.id.refreshing_img);
//加载动画资源
animation = AnimationUtils.loadAnimation(getContext(), R.anim.progress_rotate);
//动画完成后,是否保留动画最后的状态,设为true
animation.setFillAfter(true);
}
/**
* 在AlertDialog的 onStart() 生命周期里面执行开始动画
*/
@Override
protected void onStart() {
super.onStart();
if( animation != null){
progressImg.startAnimation(animation);
[java] view plain
}
}
/**
* 在AlertDialog的onStop()生命周期里面执行停止动画
*/
@Override
protected void onStop() {
super.onStop();
progressImg.clearAnimation();
}
}
效果gif图如下:
第二种使用 帧动画实现自定义ProgressDialog,
第一步:也是定义动画布局progress_drawable_dialog_layout.xml文件,如下:
[html] view plain
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/refreshing_drawable_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:scaleType="fitCenter"
android:src="@drawable/drawable_anim" />
</RelativeLayout>
第二步:在drawable文件下面定义帧动画,这就比旋转动画简单点,如下只有两个熟悉,单帧图片和它显示的时间:
[java] view plain
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@mipmap/img2"
android:ration="200" />
<item
android:drawable="@mipmap/img3"
android:ration="200" />
<item
android:drawable="@mipmap/img4"
android:ration="200" />
<item
android:drawable="@mipmap/img5"
android:ration="200" />
<item
android:drawable="@mipmap/img6"
android:ration="200" />
<item
android:drawable="@mipmap/img7"
android:ration="200" />
<item
android:drawable="@mipmap/img1"
android:ration="200" />
</animation-list>
第三部:也是使用旋转动画中的AlertDialog主题;
第四部:继承AlertDialog实现自定义的帧动画ProgressDialog:
[java] view plain
package com.example.myfirst.refreshdialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.widget.ImageView;
/**
* 自定义过场动画,主要用户数据加载时,显示等待progress
* Created by 程果 on 2016/3/16.
*/
public class ProgressDrawableAlertDialog extends AlertDialog {
private ImageView progressImg;
//帧动画
private AnimationDrawable animation;
public ProgressDrawableAlertDialog(Context context) {
super(context, R.style.MyDialog);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progress_drawable_dialog_layout);
//点击imageview外侧区域,动画不会消失
setCanceledOnTouchOutside(false);
progressImg = (ImageView) findViewById(R.id.refreshing_drawable_img);
//加载动画资源
animation = (AnimationDrawable) progressImg.getDrawable();
}
/**
* 在AlertDialog的 onStart() 生命周期里面执行开始动画
*/
@Override
protected void onStart() {
super.onStart();
animation.start();
}
/**
* 在AlertDialog的onStop()生命周期里面执行停止动画
*/
@Override
protected void onStop() {
super.onStop();
animation.stop();
}
}
E. 如何查找一个Android控件具有哪些XML属性
通过设置公共style的方式对同一个属性进行统一设置
1、res/values文件夹下新建一个xml文件
1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--定义一个叫et1的style-->
<style name="et1" parent="@android:style/Widget.EditText">
<!--设置控件的背景色-->
<item name="android:background">#1A4EA4</item>
</style>
</resources>
2、组件中使用
1
2
<EditText android:layout_width="fill_parent" android:id="@+id/et2"
android:text="自定义样式一" android:layout_height="wrap_content" style="@style/et1"></EditText> <!--设置style为et1,需要设置同样属性的其他组件可以都设置成同一个style-->
F. Android 中 怎么动态修改布局xml文件中的根属性值 比如如下图的两个属性值 fitsSystemWindows 改为 false
给这个RelativeLayout设置一个id,比如 android:id="@+id/rl"
然后在class文件中获取此控件,再设置属性即可
RelativeLayoutrelativeLayout=(RelativeLayout)findViewById(R.id.rl);
relativeLayout.setFitsSystemWindows(false);
G. Android 中的动画有哪几类,它们的特点和区别是什么
Android包含三种动画:
ViewAnimation、 Drawable Animation、Property Animation。
ViewAnimation(Tween Animation补间动画):
动画的对象除了传统的View对象,还可以是Object对象,动画之后,Object对象的属性值被实实在在的改变了。Property animation能够通过改变View对象的实际属性来实现View动画。任何时候View属性的改变,View能自动调用invalidate()来刷新。
H. android 自定义view 怎么获取xml属性
你说的是自定义view ,也就是你自己写了个自定义的UI控件,在xml中你只要将该控件写出来即可(如Button,TexView当作控件来写),例如:<view(你自定义的view的类名)/>,接下来你就懂了,在里面声明你要的属性
I. android xml配置文件中一些标签的作用
第一个xml是控件动画的xml,interpolator设置动画播放的速度模型,这个设置的是播放速度逐渐变慢。第一个scale是缩放的动画,ration是动画时间,从0.9倍放大到1倍,pivot是指参照哪个点进行缩放,这个设置的50%是指中心。alpha是改变透明度的动画,从0完全透明到1完全不透明。
第二个xml官方叫法是背景选择器,就是改变按钮之类的控件在选中、获得焦点及通常状态时的背景的,可以是纯色,也可以像你这个似的是drawable中的图片。在选择背景时从上到下找到第一个符合条件的为准,state_之类的是各种条件,pressed按下,focused获得焦点,等等,最后一个item是表示默认条件即之前条件都不满足时的背景。
J. android view动画实现从边缘滑出的效果怎么做
添加layout布局文件,在xml设置动画属性即可,上下左右四个方向均可以实现 。animation in / off 例如: 1.slide_in_right <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:ration="100" android:fromXDelta="-100.0%p" android:toXDelta="0.0" /> </set> 2.slide_in_right <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:ration="100" android:fromXDelta="100.0%p" android:toXDelta="0.0" /> </set> 3.slide_out_left <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:ration="100" android:fromXDelta="0.0" android:toXDelta="-100.0%p" /> </set> 4.slide_out_right <set xmlns:android="http://schemas.android.com/apk/res/android" >