2011-11-29 67 views
1

我想把一個縮放動畫應用到一個矩形,使用的代碼不是xml。我想讓矩形變長,非常簡單。爲什麼在Android中應用ScaleAnimation時,drawable的位置也會發生變化?

RectDrawableView - 創建RectShape ShapeDrawable

public class RectDrawableView extends View { 

Paint paint; 

public RectDrawableView(Context context) { 
    super(context); 
    paint = new Paint(); 
    paint.setColor(Color.parseColor("#aacfcce4")); 
    paint.setStyle(Paint.Style.FILL); 
} 

protected void onDraw(Canvas canvas) { 

    int x = 35; 
    int y = 250; 
    int width = 50; 
    int height = 300; 

    ShapeDrawable shapeDrawable = new ShapeDrawable(new RectShape()); 

    Rect rect = new Rect(x, y, x+width, y+height); 
    shapeDrawable.setBounds(rect); 
    shapeDrawable.getPaint().set(paint); 
    shapeDrawable.draw(canvas); 
} 

}

活動: - 創建一個新RectDrawableView並增加了佈局。 OnResume會觸發一個動畫,這會使矩形變長。

public class RectActivity extends Activity { 

final String TAG = "RectActivity"; 

TextView rect1; 
RectDrawableView rectView; 


/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.basic); 

    LinearLayout linear = (LinearLayout) findViewById(R.id.basicLayout); 
    rectView = new RectDrawableView(this); 
    linear.addView(rectView); 
} 

@Override 
public void onResume() { 
    super.onResume(); 
    RunAnimations(); 
} 


private void RunAnimations() { 


    Animation rectAnim1 = new ScaleAnimation(1, 1, 1, 1.3f, ScaleAnimation.RELATIVE_TO_SELF, 0, 
      ScaleAnimation.RELATIVE_TO_SELF, 1); 
    rectAnim1.setFillBefore(false); 
    rectAnim1.setFillAfter(true); 
    rectAnim1.setStartOffset(500); 
    rectAnim1.setDuration(500); 

    rectView.startAnimation(rectAnim1); 

} 
} 

basic.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/basicLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 

</LinearLayout> 

結果:矩形是長高,好,但矩形的位置也在不斷變化,不好。整個矩形位於屏幕的上方。

當我使用完全相同的動畫代碼,但將其應用於TextView而不是ShapeDrawable時,一切都很好。

我已經瀏覽了所有關於SO的相關文章,但我仍然在爲這一篇而努力。任何幫助將不勝感激,謝謝。

回答

3

完整代碼,我改變了矩形的高度。

public class RectActivity extends Activity { 

final String TAG = "RectActivity"; 

TextView rect1; 
RectDrawableView rectView; 


/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.basic); 

    LinearLayout linear = (LinearLayout) findViewById(R.id.rectangle); 
    rectView = new RectDrawableView(this); 
    linear.addView(rectView); 
    Button sbutton = (Button)findViewById(R.id.sbutton); 
    sbutton.setOnClickListener(new OnClickListener(){ 
     @Override 
     public void onClick(View v) { 
      RectActivity.this.RunAnimations(); 
     } 
    }); 
    Button tbutton = (Button)findViewById(R.id.tbutton); 
    tbutton.setOnClickListener(new OnClickListener(){ 
     @Override 
     public void onClick(View v) { 
      RectActivity.this.runTranslateAnimation(); 
     } 
    }); 

} 

@Override 
public void onResume() { 
    super.onResume(); 
    RunAnimations(); 
} 


private void RunAnimations() { 
    Animation rectAnim1 = new ScaleAnimation(1, 1, 1, 1.3f, 
        ScaleAnimation.RELATIVE_TO_SELF, 0, 
      ScaleAnimation.ABSOLUTE, 250); 

    rectAnim1.setFillBefore(false); 
    rectAnim1.setFillAfter(true); 
    rectAnim1.setStartOffset(500); 
    rectAnim1.setDuration(500); 
    rectView.startAnimation(rectAnim1); 

} 
private void runTranslateAnimation() { 

    float x = rectView.getX(); 
    float y = rectView.getY(); 

    TranslateAnimation trans = new TranslateAnimation(0, 0, 0, (90)); 
    trans.setFillAfter(true); 
    trans.setStartOffset(500); 
    trans.setDuration(500); 
    rectView.startAnimation(trans); 
} 
} 

public class RectDrawableView extends View { 

Paint paint; 
Rect rect; 

public RectDrawableView(Context context) { 
    super(context); 
    paint = new Paint(); 
    paint.setColor(Color.parseColor("#aacfcce4")); 
    paint.setStyle(Paint.Style.FILL); 
} 

protected void onDraw(Canvas canvas) { 

    int x = 50; 
    int y = 150; 
    int width = 50; 
    int height = 100; 

    ShapeDrawable shapeDrawable = new ShapeDrawable(new RectShape()); 

    rect = new Rect(x, y, x+width, y+height); 
    shapeDrawable.setBounds(rect); 
    shapeDrawable.getPaint().set(paint); 
    shapeDrawable.draw(canvas); 
} 
} 
+0

嘗試使用上面的代碼,其工作原理ics –

+0

感謝Rajdeep - 第一次正確回答,然後跟進了一個好的工作示例。 – electricSunny

1

試試看看這個代碼。保持pivotYType爲ABSOLUTE與價值550 (y + height)

Animation rectAnim1 = new ScaleAnimation(1, 1, 1, 1.3f, 
           ScaleAnimation.RELATIVE_TO_SELF, 0, 
           ScaleAnimation.ABSOLUTE, 550); 
+0

這是有點令人驚訝,這對我很有用 –

+0

作品像一個魅力 - 謝謝。雖然我仍然試圖理解爲什麼它在xml中使用RELATIVE_TO_SELF,但不是在代碼中。 – electricSunny

+0

我的第一條評論,我刪除了,是錯誤的 - 我正在編輯錯誤的問題! - 道歉 – electricSunny

相關問題