zoukankan      html  css  js  c++  java
  • (四十七)属性动画Demo

    1、程序结构

    2、MainActivity.java程序

    package com.example.objectanimator1;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import android.animation.ObjectAnimator;
    import android.animation.TimeInterpolator;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.animation.AccelerateInterpolator;
    import android.view.animation.BounceInterpolator;
    import android.widget.ImageView;
    import android.widget.Toast;
    
    public class MainActivity extends Activity implements OnClickListener {
    
        private int[] ivId = { R.id.iv_a, R.id.iv_b, R.id.iv_c, R.id.iv_d,
                R.id.iv_e, R.id.iv_f, R.id.iv_g, R.id.iv_h };
    
        private List<ImageView> imageInfos = new ArrayList<ImageView>();
    
        private Boolean flag = true;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            for (int i = 0; i < ivId.length; i++) {
                ImageView iv = (ImageView) findViewById(ivId[i]);
                imageInfos.add(iv);
                iv.setOnClickListener(this);
            }
        }
    
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
            case R.id.iv_a:
                if (flag) {
                    startAnima();
                    flag = false;
                } else {
                    closeAnima();
                    flag = true;
                }
    
                break;
    
            default:
                Toast.makeText(MainActivity.this, "clicked:"+ v.getId() , 0).show();
                break;
            }
        }
    
        private void startAnima() {
            // TODO Auto-generated method stub
            for (int i = 0; i < ivId.length; i++) {
                ObjectAnimator oa = ObjectAnimator.ofFloat(imageInfos.get(i),
                        "translationY", 0, i * 100);
                oa.setInterpolator(new BounceInterpolator());
                oa.setDuration(1000);
                oa.setStartDelay(300);
                oa.start();
            }
        }
    
        private void closeAnima() {
            // TODO Auto-generated method stub
            for (int i = 0; i < ivId.length; i++) {
                ObjectAnimator oa = ObjectAnimator.ofFloat(imageInfos.get(i),
                        "translationY", i * 100, 0);
                oa.setInterpolator(new AccelerateInterpolator());
                oa.setDuration(1000);
                oa.setStartDelay(300);
                oa.start();
            }
        }
    }

    2、activity_main.xml的代码

    <FrameLayout 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:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.objectanimator1.MainActivity" >
    
        <ImageView
            android:id="@+id/iv_a"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:src="@drawable/a" />
    
        <ImageView
            android:id="@+id/iv_b"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/b" />
    
        <ImageView
            android:id="@+id/iv_c"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/c" />
    
        <ImageView
            android:id="@+id/iv_d"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/d" />
    
        <ImageView
            android:id="@+id/iv_e"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/e" />
    
        <ImageView
            android:id="@+id/iv_f"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/f" />
    
        <ImageView
            android:id="@+id/iv_g"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/g" />
    
        <ImageView
            android:id="@+id/iv_h"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/h" />
    
    </FrameLayout>
  • 相关阅读:
    poj 1201 Intervals 差分约束系统
    poj 3237 Tree 树链剖分+线段树
    hdu 2256 Problem of Precision 构造整数 + 矩阵快速幂
    hdu 5451 Best Solver 矩阵循环群+矩阵快速幂
    hdu 5769 Substring 后缀数组 + KMP
    hdu 5755 2016 Multi-University Training Contest 3 Gambler Bo 高斯消元模3同余方程
    hdu 5690 2016"百度之星"
    hdu 5738 2016 Multi-University Training Contest 2 Eureka 计数问题(组合数学+STL)
    hdu 5719 BestCoder 2nd Anniversary B Arrange 简单计数问题
    hdu 5720 BestCoder 2nd Anniversary Wool 推理+一维区间的并
  • 原文地址:https://www.cnblogs.com/fuyanan/p/4312543.html
Copyright © 2011-2022 走看看