請問:簡書怎么可以把代碼格式調(diào)整?我貼出來換格式了。你們直接去Github下載工程!

分析動畫
首先分析動畫,初始狀態(tài)是由兩個相切的圓形圖案組成。 將動畫拆解為兩部分看。
假象:表面看是旋轉(zhuǎn),其實是移動導(dǎo)致視覺差
?1.中間,先往左邊,然后往右邊到中心點。然后切換顏色
? ? ?中間,先往右邊,然后往左邊到中心點,切換顏色
2.同第一步相同,只是兩小球變換位置。
具體實現(xiàn)
1.繪制藍(lán)色小球,繪制紅色小球。防止在中心的位置
2.藍(lán)色小球位移和縮放動畫
3.相交的部分變黑:
```
注意地方
1.小圓的繪制
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
? ? float cx = getWidth()/2;//對于控件來說
? ? float cy = getHeight()/2;
? ? canvas.drawCircle(cx, cy, 30, paint);
}
2.位置的放置
RelativeLayout.LayoutParams layoutParams =new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(CENTER_IN_PARENT);//設(shè)置位置居中
circleViewRedLeft =new CircleView(getContext());
circleViewBlueRight =new CircleView(getContext());
addView(circleViewRedLeft, layoutParams);
addView(circleViewBlueRight, layoutParams);
3.移動動畫
ObjectAnimator animatorleft = ObjectAnimator.ofFloat(circleViewRedLeft, "translationX", 0, -translationX);//left move
ObjectAnimator animatorRight = ObjectAnimator.ofFloat(circleViewBlueRight, "translationX", 0, translationX);
4.ValueAnimator和ObjectAnimator 區(qū)別:
ObjectAnimator?:可以設(shè)置是旋轉(zhuǎn),移動,和arg
ValueAnimator不能設(shè)置這,只能設(shè)置值!主要應(yīng)用:通過監(jiān)聽得到value。然后進(jìn)行繪制
```
具體代碼:
```
具體代碼:
public class DouYinLayoutextends RelativeLayout {
public DouYinLayout(Context context) {
this(context, null);
? ? }
public DouYinLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
? ? ? ? init();
? ? }
public DouYinLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
? ? }
private CircleViewcircleViewRedLeft, circleViewBlueRight;
? ? private float translationX =30;
? ? /**
? ? * 把2個點添加進(jìn)去
? ? */
? ? private void init() {
RelativeLayout.LayoutParams layoutParams =new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
? ? ? ? layoutParams.addRule(CENTER_IN_PARENT);//設(shè)置位置居中
? ? ? ? circleViewRedLeft =new CircleView(getContext());
? ? ? ? circleViewBlueRight =new CircleView(getContext());
? ? ? ? addView(circleViewRedLeft, layoutParams);
? ? ? ? addView(circleViewBlueRight, layoutParams);
? ? ? ? circleViewRedLeft.changeCorlor(Color.RED);
? ? ? ? circleViewBlueRight.changeCorlor(Color.BLUE);
? ? ? ? oneAnimation();
? ? }
private void oneAnimation() {
//動起來:中間的先向左移動。然后向右移動。然后不停的循環(huán)
? ? ? ? ObjectAnimator animatorleft = ObjectAnimator.ofFloat(circleViewRedLeft, "translationX", 0, -translationX);//left move
? ? ? ? ObjectAnimator animatorRight = ObjectAnimator.ofFloat(circleViewBlueRight, "translationX", 0, translationX);
? ? ? ? AnimatorSet animatorSet =new AnimatorSet();
? ? ? ? animatorSet.playTogether(animatorleft, animatorRight);
? ? ? ? animatorSet.setDuration(300);//
? ? ? ? animatorSet.start();
? ? ? ? animatorSet.addListener(new AnimatorListenerAdapter() {
@Override
? ? ? ? ? ? public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
? ? ? ? ? ? ? ? circleViewRedLeft.changeCorlor(Color.BLUE);
? ? ? ? ? ? ? ? circleViewBlueRight.changeCorlor(Color.RED);
? ? ? ? ? ? ? ? twoAnimation();
? ? ? ? ? ? }
});
? ? }
private void twoAnimation() {
ObjectAnimator animatorleft = ObjectAnimator.ofFloat(circleViewRedLeft, "translationX", -translationX, 0);//left move
? ? ? ? ObjectAnimator animatorRight = ObjectAnimator.ofFloat(circleViewBlueRight, "translationX", translationX, 0);
? ? ? ? AnimatorSet animatorSet =new AnimatorSet();
? ? ? ? animatorSet.playTogether(animatorleft, animatorRight);
? ? ? ? animatorSet.setDuration(300);//
? ? ? ? animatorSet.start();
? ? ? ? animatorSet.addListener(new AnimatorListenerAdapter() {
@Override
? ? ? ? ? ? public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
? ? ? ? ? ? ? ? circleViewRedLeft.changeCorlor(Color.RED);
? ? ? ? ? ? ? ? circleViewBlueRight.changeCorlor(Color.BLUE);
? ? ? ? ? ? ? ? oneAnimation();
? ? ? ? ? ? }
});
? ? }
}
```
動畫的兩種實現(xiàn):setX跟setTranslationX區(qū)別
Canvas.save()跟Canvas.restore()的調(diào)用時機(jī)
區(qū)別Animation和Animator的用法,概述其原理
動畫優(yōu)化總結(jié)
1.在setVivible的時候,去移除一些view和圖片
2.在ondestory的時候,取消動畫等
3.動畫的類,不要循環(huán)創(chuàng)建變量。因為動畫是循環(huán)的!!
4.過渡繪制,最主要的就是背景多重復(fù)制了