创建控件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.myapplication.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/bt_tranla"
android:onClick="translation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="位移"
/>
<Button
android:id="@+id/bt_rotation"
android:onClick="rotation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="旋转"
/>
<Button
android:id="@+id/bt_alpha"
android:onClick="alpha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="透明"
/>
<Button
android:id="@+id/bt_scale"
android:onClick="scale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="缩放"
/>
</LinearLayout>
<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="150dp"
android:src="@mipmap/ic_launcher"
/>
</LinearLayout>
主类中的JAVA码
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
/**
* 位移
*/
private Button bt_tranla;
/**
* 旋转
*/
private Button mBtRotation;
/**
* 透明
*/
private Button mBtAlpha;
/**
* 缩放
*/
private Button mBtScale;
/**
* 位移
*/
private Button mBtTranla;
private ImageView mImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
bt_tranla = (Button) findViewById(R.id.bt_tranla);
bt_tranla.setOnClickListener(this);
mBtRotation = (Button) findViewById(R.id.bt_rotation);
mBtRotation.setOnClickListener(this);
mBtAlpha = (Button) findViewById(R.id.bt_alpha);
mBtAlpha.setOnClickListener(this);
mBtScale = (Button) findViewById(R.id.bt_scale);
mBtScale.setOnClickListener(this);
mBtTranla = (Button) findViewById(R.id.bt_tranla);
mBtTranla.setOnClickListener(this);
mImageView = (ImageView) findViewById(R.id.imageView);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_tranla:
ObjectAnimator object_tranla = ObjectAnimator.ofFloat(bt_tranla, "translationX", 0, 10, 20, 50, 300);
object_tranla.setDuration(5000);
object_tranla.setRepeatCount(ObjectAnimator.INFINITE);
object_tranla.start();
break;
case R.id.bt_rotation:
ObjectAnimator object_rotation = ObjectAnimator.ofFloat(mImageView, "rotation", 0, 10, 20, 50, 300);
object_rotation.setDuration(5000);
object_rotation.start();
break;
case R.id.bt_alpha:
ObjectAnimator object_alpha = ObjectAnimator.ofFloat(mImageView, "alpha", 1.0f, 0.9f, 0.6f,0);
object_alpha.setDuration(5000);
object_alpha.start();
break;
case R.id.bt_scale:
ObjectAnimator object_scale = ObjectAnimator.ofFloat(mImageView, "scaleX", 1,0);
object_scale.setDuration(5000);
object_scale.start();
break;
}
}
}