跳至主要内容

Android Animation

button.getPivotX()*2  == button's  width

button.getX()  ==  button's  absolute x



   float   sx1 = button.getPivotX(),    sy1=button.getScaleY(),   sy2=textView.getScaleY();

    TranslateAnimation translateAnimation=new TranslateAnimation (
        Animation.ABSOLUTE,      0f,        Animation.ABSOLUTE,      sx1,        
        Animation.ABSOLUTE,      0f,         Animation.ABSOLUTE,     sy1+sy2 );

     translateAnimation.setFillAfter(true);  
     translateAnimation.setDuration(1000);

textView.startAnimation(translateAnimation);


animation. setRepeatCount ( 10 );
animation. setRepeatMod  ( Animation. REVERSE );





四、具体实现
2.java文件

Animation Set :


           AnimationSet animationSet = new AnimationSet(true);

          
 animationSet.addAnimation(alphaAnimation);











AlphaAnimation :

           AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);

ScaleAnimation :

           ScaleAnimation scaleAnimation = new ScaleAnimation(
                  0, 0.1f,0,0.1f,
                  Animation.RELATIVE_TO_SELF,0.5f,
                  Animation.RELATIVE_TO_SELF,0.5f);

TranslateAnimation :

           TranslateAnimation translateAnimation =
              new TranslateAnimation(
                  Animation.RELATIVE_TO_SELF,0f,
                  Animation.RELATIVE_TO_SELF,0.5f,
                  Animation.RELATIVE_TO_SELF,0f,
                  Animation.RELATIVE_TO_SELF,0.5f);

RotationAnimation :


Tween Animations的通用方法

  1、setDuration(long durationMills)
  设置动画持续时间(单位:毫秒)
  2、setFillAfter(Boolean fillAfter)
  如果fillAfter的值为true,则动画执行后,控件将停留在执行结束的状态
  3、setFillBefore(Boolean fillBefore)
  如果fillBefore的值为true,则动画执行后,控件将回到动画执行之前的状态
  4、setStartOffSet(long startOffSet)
  设置动画执行之前的等待时间
  5、setRepeatCount(int repeatCount)
  设置动画重复执行的次数

在代码中使用Animations可以很方便的调试、运行,但是代码的可重用性差,重复代码多。同样可以在xml文件中配置Animations,这样做可维护性变高了,只不过不容易进行调试。
一、在xml中使用Animations步骤
       1.res文件夹下建立一个anim文件夹;
       2.创建xml文件,并首先加入set标签,更改标签如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
</set>
3.在该标签当中加入rotatealphascale或者translate标签;
<alpha
        android:fromAlpha="1.0"
        android:toAlpha="0.0"
        android:startOffset="500"
        android:duration="500"/>
4.在代码当中使用AnimationUtils当中装载xml文件,并生成Animation对象。因为AnimationAnimationSet的子类,所以向上转型,用Animation对象接收。
Animation animation = AnimationUtils.loadAnimation(
                  Animation1Activity.this, R.anim.alpha);
           // 启动动画
           image.startAnimation(animation);
二、具体实现
 1、  alpha.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
    <!-- fromAlphatoAlpha是起始透明度和结束时透明度 -->
    <alpha
        android:fromAlpha="1.0"
        android:toAlpha="0.0"
        android:startOffset="500"
        android:duration="500"/>
</set>
2、  rotate.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
    <!--
        fromDegrees:开始的角度
        toDegrees:结束的角度,+表示是正的
        pivotX:用于设置旋转时的x轴坐标
        
           1)当值为"50",表示使用绝对位置定位
           2)当值为"50%",表示使用相对于控件本身定位
           3)当值为"50%p",表示使用相对于控件的父控件定位
        pivotY:用于设置旋转时的y轴坐标
      -->
    <rotate
        android:fromDegrees="0"
        android:toDegrees="+360"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="1000"/>
</set>
3、  scale.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
   <!--
       起始x轴坐标
           x轴坐标
           y轴坐标
           y轴坐标
           轴的坐标
           轴的坐标
     -->
   <scale
       android:fromXScale="1.0"
       android:toXScale="0.0"
       android:fromYScale="1.0"
       android:toYScale="0.0"
       android:pivotX="50%"
       android:pivotY="50%"
       android:duration="1000"/>
</set>

4、  translate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
    <!--
           x轴坐标
           x轴坐标
           y轴坐标
           y轴坐标
      -->
    <translate
        android:fromXDelta="0%"
        android:toXDelta="100%"
        android:fromYDelta="0%"
        android:toYDelta="100%"
        android:duration="2000"/>
</set>

5、  .java文件
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.view.View.OnClickListener;
import android.view.animation.Animation;
importandroid.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

public class Animation1Activity extends Activity {
    private Button rotateButton = null;
    private Button scaleButton = null;
    private Button alphaButton = null;
    private Button translateButton = null;
    private ImageView image = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);

       rotateButton = (Button) findViewById(R.id.rotateButton);
       scaleButton = (Button) findViewById(R.id.scaleButton);
       alphaButton = (Button) findViewById(R.id.alphaButton);
       translateButton = (Button) findViewById(R.id.translateButton);
       image = (ImageView) findViewById(R.id.image);

       rotateButton.setOnClickListener(newRotateButtonListener());
       scaleButton.setOnClickListener(newScaleButtonListener());
       alphaButton.setOnClickListener(newAlphaButtonListener());
       translateButton.setOnClickListener(newTranslateButtonListener());
    }

    class AlphaButtonListener implementsOnClickListener {
       public void onClick(View v) {
           // 使用AnimationUtils装载动画配置文件
           Animation animation = AnimationUtils.loadAnimation(
                  Animation1Activity.this, R.anim.alpha);
           // 启动动画
           image.startAnimation(animation);
       }
    }

    class RotateButtonListener implementsOnClickListener {
       public void onClick(View v) {
           Animation animation = AnimationUtils.loadAnimation(
                  Animation1Activity.this, R.anim.rotate);
           image.startAnimation(animation);
       }
    }

    class ScaleButtonListener implementsOnClickListener {
       public void onClick(View v) {
           Animation animation = AnimationUtils.loadAnimation(
                  Animation1Activity.this, R.anim.scale);
           image.startAnimation(animation);
       }
    }

    class TranslateButtonListener implementsOnClickListener {
       public void onClick(View v) {
           Animation animation = AnimationUtils.loadAnimation(Animation1Activity.this, R.anim.translate);
           image.startAnimation(animation);
       }
    }
}

评论

此博客中的热门博文

onsen ui example splitter side menu swipe

<!DOCTYPE html> <html> <head> <title>TheyTube - Watch free video online</title> <link rel="stylesheet" href="https://unpkg.com/onsenui/css/onsenui.css"> <link rel="stylesheet" href="https://unpkg.com/onsenui/css/onsen-css-components.min.css">   <script src="https://unpkg.com/onsenui/js/onsenui.min.js"></script>   <script type="text/javascript">   ons.platform.select('android')   </script> </head> <body> <ons-splitter>   <ons-splitter-side id="menu" side="left" width="220px" collapse swipeable>     <ons-page>       <ons-list>         <ons-list-item onclick="fn.load('home.html')" tappable>           Home         </ons-list-item>         <ons-list-item onclick="fn.load('settings.html')" tappable>           Setti

go golang get disk usage free space remain info

package main import ( "fmt" "syscall" ) func main() { fmt.Println(DiskUsage("./")) } func DiskUsage(path string) uint64 { fs := syscall.Statfs_t{} err := syscall.Statfs(path, &fs) if err != nil { return 0 } return fs.Bfree * uint64(fs.Bsize) } //All space   = fs.Blocks * uint64(fs.Bsize) //Free space = fs.Bfree * uint64(fs.Bsize) //Used space= fs.All - disk.Free