动画效果监听事件
来源:互联网 发布:淘宝旗舰店是正品吗 编辑:程序博客网 时间:2024/06/05 10:29
1.Activity类的实现:
package com.wang.activity;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.Animation;import android.view.animation.Animation.AnimationListener;import android.view.animation.AnimationSet;import android.view.animation.AnimationUtils;import android.widget.Button;import android.widget.ImageView;import com.wang.utils.ReflectUtil;public class MainActivity extends Activity implements OnClickListener{private final static int SIZE = 11;private ImageView[] imageViews;private Animation[] animations;private Button mButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 鍒濆鍖栨帶浠�initRes();}private void initRes() {mButton = (Button) findViewById(R.id.button1);mButton.setOnClickListener(this);int index = 0;imageViews = new ImageView[SIZE];for (int i = 0; i < SIZE; i++) {index = i + 1;int id = ReflectUtil.getResourceId("id", "white_rect" + index);ImageView imageView = (ImageView) findViewById(id);imageView.setOnClickListener(this);imageViews[i] = imageView;}animations = new Animation[SIZE];for (int i = 0; i < SIZE; i++) {index = i + 1;int id = ReflectUtil.getResourceId("anim", "my_anim" + index);Animation animation = AnimationUtils.loadAnimation(this, id);animation.setFillAfter(true);animation.setFillBefore(true);animation.setFillEnabled(true);animation.setAnimationListener(new MyAnimListener());animations[i] = animation;}}class MyAnimListener implements AnimationListener{@Overridepublic void onAnimationStart(Animation animation) {// TODO Auto-generated method stub}@Overridepublic void onAnimationEnd(Animation animation) {// TODO Auto-generated method stub}@Overridepublic void onAnimationRepeat(Animation animation) {// TODO Auto-generated method stub}}Handler mHandler = new Handler(){@Overridepublic void handleMessage(Message msg) {if (msg.what == 1) {for (int i = 0; i < SIZE; i++) {imageViews[i].startAnimation(animations[i]);imageViews[i].setColorFilter(null);}}if (msg.what == 2) {int index = msg.arg1;for (int i = 0; i < index; i++) {imageViews[i].setColorFilter(Color.BLUE);imageViews[i].setColorFilter(null);}}};};public class MyHandler implements Runnable{private Handler mHandler;private String type;private int index;public MyHandler(String type,Handler mHandler,int index) {super();this.type = type;this.index = index;this.mHandler = mHandler;}@Overridepublic void run() {Message msg = new Message();if (type.equals("Button")) {msg.what= 1;mHandler.sendMessage(msg);}if (type.equals("ImageView")) {msg.what= 2;msg.arg1 = index;mHandler.sendMessage(msg);}}}@Overridepublic void onClick(View v) {if (v instanceof Button) {Thread thread = new Thread(new MyHandler("Button", mHandler, 0));thread.start();}ImageView imageView = null;if (v instanceof ImageView) {int id = v.getId();int index = 0;for (int i = 0; i < SIZE; i++) {index = i + 1;int idTmp = ReflectUtil.getResourceId("id", "white_rect" + index);if (id == idTmp) {imageView = imageViews[i];break;}}if (imageView != null) {Thread thread = new Thread(new MyHandler("ImageView", mHandler, index));thread.start();}}}}
my_anim1.xml
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" > <rotate android:duration="0" android:fromDegrees="0" android:interpolator="@android:anim/linear_interpolator" android:pivotX="50%" android:pivotY="500%" android:toDegrees="-50" /></set>
my_anim2.xml
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" > <rotate android:duration="0" android:fromDegrees="0" android:interpolator="@android:anim/linear_interpolator" android:pivotX="50%" android:pivotY="500%" android:toDegrees="-40" /></set>
3.布局文件activity_main.xml
<RelativeLayout 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:background="@drawable/background" tools:context="${relativePackage}.${activityClass}" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <ImageView android:id="@+id/white_rect1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_marginLeft="119dp" android:layout_marginTop="129dp" android:layout_toRightOf="@+id/textView1" android:src="@drawable/white_rect_y" /> <ImageView android:id="@+id/white_rect2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_marginLeft="119dp" android:layout_marginTop="129dp" android:layout_toRightOf="@+id/textView1" android:src="@drawable/white_rect_y" /> <ImageView android:id="@+id/white_rect3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_marginLeft="119dp" android:layout_marginTop="129dp" android:layout_toRightOf="@+id/textView1" android:src="@drawable/white_rect_y" /> <ImageView android:id="@+id/white_rect4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_marginLeft="119dp" android:layout_marginTop="129dp" android:layout_toRightOf="@+id/textView1" android:src="@drawable/white_rect_y" /><ImageView android:id="@+id/white_rect5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_marginLeft="119dp" android:layout_marginTop="129dp" android:layout_toRightOf="@+id/textView1" android:src="@drawable/white_rect_y" /> <ImageView android:id="@+id/white_rect6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_marginLeft="119dp" android:layout_marginTop="129dp" android:layout_toRightOf="@+id/textView1" android:src="@drawable/white_rect_y" /> <ImageView android:id="@+id/white_rect7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_marginLeft="119dp" android:layout_marginTop="129dp" android:layout_toRightOf="@+id/textView1" android:src="@drawable/white_rect_y" /> <ImageView android:id="@+id/white_rect8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_marginLeft="119dp" android:layout_marginTop="129dp" android:layout_toRightOf="@+id/textView1" android:src="@drawable/white_rect_y" /> <ImageView android:id="@+id/white_rect9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_marginLeft="119dp" android:layout_marginTop="129dp" android:layout_toRightOf="@+id/textView1" android:src="@drawable/white_rect_y" /> <ImageView android:id="@+id/white_rect10" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_marginLeft="119dp" android:layout_marginTop="129dp" android:layout_toRightOf="@+id/textView1" android:src="@drawable/white_rect_y" /> <ImageView android:id="@+id/white_rect11" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_marginLeft="119dp" android:layout_marginTop="129dp" android:layout_toRightOf="@+id/textView1" android:src="@drawable/white_rect_y" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/white_rect1" android:layout_marginLeft="18dp" android:layout_marginTop="96dp" android:layout_toRightOf="@+id/textView1" android:text="Button"/></RelativeLayout>
白色小方块。
0 0
- 动画效果监听事件
- Android 动画监听事件
- 监听animation动画事件
- 监听动画的事件
- ScrollView滑动监听实现界面动画效果
- Animation动画开始、结束、重复事件监听
- 对css3动画事件的监听
- android 给动画添加结束监听事件
- 设置动画结束的监听事件
- Button的监听事件和点击效果
- AndroidUI 视图动画-混合动画效果 (AnimationSet)/动画效果监听
- Android 动画效果(二):四种基础动画的 **动态设置、动画监听、组合动画
- 页面切换动画效果(tabBar事件)
- trigger触发图片控制事件--动画效果
- android 触摸事件冒泡动画效果
- jQuery绑定事件及动画效果
- Android 属性动画监听事件与一个菜单的例子
- 属性动画03-animatorset和监听事件(打辅助)
- hibernate---java.lang.UnsupportedOperationException: The user must supply a JDBC connection
- Flex获取项目路径
- 浅谈ORACLE中ORA-06502:PL/SQL:数字或值错误:字符到数值的转换错误
- 关于开发简易搜索引擎的一些总结和思考
- Android学习笔记之RadioButton的使用详解
- 动画效果监听事件
- 负载均衡--服务器单机集群以及session共享技术分享和交流
- 在Macbook上配置使用Android自动化测试工具uiautomator
- 使用Oracle显式游标及for循环
- OpenGL入门6——GLUT窗口
- SOJ--4389: 川大贴吧水王
- sql知识归纳
- 登陆框注入~我要中奖
- OpenStack 最小化安装配置(四):RabbitMQ服务配置