Android 動畫的一些小總結

View.setScaleX
View.setScaleY

以上兩個方法對View.getWidth()和View.getMeasureWidth()沒有影響,獲得的仍是縮放以前的尺寸。ide

View.setTranslationX

以上方法對View.getLeft()沒影響,獲得的是移動以前的據屏幕左邊的距離,而對View.getX()則有影響,獲得的是移動後的據屏幕左邊的距離。函數

    ***********還有一個很厲害的類就是ViewDragHelper,可用在自定義的View上**************佈局

1.建立這個對象動畫

ViewDragHelper viewDragHelper=ViewDragHelper.create(ViewGroup forParent ,CallBack cb)

forParent :把此佈局做爲父佈局,通常能夠用自定view的this來傳,如今本身 理解的不是很清楚this

源碼以下:就是能夠得到context,以及不能爲空spa

public static ViewDragHelper create(ViewGroup forParent, Callback cb) {
    return new ViewDragHelper(forParent.getContext(), forParent, cb);
}
private ViewDragHelper(Context context, ViewGroup forParent, Callback cb) {
   if (forParent == null) { throw new IllegalArgumentException("Parent view may not be null"); }
    if (cb == null) {
        throw new IllegalArgumentException("Callback may not be null");
    }

    mParentView = forParent;
    mCallback = cb;

    final ViewConfiguration vc = ViewConfiguration.get(context);
    final float density = context.getResources().getDisplayMetrics().density;
    mEdgeSize = (int) (EDGE_SIZE * density + 0.5f);

    mTouchSlop = vc.getScaledTouchSlop();
    mMaxVelocity = vc.getScaledMaximumFlingVelocity();
    mMinVelocity = vc.getScaledMinimumFlingVelocity();
    mScroller = ScrollerCompat.create(context, sInterpolator);
}

2.重寫view的onTouchEvent方法獲得觸摸的對象對象

@Override
public boolean onTouchEvent(MotionEvent event) {
    viewDragHelper.processTouchEvent(event)--獲得觸摸對象,以此來解析觸摸動做
    return true;
}

3.最好重寫view的攔截事件接口

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    return viewDragHelper.shouldInterceptTouchEvent(ev)---讓這個對象來決定攔截與否
}

4.回調接口ViewDragHelper.Callback要重寫的一些重要方法事件

@Override
public boolean tryCaptureView(View child, int pointerId)
                                            ---參數是觸摸過的最上面的子view,以及它的id,若是返回true則可
                                               在後面重寫的方法中處理這個觸摸動做。返回false則不會理會這個
                                               觸摸動做
@Override 
public int clampViewPositionHorizontal(View child, int left, int dx)
                      ---在水平方向上的子view根據觸摸動做來移動
                      ---child:被觸摸的最上面的子view
                      ---left :子view最左邊距離正Y軸的距離,left=child.getLeft()+dx
                      ---dx   :是觸摸移動的距離
                      ---該返回值決定子view的位置,若是返回值爲定值則該view始終最左邊始終在改返回值的位置,
                         言外之意就是這個view無論你怎麼觸摸移動它始終在一個位置,不會根據觸摸來移動 
         
@Override  //和水平方向上重寫的方法相似
public int clampViewPositionVertical(View child, int top, int dy)
                      ---在垂直方向上的子view根據觸摸動做來移動
@Override
public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy)
                      ---在父佈局中的子佈局只要經過clampViewPositionVertical和
                         clampViewPositionHorizontal以及viewDrag.smoothSlideViewTo等相關方法
                         改變了位置則會調用這個方法,當dy,dx等爲0時也會調用。
                      ---left:子view的左邊離正Y軸的距離
                      ---top :子view的上邊離正X軸的距離
                      ---dx  :子view在X軸上的移動距離
                      ---dy  :子view在Y軸上的移動距離
@Override
public void onViewReleased(View releasedChild, float xvel, float yvel) 
                      ---此方法是觸摸子view移動時候釋放時候會被調用
                      ---releasedChild:觸摸子view移動時候釋放的那個子view
                      ---xvel         :沒有釋放時候觸摸滑動的平均移動的在X軸上的速度
                      ---yvel         :沒有釋放時候觸摸滑動的平均移動的在Y軸上的速度

5.ViewDragHelper的可能會用的方法ci

。。。。。

 

*******************************總結TranslateAnimation使用******************************************

1.建立構造函數

//建立
A.TranslateAnimation animation = new TranslateAnimation(
  Animation.RELATIVE_TO_PARENT,-1 ----int fromXType, float fromXValue
  ,Animation.RELATIVE_TO_PARENT, 1 ----int toXType, float toXValue ,Animation.RELATIVE_TO_PARENT, 0 ----int fromYType, float fromYValue,
  ,Animation.RELATIVE_TO_PARENT, 0 ----int toYType, float toYValue );

Type:  Animation.ABSOLUTE(絕對的屏幕座標,單位爲像素)

          Animation.RELATIVE_TO_SELF(相對身的寬度和高度的幾倍)

          Animation.RELATIVE_TO_PARENT(相對父佈局的寬度和高度的幾倍)

          Valuse: 控件的左邊的擺放位置

//建立
B.public TranslateAnimation (
                   float fromXDelta
                   float toXDelta, 
                   float fromYDelta, 
                   float toYDelta)

Delta:與控件原始位置的距離差

2.相關設置:

animation.setDuration(12000)--設置動畫運行時間
animation.setRepeatMode(TranslateAnimation.RESTART)--設置動畫重複的模式(從新開始,倒退回來等)
animation.setInterpolator(new LinearInterpolator())--設置動畫的加速器(有勻速,加速等)
animation.setRepeatCount(TranslateAnimation.INFINITE)--設置動畫的重複次數(一直重複等)
animation.setAnimationListener--設置監聽器,能夠監控到動畫的開始結束以及開始重複等動做
相關文章
相關標籤/搜索