【问题标题】:Translation animation on button having issue, not responding as per expected按钮上的翻译动画有问题,没有按预期响应
【发布时间】:2015-07-14 21:32:49
【问题描述】:

我是安卓新手。我正在尝试在按钮上翻译动画。我在 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>

【问题讨论】:

    标签: android translate-animation


    【解决方案1】:

    我建议您在 /res/anim 文件夹中创建一个 xml 文件,并使用它来为您的按钮设置动画。带有android:fromYDeltaandroid:toYDelta 属性的&lt;translate&gt; 标签可能会很有帮助(这两个属性都与按钮的垂直移动有关),并且在这里更容易,因为您的问题属于翻译动画问题的类别.如果您想考虑相同,您可能会找到一些很好的解释here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多