属性动画简单的使用
程序员文章站
2022-05-02 22:14:29
...
MainActivity代码
package likuo.bwie.com.donghua;
import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageButton imageView;
private Button alpha_bt;
private Button rotationY_bt;
private Button scaleX_bt;
private Button translationX_bt;
private Button AnimatorSet_bt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
//静态的初始就有一个动画需要在res文件加入animator加入一个xml 我的objectanimator.xml
Animator Xmlanimator = AnimatorInflater.loadAnimator(this, R.animator.objectanimator);
Xmlanimator.setTarget(imageView);
Xmlanimator.start();
}
private void init() {
imageView = findViewById(R.id.animation_iv);
alpha_bt = findViewById(R.id.alpha_bt);
rotationY_bt = findViewById(R.id.rotationY_bt);
scaleX_bt = findViewById(R.id.scaleX_bt);
translationX_bt = findViewById(R.id.translationY_bt);
AnimatorSet_bt = findViewById(R.id.AnimatorSet_bt);
alpha_bt.setOnClickListener(this);
rotationY_bt.setOnClickListener(this);
scaleX_bt.setOnClickListener(this);
translationX_bt.setOnClickListener(this);
AnimatorSet_bt.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.alpha_bt:
ObjectAnimator alpha = ObjectAnimator.ofFloat(imageView, "alpha", new float[]{0.0f, 0.2f, 0.4f, 0.6f, 0.8f, 1.0f});
alpha.setDuration(2000);
alpha.setRepeatMode(ObjectAnimator.RESTART);
alpha.setRepeatCount(1);
alpha.start();
break;
case R.id.rotationY_bt:
ObjectAnimator rotationY = ObjectAnimator.ofFloat(imageView, "rotationY", new float[]{90f, 180f, 270f, 360f});
rotationY.setDuration(2000);
rotationY.setRepeatMode(ObjectAnimator.RESTART);
rotationY.setRepeatCount(1);
rotationY.start();
break;
case R.id.scaleX_bt:
ObjectAnimator scaleX = ObjectAnimator.ofFloat(imageView, "scaleX", new float[]{1f, 2f, 3f, 4f, 5f, 6f, 1f});
scaleX.setDuration(2000);
scaleX.setRepeatMode(ObjectAnimator.RESTART);
scaleX.setRepeatCount(1);
scaleX.start();
break;
case R.id.translationY_bt:
ObjectAnimator translationY = ObjectAnimator.ofFloat(imageView, "translationY", new float[]{10f, 20f, 30f, 40f, 60f, 80f});
translationY.setDuration(2000);
translationY.setRepeatMode(ObjectAnimator.RESTART);
translationY.setRepeatCount(1);
translationY.start();
case R.id.AnimatorSet_bt:
AnimatorSet set = new AnimatorSet();
ObjectAnimator oa = ObjectAnimator.ofFloat(imageView, "translationX", new float[]{10f, 20f, 30f, 40f, 60f, 80f});
oa.setDuration(3000);
ObjectAnimator oa2 = ObjectAnimator.ofFloat(imageView, "translationY", new float[]{-10f, -20f, -30f, -40f, -60f, -80f});
oa2.setDuration(3000);
set.playTogether(oa, oa2);
set.start();
break;
default:
break;
}
}
public void yyyy(View v) {
System.out.println("思达高科,斯大林说看风景");
}
}
布局文件。。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- 注意background的属性置为null -->
<Button
android:id="@+id/alpha_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="渐变"/>
<Button
android:id="@+id/translationY_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="平移"/>
<Button
android:id="@+id/scaleX_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="拉伸"/>
<Button
android:id="@+id/rotationY_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="旋转"/>
<Button
android:id="@+id/AnimatorSet_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="集合"/>
<ImageButton
android:id="@+id/animation_iv"
android:layout_width="256dp"
android:layout_height="264dp"
android:layout_gravity="center"
android:background="@null"
android:onClick="yyyy"
android:src="@mipmap/ic_launcher" />
</LinearLayout>
下面是那个xml文件objectanimator.xml
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="rotationX"
android:duration="3000"
android:repeatCount="1"
android:repeatMode="reverse"
android:startOffset="0"
android:valueFrom="360.0">
</objectAnimator>
<!--注意:在xml定义动画类的属性,浮点型小数,直接写小数即可,不用再带f.
提示:控件位移的参照单位在xml文件里有所不同,不过更简单,不用再特意去定义参照物属性了,直接是根据值,区分两种方式:
一种是以整个屏幕为参照物,在xml文件属性定义值是int%p; 一种以控件自身大小为参照物,在xml文件属性定义值是int-->
下一篇: android-属性动画