2014-09-11 46 views
1

我遇到以下問題: 我在android上創建了自定義視圖。我想通過縮放動畫(pivotX =「50%」和pivotY =「50%」)製作視圖脈衝。 如果視圖放置在屏幕中間,則縮放工作正常。如果視圖放置在屏幕的左側,則看起來pivotX值將爲「75%」。 如果視圖放置在屏幕的右側,則看起來pivotX值將是「25%」。自定義視圖上的縮放動畫在未放置在屏幕中間時表現不正確

這是我的看法的代碼:

public class TestView extends View 
{ 
    private Paint _paint = new Paint(); 

    private int _x, _y; 

    public TestView(Context context, int x, int y) 
    { 
    super(context); 

    _x = x; 
    _y = y; 

    _paint.setAntiAlias(true); 
    _paint.setColor(Color.BLUE); 
    _paint.setStyle(Style.FILL); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) 
    { 
    super.onDraw(canvas); 
    canvas.drawCircle(_x, _y, 100 , _paint); 
    } 
} 

這裏是動畫pulse.xml文件的代碼:

<scale xmlns:android="http://schemas.android.com/apk/res/android" 
android:duration="100" 
android:fromXScale="1" 
android:fromYScale="1" 
android:pivotX="50%" 
android:pivotY="50%" 
android:repeatCount="3" 
android:repeatMode="reverse" 
android:toXScale="1.7" 
android:toYScale="1.7" > 
</scale> 

片段類代碼:

ViewGroup mainLayout = (ViewGroup) getView().findViewById(R.id.my_layout);  

float y = screenHeigth/2; 

float x1 = screenWidth*3/4; // right hand side of the screen 
dest1 = new TestView(getActivity(), x1, y); 
mainLayout.addView(dest1); 

float x2 = screenWidth/2; // in the middle of the screen 
dest2 = new TestView(getActivity(), x2, y); 
mainLayout.addView(dest2); 

float x3 = screenWidth/4; // left hand side of the screen 
dest3 = new TestView(getActivity(), x3, y); 
mainLayout.addView(dest3); 

ScaleAnimation pulse = (ScaleAnimation) AnimationUtils.loadAnimation(getActivity(), R.anim.pulse); 

dest1.startAnimation(pulse); 
dest2.startAnimation(pulse); 
dest3.startAnimation(pulse); 

我在做什麼錯?也許我必須在我的TestView類中做些什麼?

+0

什麼PARAMS x和在TestView構造函數y代表? – pskink 2014-09-11 12:46:19

+0

這是用於繪製特定位置上的圓圈。 - > canvas.drawCircle(_x,_y,100,_paint);圈子的座標 – manu 2014-09-11 12:56:28

+0

和主要佈局是什麼? – pskink 2014-09-11 13:06:01

回答

0

現在我明白了:-)謝謝pskink讓我走上正確的道路。 問題是,我的TestView填充了整個父視圖。因此,我認爲的中心是父視圖的中心,而不是繪製圓的中心。

這裏是解決方案:

public class TestView extends View 
{ 
    private Paint _paint = new Paint(); 

    private int _xRelativeToView, _yRelativeToView, _w, _radius; 

    public TestView(Context context, int xOfView, int yOfView) 
    { 
    super(context); 

    _w = 100; 
    _radius = _w/2; 

    // set the coordinates of the circle within this view 
    _xRelativeToView = _radius; 
    _yRelativeToView = _radius; 

    // set the position of the view on the screen: 
    LayoutParams mparam = new LayoutParams(_w, _w); // the view is as big as the circle 
    mparam.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
    mparam.setMargins(xOfView - _radius, yOfView - _radius, 0, 0); 
    setLayoutParams(mparam); 

    // initialize paint 
    _paint.setAntiAlias(true); 
    _paint.setColor(Color.BLUE); 
    _paint.setStyle(Style.FILL); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) 
    { 
    super.onDraw(canvas); 
    canvas.drawCircle(_xRelativeToView, _yRelativeToView, _radius, _paint); 
    } 
} 
相關問題