帧动画:?android:duration="200" 设置每张图片出现的时间,单位为毫秒。
xml代码块:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@drawable/frame"
android:id="@+id/linear"
>
</RelativeLayout>
drawable/frame:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/one"
android:duration="200"
/>
<item
android:drawable="@drawable/two"
android:duration="200"
/>
<item
android:drawable="@drawable/three"
android:duration="200"
/>
<item
android:drawable="@drawable/fore"
android:duration="200"
/>
</animation-list>
java代码块:
public class MainActivity extends AppCompatActivity {
private boolean flag = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout relativeLayout = findViewById(R.id.linear);
AnimationDrawable anim = (AnimationDrawable)relativeLayout.getBackground();
relativeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (flag){
anim.start();
flag =false;
}else{
anim.stop();
flag=true;
}
}
});
}
}
二? 补间动画
首先在res文件夹下面创建一个anim文件夹,在anim文件夹中创建你所需要的AnimationResourceFile中创建需要的相关文件如:alpha? ? rotate? ?scale? translate的相关文件
在activity中用java代码进行设置:
ImageView imageView = findViewById(R.id.imageView);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//通过加载xml动画设置文件来创建一个animationduixiang
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate);
imageView.startAnimation(animation);
}
});
?
1.alpha? ?透明度? ?
android:fromAlpha="0"
android:toAlpha="1"
android:duration="2000"周期时间是多少
2:rotate? ?旋转
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%" 旋转中心
android:duration="2000"
3:scale? ?缩放
android:fromXScale="1"
android:fromYScale="1"
android:toXScale="0.5"
android:toYScale="0.5"
android:pivotY="50%"
android:pivotX="50%"
android:duration="3000"
4:tr anslate? ?平移
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="400"
android:toYDelta="400"
android:duration="3000"
三:属性动画
1.valueAnimator属性
setDuration(2000);
2.ObjectAnimator属性
java代码块:
TextView textView = findViewById(R.id.view1);
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(textView, "alpha", 0f, 1f);
objectAnimator.setDuration(4000);
objectAnimator.start();
3.监听器
bjectAnimator.addListener(new AnimatorListenerAdapter()
public void onAnimationStart(Animator animator) {
动画开始时候调用
}
@Override
public void onAnimationEnd(Animator animator) {
动画结束时候调用
}
@Override
public void onAnimationCancel(Animator animator) {
动画被取消的时候调用
}
@Override
public void onAnimationRepeat(Animator animator) {
动画重复执行的时候调用
}
|