关于Drawable动画效果的实现demo(参考ray的博客例子)

直接上代码把,上面都写了很清楚的注释:

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 parentLayout;
private ExplosionView customView;
private AnimationDrawable exa1;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置无标题栏,全屏效果
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

// 代码创建一个FrameLayout,设置背景图片
parentLayout = new FrameLayout(this);
parentLayout.setBackgroundResource(R.drawable.bg);

// 代码创建一个自定义ImageView,并添加到上面的FrameLayout当中
// 并且设置这个ImageView有个动画的背景效果,
// 设置为不可见
customView = new ExplosionView(this);
customView.setVisibility(View.INVISIBLE);
customView.setBackgroundResource(R.anim.explosion);

// 获取动画效果背景的图像
exa1 = (AnimationDrawable) customView.getBackground();

parentLayout.addView(customView);
// 设置页面点击监听
parentLayout.setOnTouchListener(new LayoutListener());
setContentView(parentLayout);
}

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!
customView.setVisibility(View.INVISIBLE);
// 如果动画已在运行,则不起效果
exa1.stop();
float x = event.getX();
float y = event.getY();
// 重新定位ImageView在layout上的坐标位置
customView.setLocation((int) y - 20, (int) x - 20);
customView.setVisibility(View.VISIBLE);
exa1.start();
return false;
}

}
}

还有重要的就是这个anim文件夹中的explosion.xml

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


[color=red]附件是图片资源[/color]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值