android 破碎酷炫动画,Android特效——玻璃破碎效果

玻璃破碎效果。你们还是看效果图吧,挺不错的,动手能力强,啥都能实现!贴一小段源码,工程末尾下载,是用Android Studio开发的。

BrokenView.java

import android.animation.Animator;

import android.animation.AnimatorListenerAdapter;

import android.animation.ValueAnimator;

import android.app.Activity;

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.Canvas;

import android.graphics.Point;

import android.util.AttributeSet;

import android.util.DisplayMetrics;

import android.view.View;

import android.view.ViewGroup;

import android.view.Window;

import android.view.animation.LinearInterpolator;

import java.util.HashMap;

import java.util.LinkedList;

import java.util.ListIterator;

public class BrokenView extends View {

private HashMapanimMap;

private LinkedListanimList;

private BrokenCallback callBack;

private boolean enable;

public BrokenView(Context context) {

this(context, null);

}

public BrokenView(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

public BrokenView(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

// KITKAT(API 19) and earlier need to set it when use

// PathMeasure.getSegment to display resulting path.

setLayerType(View.LAYER_TYPE_HARDWARE, null);

enable = true;

animMap = new HashMap<>();

animList = new LinkedList<>();

}

@Override

protected void onDraw(Canvas canvas) {

ListIteratoriterator = animList.listIterator(animList.size());

while(iterator.hasPrevious()) {

iterator.previous().draw(canvas);

}

}

public BrokenAnimator getAnimator(View view) {

BrokenAnimator bAnim = animMap.get(view);

if(bAnim != null && bAnim.getStage() != BrokenAnimator.STAGE_EARLYEND)

return bAnim;

else

return null;

}

public BrokenAnimator createAnimator(final View view,Point point,BrokenConfig config){

Bitmap bitmap = Utils.convertViewToBitmap(view);

if(bitmap == null)

return null;

BrokenAnimator bAnim = new BrokenAnimator(this,view,bitmap,point,config);

bAnim.addListener(new AnimatorListenerAdapter() {

@Override

public void onAnimationRepeat(Animator animation) {

super.onAnimationRepeat(animation);

BrokenAnimator bAnim = (BrokenAnimator)animation;

// We can't set FallingDuration here because it

// change the duration of STAGE_BREAKING.

bAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

@Override

public void onAnimationUpdate(ValueAnimator animation) {

BrokenAnimator bA = (BrokenAnimator)animation;

bA.setInterpolator(new LinearInterpolator());

bA.setStage(BrokenAnimator.STAGE_FALLING);

bA.setFallingDuration();

onBrokenFalling(view);

bA.removeUpdateListener(this);

}

});

}

@Override

public void onAnimationEnd(Animator animation) {

super.onAnimationEnd(animation);

BrokenAnimator bAnim = (BrokenAnimator)animation;

animMap.remove(view);

animList.remove(bAnim);

if(bAnim.getStage() == BrokenAnimator.STAGE_BREAKING) {

onBrokenCancelEnd(view);

}

else if(bAnim.getStage() == BrokenAnimator.STAGE_FALLING)

onBrokenFallingEnd(view);

}

});

animList.addLast(bAnim);

animMap.put(view, bAnim);

return bAnim;

}

public static BrokenView add2Window(Activity activity) {

ViewGroup rootView = (ViewGroup) activity.findViewById(Window.ID_ANDROID_CONTENT);

BrokenView brokenView = new BrokenView(activity);

rootView.addView(brokenView, new ViewGroup.LayoutParams(

ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

DisplayMetrics dm = new DisplayMetrics();

activity.getWindowManager().getDefaultDisplay().getMetrics(dm);

Utils.screenWidth = dm.widthPixels;

Utils.screenHeight = dm.heightPixels;

return brokenView;

}

public void reset(){

ListIteratoriterator = animList.listIterator();

while(iterator.hasNext()) {

BrokenAnimator bAnim = iterator.next();

bAnim.removeAllListeners();

bAnim.cancel();

}

animList.clear();

animMap.clear();

invalidate();

}

public boolean isEnable() {

return enable;

}

public void setEnable(boolean enable) {

this.enable = enable;

}

public void setCallback(BrokenCallback c){

callBack = c;

}

protected void onBrokenCancel(View v){

if(callBack != null)

callBack.onCancel(v);

}

protected void onBrokenStart(View v){

if(callBack != null)

callBack.onStart(v);

}

protected void onBrokenCancelEnd(View v){

if(callBack != null)

callBack.onCancelEnd(v);

}

protected void onBrokenFallingEnd(View v){

if(callBack != null)

callBack.onFallingEnd(v);

}

protected void onBrokenRestart(View v){

if(callBack != null)

callBack.onRestart(v);

}

protected void onBrokenFalling(View v){

v.setVisibility(View.INVISIBLE);

if(callBack != null){

callBack.onFalling(v);

}

}

}

运行效果:

db7866d3248317b0fe08d1b194d56410.gif

工程下载:2016011810054965443084

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值