2016-12-09 22 views
0

這裏是我的Android應用程序在Android Studio代碼:我想用動畫更改邊距以在我的Android應用中移動此邊距。怎麼做?

FrameLayout root = (FrameLayout) findViewById(R.id.root); 
    ImageView img = new ImageView(this); 
    img.setBackgroundColor(Color.RED); 
    //..load something inside the ImageView, we just set the background color 

    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(20, 20); 
    params.leftMargin=Math.round(deltaX); 
    params.topMargin=Math.round(deltaY); 
    root.addView(img,params); 
    //... 

我想與動畫中移動這個時候TOPMARGIN和LEFTMARGIN變化值。

回答

0

你可以改變Padding

FrameLayout root = (FrameLayout) findViewById(R.id.root); 
    ImageView img = new ImageView(this); 
    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(20, 20); 
    img.setLayoutParams(params); 
    img.setBackgroundColor(Color.RED); 
    //..load something inside the ImageView, we just set the background color 

    root.setPadding(deltaX, deltaX, 0, 0); 
    root.addView(img); 

然後,您可以使用Property Animation

ObjectAnimator.ofFloat(root, "translationX", 0, 1000).start(); 
    ObjectAnimator.ofFloat(root, "translationY", 0, 1000).start(); 

root.animate().translationY(100); 
+0

謝謝你..你的回答對我很有幫助。你能告訴我如何讓動畫流暢嗎?我認爲可以使用爲動畫製作的那些類來完成。 –

+0

@FaruqueAhamedMollick更新。 – Yat3s