1.camera由远到近特效(缩放动画)
首先新建一个布局文件camera_stretch_activity
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/img" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitXY" android:src="@drawable/ic_launcher_background"/> </LinearLayout>
具体代码
ImageView imageView = findViewById(R.id.img); ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f,1.2f,1.0f,1.2f, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); scaleAnimation.setFillAfter(true); scaleAnimation.setInterpolator(new BounceInterpolator()); scaleAnimation.setDuration(6000); imageView.startAnimation(scaleAnimation);
这里的插值器选择的是回弹插值器
2.旋转动画效果 数据加载进度
布局文件loading:
<?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"> <ImageView android:id="@+id/loading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/loading" android:layout_gravity="center"/> </LinearLayout>
具体代码:
ImageView imageView = findViewById(R.id.loading); RotateAnimation rotateAnm = new RotateAnimation(0,360, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); rotateAnm.setRepeatCount(Animation.INFINITE); rotateAnm.setDuration(2000); rotateAnm.setInterpolator(new LinearInterpolator()); imageView.startAnimation(rotateAnm);
3 位移动画
布局文件:
<LinearLayout 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:orientation="vertical" tools:context=".ValueAnimActivity"> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:text="start Anim"/> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:background="#ffff00" android:text="Hello qijian"/> </LinearLayout>
具体代码:
final TextView tv = findViewById(R.id.tv); Button btn = findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { TranslateAnimation animation = new TranslateAnimation(Animation.ABSOLUTE,0, Animation.ABSOLUTE,400,Animation.ABSOLUTE,0,Animation.ABSOLUTE,100); animation.setFillAfter(true); animation.setDuration(1000); tv.startAnimation(animation); } });
4.ValueAnimator属性动画实现位移动画效果
结合上面的布局文件使用ValueAnimator实现位移动画效果
private void doAnimation() { ValueAnimator animator = ValueAnimator.ofInt(0, 400); animator.setDuration(1000); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { int curValue = (int) animation.getAnimatedValue(); tv.layout(curValue, curValue, curValue + tv.getWidth(), curValue + tv.getHeight()); } }); animator.start();
网友评论