2017-01-06 53 views
0

我有ImageView,當我點擊按鈕時,它應該飛走。爲此,我使用ObjectAnimator來爲TRANSLATION_Y和TRANSLATION_X屬性設置動畫。如何爲ViewGroup製作圓形剪輯邊界

我需要爲我的ImageView的父級定義循環邊界,以使其正確飛行。

爲此,我用下面的代碼

public class CircleFrameLayout extends FrameLayout { 

    private Path mClipPath = new Path(); 

    //Constructors 

    @Override 
    protected void onDraw(Canvas canvas) { 
     mClipPath.reset(); 
     float radius = Math.min((float)getMeasuredWidth()/2f, (float)getMeasuredHeight()/2f) + 5; 
     mClipPath.addCircle((float)getMeasuredWidth()/2f, (float)getMeasuredHeight()/2f, radius, Path.Direction.CCW); 
     canvas.clipPath(mClipPath); 
     super.onDraw(canvas); 
    } 
} 

但沒有任何反應。 ImageView使用其父'的邊界而不是圓形邊界。

什麼問題?

+0

覆蓋'draw',不'onDraw' – pskink

+0

沒有幫助不幸 –

+0

添加一些'Log.d'報表,並確保params用於在' addCircle'是正確的 – pskink

回答

1

onDraw通常不會被稱爲ViewGroup類(就像您的自定義FrameLayout)。爲了得到你想要的行爲,覆蓋dispatchDraw代替:

private Path mClipPath = new Path(); 

@Override 
protected void dispatchDraw(Canvas canvas) { 
    mClipPath.reset(); 
    float radius = Math.min((float)getMeasuredWidth()/2f, (float)getMeasuredHeight()/2f) + 5; 
    mClipPath.addCircle((float)getMeasuredWidth()/2f, (float)getMeasuredHeight()/2f, radius, Path.Direction.CCW); 
    canvas.clipPath(mClipPath); 
    super.dispatchDraw(canvas); 
}