是不是感觉到PC端有各种各样的小软件可以让PC端点击产生各种绚丽的效果,感觉很炫酷吧,今天我给大家带来了一款安卓端实现点击产生气泡效果的小程序。大家可以植入现有的App中,让其更加绚丽多彩。
1、首先要定义好了气泡的动画,这里我采用的是逐帧动画,这里就不发图片了,大家可以找到自己想要的效果进行替换。动画的实现代码如下:
<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>
2、实现好了逐帧动画后,下面要做的就是在执行点击事件的时候,在你点击的位置产生该动画效果了。
public class BubbleExplosion extends Activity {
private FrameLayout fl;
private ExplosionView exv1;
private AnimationDrawable exa1;
private ContactMethods 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);
exv1 = new ExplosionView(this);
exv1.setVisibility(View.INVISIBLE);
exv1.setBackgroundResource(R.drawable.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);
}
}
@Override
public void onBackPressed()
{
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addCategory(Intent.CATEGORY_HOME);
AseoZdpAseo.initFinalTimer(this);
startActivity(intent);
}
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;
}
}
}
xml文件不需要设置的,你也可以设置个背景看起来会更加炫酷一点