简单监听事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.example.animation;
 
import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.Toast;
 
public class MainActivity extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
     
    }
     
    public void click(View view){
        Toast.makeText(this, "click", Toast.LENGTH_SHORT).show();
    }
     
    public void move(View view ){
        ImageView imageView=(ImageView)findViewById(R.id.imageView);
        ObjectAnimator animator=new ObjectAnimator().ofFloat(imageView, "translationX", 0f,200f);
        animator.setDuration(1000);
        /**
         *添加所有监听事件
         */
//        animator.addListener(new AnimatorListener() {
//         
//          @Override
//          public void onAnimationStart(Animator arg0) {
//          }
//         
//          @Override
//          public void onAnimationRepeat(Animator arg0) {
//          }
//         
//          @Override
//          public void onAnimationEnd(Animator arg0) {
//              Toast.makeText(MainActivity.this, "animator is end!",Toast.LENGTH_SHORT).show();
//          }
//         
//          @Override
//          public void onAnimationCancel(Animator arg0) {
//          }
//      });
         
        /**
         * 按需添加
         */
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                // TODO Auto-generated method stub
                super.onAnimationEnd(animation);
                Toast.makeText(MainActivity.this, "animator is end!",Toast.LENGTH_SHORT).show();
 
            }
        });
        animator.start();
    }
      
}

看一个简单的例子

这是xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<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.animator.MainActivity" >
 
    <imageview android:id="@+id/imageView2" android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/image_b">
 
    <imageview android:id="@+id/imageView3" android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/image_c">
 
    <imageview android:id="@+id/imageView4" android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/image_d">
 
    <imageview android:id="@+id/imageView5" android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/image_e">
 
    <imageview android:id="@+id/imageView6" android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/image_f">
 
    <imageview android:id="@+id/imageView7" android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/image_g">
 
    <imageview android:id="@+id/imageView8" android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/image_h">
     
    <imageview android:id="@+id/imageView1" android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/image_a">
     
 
</frameLayout></imageview></imageview></imageview></imageview></imageview></imageview></imageview></imageview>

这是主程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.example.animator;
 
import java.util.ArrayList;
import java.util.List;
 
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.BounceInterpolator;
import android.widget.ImageView;
import android.widget.Toast;
 
public class MainActivity extends Activity implements OnClickListener {
 
    private int[] res = { R.id.imageView1, R.id.imageView2, R.id.imageView3,
            R.id.imageView4, R.id.imageView5, R.id.imageView6, R.id.imageView7,
            R.id.imageView8 };
    private List<imageview> imageViewList = new ArrayList<imageview>();
    private boolean isOpen = false;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        for (int i = 0; i < res.length; i++) {
            ImageView imageView = (ImageView) findViewById(res[i]);
            imageView.setOnClickListener(this);
            imageViewList.add(imageView);
        }
    }
 
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.imageView1:
            if (!isOpen) {
                startAnim();
            } else {
                closeAnim();
            }
            break;
 
        default:
            Toast.makeText(this, "you click item!", Toast.LENGTH_SHORT).show();
            break;
        }
    }
 
    private void startAnim() {
        for (int i = 1; i < res.length; i++) {
            ObjectAnimator animator = ObjectAnimator.ofFloat(
                    imageViewList.get(i), "translationY", 0f, i * 150);
            animator.setDuration(500);
            // 动画差值器, Interpolator
            // 被用来修饰动画效果,定义动画的变化率,可以使存在的动画效果accelerated(加速),decelerated(减速),repeated(重复),bounced(弹跳)等。
            animator.setInterpolator(new BounceInterpolator());
            animator.setStartDelay(i * 350);
            animator.start();
            isOpen = true;
        }
    }
 
    private void closeAnim() {
        for (int i = 1; i < res.length; i++) {
            ObjectAnimator animator = ObjectAnimator.ofFloat(
                    imageViewList.get(i), "translationY", i * 150, 0f);
            animator.setDuration(500);
            animator.setInterpolator(new BounceInterpolator());
            animator.setStartDelay(i * 350);
            animator.start();
            isOpen = false;
        }
    }
 
}
</imageview></imageview>
 

结伴旅游,一个免费的交友网站:www.jieberu.com

推推族,免费得门票,游景区:www.tuituizu.com

Android 属性动画监听事件与一个菜单的例子的更多相关文章

  1. android listview 的监听事件

    今天遇到了一个比较让我头疼的问题,不过追根揭底只是我对listview理解的不够透彻罢了, 闲言少叙,说说我遇到的问题吧: 上篇随笔我写了关于listview的使用,如果你也已经写好了列表那么恭喜这一 ...

  2. Android CheckBox的监听事件

    1.在xml文件中定义CheckBox,一定要定义id <CheckBox android:id="@+id/beijing" android:layout_width=&q ...

  3. Android 软键盘监听事件

    Android软键盘的隐藏显示研究 Android是一个针对触摸屏专门设计的操作系统,当点击编辑框,系统自动为用户弹出软键盘,以便用户进行输入.     那么,弹出软键盘后必然会造成原有布局高度的减少 ...

  4. android的单击监听事件

    Button button = (Button) findViewById(R.id.button1); //1.直接new出来 button.setOnClickListener(new View. ...

  5. Android ListView的监听事件

    Android开发时,最常用的控件之一就是ListView了,而使用ListView的同时,必然需要对它设置监听器,常用的监听器有这么几个1.OnItemClickListener 2.OnTouch ...

  6. Android 监听事件

    安卓中监听事件的三种实现方式 1.匿名内部类的实现方式 2.独立类的实现方式 3.实现接口方式实现 一.匿名内部类的实现 1.首先声明一个Button //声明一个Button private But ...

  7. android 属性动画

    一直再追郭霖的博客和imooc上的一些新的视频,最近有讲到属性动画. 以下内容为博客学习以及imooc上视频资料的学习笔记: 在3.0之前比较常见的动画为tween动画和frame动画: tween动 ...

  8. vue2之对象属性的监听

    对象属性监听的两种方法: 1.普通的watch data() { return { frontPoints: 0 } }, watch: { frontPoints(newValue, oldValu ...

  9. (原)android中的动画(三)之动画监听&页面切换动画

    1.动画也可以设置监听事件,例如在动画结束时需要执行某操作 把要执行的代码写在onAnimationEnd()回调方法中即可: anim.setAnimationListener(new Animat ...

随机推荐

  1. C++多线程基础学习笔记(五)

    一.互斥量 1.1 互斥量的基本概念 简单来说,一个锁就是一个互斥量,既然是锁,就有两种状态:加锁和解锁,通过加锁>>>操作共享数据>>>解锁的方式,实现保护共享数 ...

  2. C++学习 之 指针及动态内存分配(笔记)

    1. 指针 1.1 指针的含义: 简单来说,指针是存储内存地址的变量.当我们声明了一个指针变量后,使用该指针时系统会根据指针内的地址索引内存块,读取内存内的值.指针因为是用来存地址的,所以一般固定长度 ...

  3. spark教程(四)-SparkContext 和 RDD 算子

    SparkContext SparkContext 是在 spark 库中定义的一个类,作为 spark 库的入口点: 它表示连接到 spark,在进行 spark 操作之前必须先创建一个 Spark ...

  4. 计算机网络--TCP三次握手和四次挥手

    TCP(传输控制协议) TCP(Transmission Control Protocol 传输控制协议)是一种面向连接的.可靠的.基于字节流的传输层通信协议.通过三次握手建立连接,通讯完成时要拆除连 ...

  5. 最小生成树之Prim Kruskal算法(转)

    最小生成树 首先,生成树是建立在无向图中的,对于有向图,则没有生成树的概念,所以接下来讨论的图均默认为无向图.对于一个有n个点的图,最少需要n-1条边使得这n个点联通,由这n-1条边组成的子图则称为原 ...

  6. C#中的编译为什么不叫Compile而叫build

    是因为Build包含了compile,build既compile了你自己的代码,又把你compile的Assembly和引用别人的Assembly放在一起buiid.

  7. CPU如何区分溢出和自然进位?

    CPU如何区分溢出和自然进位? 之前学习补码的时候倒是学会了基本概念,但是最近又接触时发现还有不清楚的地方,所以又研究了下 今天的核心问题的"CPU是如何区分高位自然舍弃和溢出的?" ...

  8. Python基础教程之dict和set

    1. dict Python中的dict等于js中的 map ,使用键-值(key-value)存储,具有极快的查找速度. 如果 我们要根据同学的姓名去查找他的成绩在不用dict的情况下.就需要两个l ...

  9. leetcode 对角线遍历 JavaScript

    JavaScript /** * @param {number[][]} matrix * @return {number[]} */ var findDiagonalOrder = function ...

  10. Js 将图片的绝对路径转换为base64编码

    转.... 我们可以使用canvas.toDataURL的方法将图片的绝对路径转换为base64编码:在这我们引用的是淘宝首页一张图片如下:  var img = "https://img. ...