View動畫:平移烁巫、縮放、旋轉(zhuǎn)磁餐、透明度;
對應的在xml中的標簽為:traslate阿弃、scale羞延、rotate脾还、alpha;
四種變換效果對應Animation的四個子類:TranslateAnimation、ScaleAnimation赛蔫、RotateAnimation泥张、AlphaAnimation.
描述view動畫的xml語法如下所示:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="true"
android:fillAfter="true">
<translate android:fromXDelta="0%p"
android:fromYDelta="0%p"
android:toXDelta="80%p"
android:toYDelta="80%p"/>
<scale android:fromXScale="1"
android:fromYScale="1"
android:toXScale="0.2"
android:toYScale="0.2"
/>
<rotate android:fromDegrees="0"
android:toDegrees="360"
/>
<alpha android:fromAlpha="1"
android:toAlpha="0.5"
/>
</set>
其中,<set>標簽標示動畫的集合渗钉,對應于AnimationSet類钞钙,其中interpalator屬性表示動畫所采用的插值器,默認為@android:anim/accelerate_decelerate_interpolator,先加速芒炼,后減速的效果本刽。shareInterpolator屬性表示集合中的動畫是否都共享同一個插值器。
對于xml中各種數(shù)值的單位子寓,比如80%指相對于自身的80%,該數(shù)值實際上代表了兩種屬性炸裆,一個pivotType和pivotValue,80%在java代碼中對應于pivotType = Animation.RELATIVE_TO_SELF,
pivotValue = 0.8f鲜屏。另外一種是相當于父布局,比如80%p,此時听系,pivot*Type = Animation.RELATIVE_TO_PARENT虹菲。
Java代碼如下:
AnimationSet animationSet = new AnimationSet(true);
ScaleAnimation scaleAnimation = new ScaleAnimation(0.1f,1f,0.1f,1f,
Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT,0f,Animation.RELATIVE_TO_PARENT,0.5f,
Animation.RELATIVE_TO_PARENT,0f,Animation.RELATIVE_TO_PARENT,0.5f);
RotateAnimation rotateAnimation = new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
AlphaAnimation alphaAnimation = new AlphaAnimation(0.3f,1f);
animationSet.addAnimation(scaleAnimation);
animationSet.addAnimation(rotateAnimation);
animationSet.addAnimation(alphaAnimation);
animationSet.addAnimation(translateAnimation);
animationSet.setFillAfter(true);
scaleAnimation.setDuration(3000);
translateAnimation.setDuration(3000);
alphaAnimation.setDuration(3000);
rotateAnimation.setDuration(500);
rotateAnimation.setRepeatCount(6);
mShowAnimationTv.startAnimation(animationSet);
上面的代碼是將左上角的textView移到右下角,并且伴隨著旋轉(zhuǎn)浪漠,透明度,縮放的動畫(代碼實現(xiàn)時要注意set集合add時的順序)该镣。
實現(xiàn)原理
首先响谓,從動畫開始的地方AnimatonUtils.loadAnimaiton()方法開始,源碼如下:
public static Animation loadAnimation(Context context, @AnimRes int id)
throws NotFoundException {
XmlResourceParser parser = null;
try {
parser = context.getResources().getAnimation(id);
return createAnimationFromXml(context, parser);
} catch (XmlPullParserException ex) {
NotFoundException rnf = new NotFoundException("Can't load animation resource ID #0x" +
Integer.toHexString(id));
rnf.initCause(ex);
throw rnf;
} catch (IOException ex) {
NotFoundException rnf = new NotFoundException("Can't load animation resource ID #0x" +
Integer.toHexString(id));
rnf.initCause(ex);
throw rnf;
} finally {
if (parser != null) parser.close();
}
}
最后會調(diào)用createAnimationFromXml(context, parser),此處使用了XmlResourceParse嫁审。
private static Animation createAnimationFromXml(Context c, XmlPullParser parser)
throws XmlPullParserException, IOException {
return createAnimationFromXml(c, parser, null, Xml.asAttributeSet(parser));
}
private static Animation createAnimationFromXml(Context c, XmlPullParser parser,
AnimationSet parent, AttributeSet attrs) throws XmlPullParserException, IOException {
Animation anim = null;
// Make sure we are on a start tag.
int type;
int depth = parser.getDepth();
while (((type=parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth)
&& type != XmlPullParser.END_DOCUMENT) {
if (type != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals("set")) {
anim = new AnimationSet(c, attrs);
createAnimationFromXml(c, parser, (AnimationSet)anim, attrs);
} else if (name.equals("alpha")) {
anim = new AlphaAnimation(c, attrs);
} else if (name.equals("scale")) {
anim = new ScaleAnimation(c, attrs);
} else if (name.equals("rotate")) {
anim = new RotateAnimation(c, attrs);
} else if (name.equals("translate")) {
anim = new TranslateAnimation(c, attrs);
} else {
throw new RuntimeException("Unknown animation name: " + parser.getName());
}
if (parent != null) {
parent.addAnimation(anim);
}
}
return anim;
}
最終會調(diào)用XmlPullParser,基于事件驅(qū)動模型來解析xml文件中的動畫律适,從代碼中可以看出來遏插,每一個節(jié)點都對應一個相應的View動畫類,這樣與實際寫代碼差不多厂僧,在Java代碼中也是先創(chuàng)建出相應的view動畫類胎围,然后按照順序添加到集合中。