ScaleAnimation(缩放动画)
缩放的意思就是对图片或者文字等进行扩大或缩小。下面开始编写代码,相关重要属性参数的解释都在代码中。
1、编写main.xml文件。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<ImageView
android:id="@+id/image"
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/car_one1"/>
</RelativeLayout>
2、编写MainActivity.java文件。
package com.example.dell.bitmapproject;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image =(ImageView)findViewById(R.id.image);
image.setOnClickListener(new OnClickListenerImpl());
}
private class OnClickListenerImpl implements View.OnClickListener {
@Override
public void onClick(View v) {
/*
AnimationSet相当于一个动画的集合,true表示使用Animation的interpolator
false则是使用自己的。
Interpolator 被用来修饰动画效果,定义动画的变化率,可以使存在的动画效果
accelerated(加速),decelerated(减速),repeated(重复),bounced(弹跳)等。
*/
AnimationSet animationSet = new AnimationSet(true);
/*
参数解释:
第一个参数:X轴水平缩放起始位置的大小(fromX)。1代表正常大小
第二个参数:X轴水平缩放完了之后(toX)的大小,0代表完全消失了
第三个参数:Y轴垂直缩放起始时的大小(fromY)
第四个参数:Y轴垂直缩放结束后的大小(toY)
第五个参数:pivotXType为动画在X轴相对于物件位置类型
第六个参数:pivotXValue为动画相对于物件的X坐标的开始位置
第七个参数:pivotXType为动画在Y轴相对于物件位置类型
第八个参数:pivotYValue为动画相对于物件的Y坐标的开始位置
(第五个参数,第六个参数),(第七个参数,第八个参数)是用来指定缩放的中心点
0.5f代表从中心缩放
*/
ScaleAnimation scaleAnimation = new ScaleAnimation(1,0.5f,1,0.5f,
Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
//3秒完成动画
scaleAnimation.setDuration(2000);
//将AlphaAnimation这个已经设置好的动画添加到 AnimationSet中
animationSet.addAnimation(scaleAnimation);
//启动动画
MainActivity.this.image.startAnimation(animationSet);
}
}
}
其实关于动画的设置并不难理解,只是一些属性的参数不好记而已,多用几遍就好了。