1.用代码实现动画
透明度动画、旋转动画、尺寸伸缩动画、移动动画
public class MainActivity extends Activity {
private ImageView image;
private Button but1;
private Button but2;
Bitmap bm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
but1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
image.setImageBitmap(rotateToFit(bm, -90f));
bm = rotateToFit(bm, -90f);
}
});
but2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
image.setImageBitmap(rotateToFit(bm, 90f));
bm = rotateToFit(bm, 90f);
}
});
image.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public void onClick(View v) {
System.out.println("你点击了图片");
initAnimation();
}
});
}
@SuppressLint("NewApi")
private void init() {
image = (ImageView) findViewById(R.id.image);
but1 = (Button) findViewById(R.id.but1);
but2 = (Button) findViewById(R.id.but2);
but1.setText("向左转");
but2.setText("向右转");
bm = BitmapFactory.decodeResource(getResources(), R.drawable.eee);
image.setImageBitmap(bm);
image.setAlpha(alpha);
}
public static Bitmap rotateToFit(Bitmap bm, float degrees) {
int wight = bm.getWidth();
int height = bm.getHeight();
Matrix matrix = new Matrix();
matrix.postRotate(degrees);
Bitmap resBitmap = Bitmap.createBitmap(bm, 0, 0, wight, height, matrix,
true);
return resBitmap;
}
private Animation animation_alpha,animation_scale,animation_translate,animation_rotate;
private AnimationSet animationSet;
private void initAnimation() {
//透明度控制动画效果 alpha
animation_alpha=new AlphaAnimation(0.1f,1.0f);
//第一个参数fromAlpha为 动画开始时候透明度
//第二个参数toAlpha为 动画结束时候透明度
animation_alpha.setRepeatCount(0);//设置循环
animation_alpha.setDuration(5000);//设置时间持续时间为 5000毫秒
// 旋转效果rotate
animation_rotate = new RotateAnimation(0, -720,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
//第一个参数fromDegrees为动画起始时的旋转角度
//第二个参数toDegrees为动画旋转到的角度
//第三个参数pivotXType为动画在X轴相对于物件位置类型
//第四个参数pivotXValue为动画相对于物件的X座标的开始位置
//第五个参数pivotXType为动画在Y轴相对于物件位置类型
//第六个参数pivotYValue为动画相对于物件的Y座标的开始位置
animation_rotate.setRepeatCount(0);
animation_rotate.setDuration(5000);//设置时间持续时间为 5000毫秒
//尺寸伸缩动画效果 scale
animation_scale=new ScaleAnimation(0.1f,3.0f,0.1f,3.0f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
//第一个参数fromX为动画起始时 X座标上的伸缩尺寸
//第二个参数toX为动画结束时 X座标上的伸缩尺寸
//第三个参数fromY为动画起始时Y座标上的伸缩尺寸
//第四个参数toY为动画结束时Y座标上的伸缩尺寸
/*说明:
以上四种属性值
0.0表示收缩到没有
1.0表示正常无伸缩
值小于1.0表示收缩
值大于1.0表示放大
*/
//第五个参数pivotXType为动画在X轴相对于物件位置类型
//第六个参数pivotXValue为动画相对于物件的X座标的开始位置
//第七个参数pivotXType为动画在Y轴相对于物件位置类型
//第八个参数pivotYValue为动画相对于物件的Y座标的开始位置
animation_scale.setRepeatCount(0);
animation_scale.setDuration(5000);//设置时间持续时间为 5000毫秒
//移动动画效果translate
animation_translate=new TranslateAnimation(-20f,300f,-20f,300f);
//第一个参数fromXDelta为动画起始时 X座标上的移动位置
//第二个参数toXDelta为动画结束时 X座标上的移动位置
//第三个参数fromYDelta为动画起始时Y座标上的移动位置
//第三个参数toYDelta为动画结束时Y座标上的移动位置
animation_translate.setRepeatCount(0);//设置动画执行多少次,如果是-1的话就是一直重复
animation_translate.setDuration(5000);//设置时间持续时间为 5000毫秒
animationSet=new AnimationSet(true);
animationSet.addAnimation(animation_alpha);//透明度
animationSet.addAnimation(animation_rotate);//旋转
animationSet.addAnimation(animation_scale);//尺寸伸缩
animationSet.addAnimation(animation_translate);//移动
image.startAnimation(animationSet);//开始播放
}
}
- setFillAfter方法的作用是设置一个动画效果执行完毕后,View对象是否保留在终止的位置
- 若我们需要将真实组件最终移动到动画结束的位置,则需要在动画结束的回调函数内,调用组件的 clearAnimation接口,来清除动画过程中对某些属性的修改
- 在调用 clearAnimation接口后,利用layout接口可改变组件的真实位置
- 若调用layout之前不调用 clearAnimation接口: setFillAfter为flase时,会出现跳闪的问题, setFillAfter为true时,会出现组件偏离正确位置的问题
例子:
//上下箭头动画
RotateAnimation animation_rotate = new RotateAnimation(0, 180,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
animation_rotate.setFillAfter(true);
animation_rotate.setDuration(300);
animation_rotate.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
// imgPriceDeatil.clearAnimation();
// imgPriceDeatil.setBackgroundResource(R.mipmap.ic_estimate_expand);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
imgPriceDeatil.startAnimation(animation_rotate);
例子:
进入动画(集合)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false"
android:zAdjustment="top">
<scale
android:fromXScale="0.0"
android:toXScale="1.0"
android:fromYScale="0.0"
android:toYScale="1.0"
android:pivotX="8%"
android:pivotY="0%"
android:duration="300"
/>
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="300"
/>
</set>
出去的动画(集合)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false"
android:zAdjustment="top">
<scale
android:fromXScale="1.0"
android:toXScale="0.0"
android:fromYScale="1.0"
android:toYScale="0.0"
android:pivotX="8%"
android:pivotY="0%"
android:duration="300"
/>
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="300"
/>
</set>
代码绑定该xml动画
Animation outAnim = AnimationUtils.loadAnimation(mContext, R.anim.slide_out_top_left);
outAnim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
llGetCashTip.setVisibility(View.GONE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
llGetCashTip.startAnimation(outAnim);
XML资源文件的使用可以见:Animation Resources。
XML文件放在项目的res/anim/目录下。文件必须有一个唯一的根节点。
这个根节点可以是:<alpha>, <scale>, <translate>, <rotate>, interpolator element, 或者是<set>。
默认情况下,所有的动画都是并行进行的,要想使得它们顺寻发生,你必须指定startOffset属性。
有一些值,可以指定是相对于View本身还是相对于父类容器的。
比如pivotX,要表示相对于自身的50%,要用50%;要表示相对于父类容器的50%,则直接写50。
例子:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<scale
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXScale="1.0"
android:toXScale="1.4"
android:fromYScale="1.0"
android:toYScale="0.6"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="false"
android:duration="700" />
<set
android:interpolator="@android:anim/accelerate_interpolator"
android:startOffset="700">
<scale
android:fromXScale="1.4"
android:toXScale="0.0"
android:fromYScale="0.6"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="400" />
<rotate
android:fromDegrees="0"
android:toDegrees="-45"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="400" />
</set>
</set>