android-魔法泡泡动画分析(附源码)

  来看贴图

  原图魔法效果:(透明的有些看不清)

  

  PS之后加了背景色并放大后的效果

  

  在屏幕中的效果(左上很小的那个,其他都是背景图):

  

  中间很小的那个就是

  先看动画实现代码explosion.xml(explosion意思是爆发)

  <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
  android:oneshot="true">
  <item android:drawable="@drawable/explode1" android:duration="70" />
  <item android:drawable="@drawable/explode2" android:duration="70" />
  <item android:drawable="@drawable/explode3" android:duration="70" />
  <item android:drawable="@drawable/explode4" android:duration="70" />
  <item android:drawable="@drawable/explode5" android:duration="70" />
  </animation-list>

 

   手指点击后产生泡泡的动画是5张40*40的图片顺序播放产生的,每张持续时间为70毫秒,播放模式为oneshot,即一次。
  BubbleExplosion.java

复制代码
  package com.ray.bubble;
  import android.app.Activity;
  import android.content.Context;
  import android.graphics.drawable.AnimationDrawable;
  import android.os.Bundle;
  import android.view.MotionEvent;
  import android.view.View;
  import android.view.Window;
  import android.view.WindowManager;
  import android.view.View.OnTouchListener;
  import android.widget.FrameLayout;
  import android.widget.ImageView;
  public class BubbleExplosion extends Activity {
  private FrameLayout fl;
  private ExplosionView exv1;
  private AnimationDrawable exa1;
  //        private Contact contact;
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  //set full screen
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,
  WindowManager.LayoutParams. FLAG_FULLSCREEN);
  fl = new FrameLayout(this);
  fl.setBackgroundResource(R.drawable.bg);
  exv1 = new ExplosionView(this);
  exv1.setVisibility(View.INVISIBLE);
  exv1.setBackgroundResource(R.anim.explosion);
  exa1 = (AnimationDrawable)exv1.getBackground();
  fl.addView(exv1);
  fl.setOnTouchListener(new LayoutListener());
  setContentView(fl);
  }
  class ExplosionView extends ImageView{
  public ExplosionView(Context context) {
  super(context);
  }
  //handle the location of the explosion
  public void setLocation(int top,int left){
  this.setFrame(left, top, left+40, top+40);
  }
  }
  class LayoutListener implements OnTouchListener{
  public boolean onTouch(View v, MotionEvent event) {
  //first u have to stop the animation,or if the animation
  //is starting ,u can start it again!
  exv1.setVisibility(View.INVISIBLE);
  exa1.stop();
  float x = event.getX();
  float y = event.getY();
  exv1.setLocation((int)y-20, (int)x-20);
  exv1.setVisibility(View.VISIBLE);
  exa1.start();
  return false;
  }
  }
  }
复制代码

  精华提炼:
  1.Line 31 exv1.setBackgroundResource(R.anim.explosion);
  exv1是继承自ImageView的视图,看到他将一个animation设置成背景了,惊讶!原来动画可以设置为背景图。
  2.Line 32 exa1 = (AnimationDrawable)exv1.getBackground();
  Line 60 exa1.start();
  不仅仅Aniamtion有start()方法,原来AnimationDrawable作为一个Drawable的子类也可以有start()方法哦。
  没见过吧,之前我也没见过;见过啦?我现在也见过了!
  再补充几个常识性的
  3.setContentView(fl);
  用代码绘制布局,完全没用到layout/main.xml~~
  4.Line 23-25
  设置全屏

点击下载附件

posted on   vus520  阅读(1212)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架

导航

< 2012年6月 >
27 28 29 30 31 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
1 2 3 4 5 6 7
点击右上角即可分享
微信分享提示