Android 动画之属性动画,移动智能终端开发技术题库
android:layout_weight="1"
android:text="透明动画" />
<Button
android:id="@+id/btn_translate"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:text="位移动画" />
<Button
android:id="@+id/btn_rotate"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:text="旋转动画" />
<Button
android:id="@+id/btn_scale"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:text="缩放动画" />
</LinearLayout>
<ImageView
android:id="@+id/iv_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@mipmap/ic_launcher" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBot
tom="true"
android:onClick="groupshow"
android:text="组合显示" />
</RelativeLayout>
ThirdActivity.java 文件:
//属性动画
public class ThirdActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_alpha;
private Button btn_translate;
private Button btn_rotate;
private Button btn_scale;
private ImageView iv_show;
ObjectAnimator objectAnimator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
initView();
}
private void initView() {
btn_alpha = (Button) findViewById(R.id.btn_alpha);
btn_translate = (Button) findViewById(R.id.btn_translate);
btn_rotate = (Button) findViewById(R.id.btn_rotate);
btn_scale = (Button) findViewById(R.id.btn_scale);
iv_show = (ImageView) findViewById(R.id.iv_show);
btn_alpha.setOnClickListener(this);
btn_translate.setOnClickListener(this);
btn_rotate.setOnClickListener(this);
btn_scale.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//ofFloat:三个参数 :1.受到动画影响的对象(UI 控件)2. 要执行的动画类型 3. 一组动画的属性
switch (v.getId()) {
case R.id.btn_alpha://透明动画
objectAnimator = ObjectAnimator.ofFloat(iv_show, "alpha", 0.5f, 1f, 0.5f, 1f);
break;
case R.id.btn_translate://位移动画
//只会执行一个
objectAnimator = ObjectAnimator.ofFloat(iv_show, "translationX", 0, 200);
//objectAnimator=ObjectAnimator.ofFloat(iv_show,"translationY",0,200);
break;
case R.id.btn_rotate://旋转动画
objectAnimator = ObjectAnimator.ofFloat(iv_show, "rotation", 0, 90f, 180f, 90f, 45f, 100f);
break;
case R.id.btn_scale://缩放动画
//只会执行一个
objectAnimator = ObjectAnimator.ofFloat(iv_show, "scaleY", 1f, 2f, 3f, 4f);
// objectAnimator=ObjectAnimator.ofFloat(iv_show,"scaleX",1f,2f,3f,4f);
break;
}
//动画持续时间
objectAnimator.setDuration(3000);
//启动动画
objectAnimator.start();
}
评论