android 点击效果动画增强

本文介绍如何使用开源库为Android视图添加放大+透明度的动画效果,包括设计回调接口,实现在点击事件中调用动画,并在动画完成时恢复视图状态。还提供了额外的动画效果如摇摆/震动,通过示例代码和视频演示实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

android 点击效果动画增强

1.使用开源库制作view 的放大+透明度的动画

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. animate(v).scaleX(factor).scaleY(factor).alpha(0);  

2. 设计回调接口
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public interface ClickAnimation {  
  2.     public void onClick(View v);  
  3. }  

3. 在view 的onclick 的处理中增加动画的调用,并把以前的onclick事件处理 作为回调加入到animation中,在动画完成后回调处理函数。在动画结束时将view的状态恢复到原始状态

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. private void animateClickView(final View v, final ClickAnimation callback) {  
  2.     float factor = 2;  
  3.     animate(v).scaleX(factor).scaleY(factor).alpha(0).setListener(new AnimatorListenerAdapter() {  
  4.         @Override  
  5.         public void onAnimationEnd(Animator animation) {  
  6.             ViewHelper.setScaleX(v, 1);  
  7.             ViewHelper.setScaleY(v, 1);  
  8.             ViewHelper.setAlpha(v, 1);  
  9.             if (callback != null) {  
  10.                 callback.onClick(v);  
  11.             }  
  12.             super.onAnimationEnd(animation);  
  13.         }  
  14.     });  
  15. }  

4. TODO:可以依据这个结构,添加更多动画效果。比如摇摆/震动等

实现效果参见youku视频:

http://v.youku.com/v_show/id_XNjYzOTI4NzMy.html


具体实现参加代码:

java文件

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.buptfarmer.devapp;  
  2.   
  3. import static com.nineoldandroids.view.ViewPropertyAnimator.animate;  
  4.   
  5. import com.nineoldandroids.animation.Animator;  
  6. import com.nineoldandroids.animation.AnimatorListenerAdapter;  
  7. import com.nineoldandroids.view.ViewHelper;  
  8.   
  9. import android.app.Activity;  
  10. import android.os.Bundle;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.widget.Button;  
  14. import android.widget.ImageView;  
  15. import android.widget.TextView;  
  16. import android.widget.Toast;  
  17.   
  18. public class ClickAnimationExample extends Activity implements OnClickListener {  
  19.   
  20.     private Button aaaBtn;  
  21.     private Button bbbBtn;  
  22.     private ImageView bbbImg;  
  23.     private Button cccBtn;  
  24.     private TextView aaaText;  
  25.   
  26.     @Override  
  27.     protected void onCreate(Bundle savedInstanceState) {  
  28.         // TODO Auto-generated method stub  
  29.         super.onCreate(savedInstanceState);  
  30.         setContentView(R.layout.click_animation_example);  
  31.         initView();  
  32.     }  
  33.   
  34.     private void initView() {  
  35.   
  36.         aaaBtn = (Button) findViewById(R.id.aaa_btn);  
  37.         aaaBtn.setOnClickListener(this);  
  38.         aaaText = (TextView) findViewById(R.id.aaa_text);  
  39.         aaaText.setOnClickListener(this);  
  40.         bbbBtn = (Button) findViewById(R.id.bbb_btn);  
  41.         bbbBtn.setOnClickListener(this);  
  42.         bbbImg = (ImageView) findViewById(R.id.bbb_img);  
  43.         bbbImg.setOnClickListener(this);  
  44.     }  
  45.   
  46.     private void animateClickView(final View v, final ClickAnimation callback) {  
  47.         float factor = 2;  
  48.         animate(v).scaleX(factor).scaleY(factor).alpha(0).setListener(new AnimatorListenerAdapter() {  
  49.             @Override  
  50.             public void onAnimationEnd(Animator animation) {  
  51.                 ViewHelper.setScaleX(v, 1);  
  52.                 ViewHelper.setScaleY(v, 1);  
  53.                 ViewHelper.setAlpha(v, 1);  
  54.                 if (callback != null) {  
  55.                     callback.onClick(v);  
  56.                 }  
  57.                 super.onAnimationEnd(animation);  
  58.             }  
  59.         });  
  60.     }  
  61.   
  62.     @Override  
  63.     public void onClick(View v) {  
  64.         // TODO Auto-generated method stub  
  65.         animateClickView(v, new ClickAnimation() {  
  66.             @Override  
  67.             public void onClick(View v) {  
  68.                 // TODO Auto-generated method stub  
  69.                 if (v == aaaBtn) {  
  70.                     Toast.makeText(getApplicationContext(), "aaaBtn has been clicked", Toast.LENGTH_SHORT)  
  71.                             .show();  
  72.                 } else if (v == aaaText) {  
  73.                     Toast.makeText(getApplicationContext(), "aaaText has been clicked", Toast.LENGTH_SHORT)  
  74.                             .show();  
  75.                 } else if (v == bbbBtn) {  
  76.                     Toast.makeText(getApplicationContext(), "bbbBtn has been clicked", Toast.LENGTH_SHORT)  
  77.                             .show();  
  78.                 } else if (v == bbbImg) {  
  79.                     Toast.makeText(getApplicationContext(), "bbbImg has been clicked", Toast.LENGTH_SHORT)  
  80.                             .show();  
  81.                 }  
  82.             }  
  83.         });  
  84.     }  
  85.   
  86.     public interface ClickAnimation {  
  87.         public void onClick(View v);  
  88.     }  
  89. }  

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. layout 布局文件  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical"  
  7.     android:paddingBottom="@dimen/activity_vertical_margin"  
  8.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  9.     android:paddingRight="@dimen/activity_horizontal_margin"  
  10.     android:paddingTop="@dimen/activity_vertical_margin"  
  11.     tools:context=".MainActivity" >  
  12.   
  13.     <ImageView  
  14.         android:id="@+id/bbb_img"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:contentDescription="bbb_img"  
  18.         android:src="@drawable/ic_launcher" />  
  19.   
  20.     <TextView  
  21.         android:layout_width="wrap_content"  
  22.         android:layout_height="wrap_content"  
  23.         android:text="@string/hello_world" />  
  24.   
  25.     <LinearLayout  
  26.         android:layout_width="match_parent"  
  27.         android:layout_height="wrap_content"  
  28.         android:orientation="horizontal" >  
  29.   
  30.         <Button  
  31.             android:id="@+id/aaa_btn"  
  32.             android:layout_width="wrap_content"  
  33.             android:layout_height="wrap_content"  
  34.             android:text="aaa" />  
  35.   
  36.         <TextView  
  37.             android:id="@+id/aaa_text"  
  38.             android:layout_width="wrap_content"  
  39.             android:layout_height="wrap_content"  
  40.             android:text="aaa_text" />  
  41.     </LinearLayout>  
  42.   
  43.     <LinearLayout  
  44.         android:layout_width="match_parent"  
  45.         android:layout_height="wrap_content"  
  46.         android:orientation="horizontal" >  
  47.   
  48.         <Button  
  49.             android:id="@+id/bbb_btn"  
  50.             android:layout_width="wrap_content"  
  51.             android:layout_height="wrap_content"  
  52.             android:text="bbb" />  
  53.     </LinearLayout>  
  54.   
  55.     <Button  
  56.         android:id="@+id/ccc_btn"  
  57.         android:layout_width="wrap_content"  
  58.         android:layout_height="wrap_content"  
  59.         android:text="ccc" />  
  60.   
  61.   
  62. </LinearLayout>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值