首页 > 其他分享 >LayoutAnimationController,补间动画,属性动画,值动画,自定义动画,帧动画

LayoutAnimationController,补间动画,属性动画,值动画,自定义动画,帧动画

时间:2023-03-10 13:32:48浏览次数:29  
标签:动画 layout 自定义 bt animation import 补间 android id


最好的代码永远是自己写出来的


布局


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.liuan.animation.MainActivity">

<ScrollView
android:fadeScrollbars="true"
android:layout_width="match_parent"
android:layout_height="250dp">

<LinearLayout
android:id="@+id/ll_amimation"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">

<Button
android:id="@+id/bt_alpha"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="7dp"
android:text="透明度动画Alpha" />

<Button
android:id="@+id/bt_Rotate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="7dp"
android:text="旋转动画Rotate" />

<Button
android:id="@+id/bt_Translate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="7dp"
android:text="位移动画Translate" />

<Button
android:id="@+id/bt_Scale"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="7dp"
android:text="缩放动画Scale" />
<Button
android:id="@+id/bt_Object"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="7dp"
android:text="属性动画Object" />
<Button
android:id="@+id/bt_Value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="7dp"
android:text="值动画" />

<Button
android:id="@+id/bt_Set"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="7dp"
android:text="补间/属性动画合集set" />
<Button
android:id="@+id/bt_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="7dp"
android:text="自定义动画TV" />
<Button
android:id="@+id/bt_3d"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="7dp"
android:text="自定义3d动画" />




</LinearLayout>

</ScrollView>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:layout_centerInParent="true"
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:src="@mipmap/ic_launcher" />

</RelativeLayout>

</LinearLayout>


mainActivity


package com.example.liuan.animation;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LayoutAnimationController;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

@Bind(R.id.bt_alpha)
Button btAlpha;
@Bind(R.id.bt_Rotate)
Button btRotate;
@Bind(R.id.bt_Translate)
Button btTranslate;
@Bind(R.id.bt_Scale)
Button btScale;
@Bind(R.id.ll_amimation)
LinearLayout llAmimation;
@Bind(R.id.imageview)
ImageView imageview;
@Bind(R.id.activity_main)
LinearLayout activityMain;
@Bind(R.id.bt_Object)
Button btObject;
@Bind(R.id.bt_Value)
Button btValue;
@Bind(R.id.bt_Set)
Button btSet;
@Bind(R.id.bt_tv)
Button btTv;
@Bind(R.id.bt_3d)
Button bt3d;
private int animationDuration = 2000;
private boolean isViewSet=true;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
imageview.setImageResource(R.mipmap.ic_launcher);
//为布局设置动画时间
ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1, 0, 1);
scaleAnimation.setDuration(animationDuration);
//设置布局动画的显示属性
LayoutAnimationController lac = new LayoutAnimationController(scaleAnimation, 0.5f);
//设置View显示的顺序
//匀速ORDER_NORMAL
//随机ORDER_RANDOM
//反顺序
lac.setOrder(LayoutAnimationController.ORDER_REVERSE);
//为ViewGroup设置布局动画
activityMain.setLayoutAnimation(lac);



}

@OnClick({R.id.bt_alpha, R.id.bt_Rotate, R.id.bt_Translate, R.id.bt_Scale, R.id.bt_Object, R.id.bt_Value, R.id.bt_Set, R.id.bt_tv,R.id.bt_3d})
public void onClick(View view) {
switch (view.getId()) {

case R.id.bt_alpha:
// Toast.makeText(MainActivity.this, "getWidth"+imageview.getWidth()+"getHeight"+imageview.getHeight(), Toast.LENGTH_SHORT).show();
AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
alphaAnimation.setDuration(animationDuration);
imageview.startAnimation(alphaAnimation);
break;
case R.id.bt_Rotate:
RotateAnimation ra = new RotateAnimation(0, 360, imageview.getX() / 2, imageview.getY() / 2);
ra.setDuration(animationDuration);
imageview.startAnimation(ra);
break;
case R.id.bt_Translate:
TranslateAnimation translateAnimation = new TranslateAnimation(0, 0, imageview.getX(), imageview.getY());
translateAnimation.setDuration(animationDuration);
imageview.startAnimation(translateAnimation);
break;
case R.id.bt_Scale:
ScaleAnimation scaleAnimation = new ScaleAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 360, Animation.RELATIVE_TO_SELF, 0.5F, Animation.RELATIVE_TO_SELF, 0.5F);
scaleAnimation.setDuration(animationDuration);
imageview.startAnimation(scaleAnimation);
break;
case R.id.bt_Object:

//属性动画不会改变实际位置
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageview,
"translationX", 300);
objectAnimator.setDuration(animationDuration);
objectAnimator.start();
objectAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {

}

@Override
public void onAnimationEnd(Animator animation) {
ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(imageview,
"translationX", 0);
objectAnimator2.setDuration(animationDuration);
objectAnimator2.start();
}

@Override
public void onAnimationCancel(Animator animation) {

}

@Override
public void onAnimationRepeat(Animator animation) {

}
});


break;
case R.id.bt_Value:
ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 10);
valueAnimator.setTarget(imageview);
valueAnimator.setDuration(10 * 1000);
valueAnimator.setInterpolator(new LinearInterpolator());

valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

private AlertDialog dialog;
private AlertDialog.Builder builder;

@Override
public void onAnimationUpdate(ValueAnimator animation) {
int animatedValue = (int) animation.getAnimatedValue();
if (builder == null) {
builder = new AlertDialog.Builder(MainActivity.this);
}
if(dialog==null) {
dialog = builder.create();
}
dialog.setTitle("值动画");
dialog.setMessage("" + animatedValue);
if (animatedValue == 10) {
dialog.dismiss();
return;
}
dialog.show();


}
});
valueAnimator.start();
break;
case R.id.bt_Set:

if(isViewSet){
Toast.makeText(this, "这是补间动画的合集", Toast.LENGTH_SHORT).show();
AnimationSet set=new AnimationSet(true);
set.setDuration(animationDuration);
AlphaAnimation alphaAnimation1 = new AlphaAnimation(0, 1);
alphaAnimation1.setDuration(animationDuration);
set.addAnimation(alphaAnimation1);

TranslateAnimation ta = new TranslateAnimation(0, 100, 0, 200);
ta.setDuration(animationDuration);
set.addAnimation(ta);

imageview.startAnimation(set);
}else{
Toast.makeText(this, "这是属性动画的合集", Toast.LENGTH_SHORT).show();
AnimatorSet set=new AnimatorSet();
set.setDuration(animationDuration);

ObjectAnimator oa1=ObjectAnimator.ofFloat(imageview,"translationY",0,300,0);
ObjectAnimator oa2=ObjectAnimator.ofFloat(imageview,"scaleX",1f,0f,1f);
ObjectAnimator oa3=ObjectAnimator.ofFloat(imageview,"scaleY",1f,0f,1f);
set.playTogether(oa1,oa2,oa3);
set.start();


}
isViewSet=!isViewSet;

break;
case R.id.bt_tv:
TvAnimator tvAnimator = new TvAnimator();
tvAnimator.setDuration(animationDuration);
imageview.startAnimation(tvAnimator);
break;
case R.id.bt_3d:
ThereDAnimator tda=new ThereDAnimator();
tda.setDuration(animationDuration);
imageview.startAnimation(tda);
break;

}
}


}




ThereDAnimator


package com.example.liuan.animation;

import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.BounceInterpolator;
import android.view.animation.Transformation;

/**
* Name: ThereDAnimator
* Author: liuan
* creatTime:2017-01-05 20:04
*/

public class ThereDAnimator extends Animation {
private int mCenterwidth;
private int mCenterHeight;
private Camera mCamera=new Camera();

@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
//设置默认时长
setDuration(2000);
//结束后保留动作
setFillAfter(true);
//设置默认插值器
setInterpolator(new BounceInterpolator());
mCenterwidth=width/2;
mCenterHeight=height/2;
}
//动画的核心
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
Matrix matrix = t.getMatrix();
mCamera.save();;
//使用camera设置旋转的角度
float mRotateY=360;
mCamera.rotateY(mRotateY*interpolatedTime);
//将旋转变换作用到matrix上
mCamera.getMatrix(matrix);
mCamera.restore();
//通过pre方法设置矩阵作用前的偏移量来改变旋转中心
matrix.preTranslate(mCenterwidth,mCenterHeight);
matrix.postTranslate(-mCenterwidth,-mCenterHeight);

}
}



TvAnimator


package com.example.liuan.animation;

import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.Transformation;

/**
* Name: TvAnimator
* Author: liuan
* creatTime:2017-01-04 22:30
*/

public class TvAnimator extends Animation {
MainActivity s=new MainActivity();


@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
Matrix matrix = t.getMatrix();
matrix.preScale(1,1-interpolatedTime,36,36);

}
}


butterknife的使用 请查看我下一个的博客 


场面一度十分尴尬


今天才发现少了个帧动画 补上  首先要 在drawable 下放好图片 然后 建布局文件


如下


animation_nor.xml


<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="@drawable/wife1" android:duration="150"/>
<item android:drawable="@drawable/wife2" android:duration="150"/>
<item android:drawable="@drawable/wife3" android:duration="150"/>
<item android:drawable="@drawable/wife4" android:duration="150"/>
<item android:drawable="@drawable/wife5" android:duration="150"/>
<item android:drawable="@drawable/wife6" android:duration="150"/>

</animation-list>




animation_res.xml


<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="@drawable/wife6" android:duration="150"/>
<item android:drawable="@drawable/wife5" android:duration="150"/>
<item android:drawable="@drawable/wife4" android:duration="150"/>
<item android:drawable="@drawable/wife3" android:duration="150"/>
<item android:drawable="@drawable/wife2" android:duration="150"/>
<item android:drawable="@drawable/wife1" android:duration="150"/>

</animation-list>

然后添加布局Button什么的我就步多说啦 直接上核心代码


case R.id.bt_animationlist_nor:
imageview.setImageResource(R.drawable.animation_nor);
animationDrawable= (AnimationDrawable) imageview.getDrawable();
animationDrawable.start();

break;
case R.id.bt_animationlist_res:
imageview.setImageResource(R.drawable.animation_res);
animationDrawable= (AnimationDrawable) imageview.getDrawable();
animationDrawable.start();

break;





标签:动画,layout,自定义,bt,animation,import,补间,android,id
From: https://blog.51cto.com/u_14523369/6112929

相关文章