2015-07-13 51 views
1

我是Android的新手。我正在尝试翻译按钮上的动画。我在xml中添加了一个按钮,点击后,另一个按程序添加的按钮从其当前位置移动到屏幕顶部并被修复。但问题是,它不是从目前的位置开始,而是从下面的某个位置开始,而不是固定在顶端。请有人帮助我,如果他们可以。提前致谢。 这里是我的代码,我使用: -按钮上的翻译动画有问题,未按预期做出响应

ll= (LinearLayout) findViewById(R.id.ll); 
    final int width = this.getResources().getDisplayMetrics().widthPixels; 
    final int height= this.getResources().getDisplayMetrics().heightPixels; 


    final Button btn=new Button(MainActivity.this); 
    btn.setText("Message"); 
    btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
    btn.setX(width/3); 
    btn.setY(height/3); 
    ll.addView(btn); 

    Button btn_click= (Button) findViewById(R.id.buttonbottom); 
    btn_click.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
    Animation animate= new TranslateAnimation(btn.getX(),btn.getX(), btn.getY(), 0); 
    animate.setDuration(7000); 
    btn.startAnimation(animate); 
    } 
}); 
} 

和它的XML是: -

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

<Button 
    android:id="@+id/buttonbottom" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Click Me" /> 
    </LinearLayout> 

回答

0

我建议你在你的/res/anim文件夹中创建一个XML文件,并用它来动画的按钮。具有android:fromYDeltaandroid:toYDelta作为其属性的<translate>标记可能被证明是有帮助的(这两个属性都与垂直按钮的移动有关),并且在这里更容易,因为您的问题属于翻译动画问题的类别。如果你想考虑一样的话,你可能会找到一些很好的解释here