作为教程,当我按下它时,我想使用线性动画在屏幕上移动按钮。但是,我使用的objectanimator直接进入最终值,没有补间。但只有通过Onclick事件调用它。在OnCreate直接调用它时工作正常。我错过了什么?
这是我的代码:(对于调试,目标对象替换为' this'以显示被调用的getter和setter)
package com.example.tweakerslayout;
import android.animation.ObjectAnimator;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
float y =500;
public float getY(){
System.out.println("getY was called");
return y;
}
public void setY(float value){
System.out.println("Y setter called with value: " + value);
y = value;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Animate();
final Button myButton = (Button)findViewById(R.id.myButton);
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animate2();
}
});
}
public void Animate(){
ObjectAnimator animY = ObjectAnimator.ofFloat(MainActivity.this, "y", 100f);
animY.setInterpolator(new LinearInterpolator());
animY.setDuration(2000);
animY.start();
}
public void Animate2(){
ObjectAnimator animY = ObjectAnimator.ofFloat(MainActivity.this, "y", 300f);
animY.setInterpolator(new LinearInterpolator());
animY.setDuration(2000);
animY.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我的xml activity_main:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp">
<TextView
android:id="@+id/myText"
android:text="Hello, we're just testing" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/myButton"
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tweakerslayout" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
</resources>
打印输出 - 最后几行是我点击按钮的时候:
05-23 12:26:09.417 25524-25524/com.example.tweakerslayout D/OpenGLRenderer﹕ Enabling debug mode 0
05-23 12:26:09.447 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 480.0
05-23 12:26:09.507 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 467.0
05-23 12:26:09.517 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 463.6
05-23 12:26:09.527 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 460.4
05-23 12:26:09.537 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 457.0
05-23 12:26:09.557 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 453.8
05-23 12:26:09.577 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 450.4
05-23 12:26:09.587 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 447.2
05-23 12:26:09.607 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 443.8
05-23 12:26:09.627 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 440.6
05-23 12:26:09.637 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 437.2
05-23 12:26:09.657 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 434.0
05-23 12:26:09.677 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 430.6
05-23 12:26:09.687 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 427.4
05-23 12:26:09.707 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 424.0
05-23 12:26:09.727 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 420.8
05-23 12:26:09.737 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 417.4
05-23 12:26:09.757 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 414.2
05-23 12:26:09.777 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 410.8
05-23 12:26:09.787 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 407.6
05-23 12:26:09.807 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 404.2
05-23 12:26:09.827 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 401.0
05-23 12:26:09.837 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 397.6
05-23 12:26:09.857 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 394.4
05-23 12:26:09.867 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 391.0
05-23 12:26:09.887 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 387.8
05-23 12:26:09.907 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 384.4
05-23 12:26:09.917 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 381.2
05-23 12:26:09.937 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 377.8
05-23 12:26:09.957 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 374.4
05-23 12:26:09.967 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 371.2
05-23 12:26:09.987 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 367.8
05-23 12:26:10.007 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 364.6
05-23 12:26:10.017 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 361.2
05-23 12:26:10.037 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 358.0
05-23 12:26:10.057 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 354.6
05-23 12:26:10.067 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 351.40002
05-23 12:26:10.087 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 348.0
05-23 12:26:10.107 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 344.8
05-23 12:26:10.117 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 341.40002
05-23 12:26:10.137 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 338.2
05-23 12:26:10.157 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 334.8
05-23 12:26:10.167 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 331.6
05-23 12:26:10.187 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 328.2
05-23 12:26:10.207 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 325.0
05-23 12:26:10.217 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 321.59998
05-23 12:26:10.237 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 318.4
05-23 12:26:10.247 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 315.0
05-23 12:26:10.267 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 311.8
05-23 12:26:10.287 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 308.4
05-23 12:26:10.297 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 305.2
05-23 12:26:10.317 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 301.8
05-23 12:26:10.337 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 298.6
05-23 12:26:10.347 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 295.2
05-23 12:26:10.367 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 292.0
05-23 12:26:10.387 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 288.59998
05-23 12:26:10.397 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 285.2
05-23 12:26:10.417 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 282.0
05-23 12:26:10.437 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 278.6
05-23 12:26:10.447 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 275.4
05-23 12:26:10.467 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 272.0
05-23 12:26:10.487 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 268.8
05-23 12:26:10.497 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 265.40002
05-23 12:26:10.517 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 262.2
05-23 12:26:10.537 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 258.8
05-23 12:26:10.547 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 255.6
05-23 12:26:10.567 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 252.20001
05-23 12:26:10.587 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 249.0
05-23 12:26:10.597 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 245.6
05-23 12:26:10.617 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 242.4
05-23 12:26:10.637 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 239.0
05-23 12:26:10.647 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 235.80002
05-23 12:26:10.667 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 232.4
05-23 12:26:10.677 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 229.20001
05-23 12:26:10.697 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 225.79999
05-23 12:26:10.717 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 222.6
05-23 12:26:10.727 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 219.19998
05-23 12:26:10.747 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 216.0
05-23 12:26:10.767 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 212.6
05-23 12:26:10.777 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 209.40002
05-23 12:26:10.797 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 206.0
05-23 12:26:10.817 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 202.80002
05-23 12:26:10.827 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 199.4
05-23 12:26:10.847 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 196.19998
05-23 12:26:10.867 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 192.79999
05-23 12:26:10.877 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 189.4
05-23 12:26:10.897 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 186.20001
05-23 12:26:10.917 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 182.80002
05-23 12:26:10.927 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 179.6
05-23 12:26:10.947 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 176.20001
05-23 12:26:10.967 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 173.0
05-23 12:26:10.977 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 169.6
05-23 12:26:10.997 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 166.4
05-23 12:26:11.007 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 163.0
05-23 12:26:11.027 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 159.80002
05-23 12:26:11.047 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 156.4
05-23 12:26:11.057 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 153.20001
05-23 12:26:11.077 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 149.79999
05-23 12:26:11.097 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 146.6
05-23 12:26:11.107 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 143.19998
05-23 12:26:11.127 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 140.0
05-23 12:26:11.147 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 136.6
05-23 12:26:11.157 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 133.40002
05-23 12:26:11.177 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 130.0
05-23 12:26:11.197 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 126.79999
05-23 12:26:11.207 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 123.399994
05-23 12:26:11.227 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 120.19998
05-23 12:26:11.247 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 116.79999
05-23 12:26:11.257 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 113.600006
05-23 12:26:11.277 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 110.20001
05-23 12:26:11.297 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 107.0
05-23 12:26:11.307 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 103.600006
05-23 12:26:11.327 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 100.20001
05-23 12:26:11.347 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 100.0
05-23 12:26:20.937 25524-25524/com.example.tweakerslayout I/System.out﹕ getY was called
05-23 12:26:20.937 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 300.0
05-23 12:26:20.957 25524-25524/com.example.tweakerslayout I/System.out﹕ Y setter called with value: 300.0