玻璃破碎效果。你们还是看效果图吧,挺不错的,动手能力强,啥都能实现!贴一小段源码,工程末尾下载,是用Android Studio开发的。
BrokenView.java
工程下载:2016011810054965443084
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 LinkedList animList; 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) { ListIterator iterator = 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(){ ListIterator iterator = 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); } } }
运行效果:
工程下载:2016011810054965443084
收藏的用户(0) X
正在加载信息~
推荐阅读
最新回复 (0)
站点信息
- 文章2302
- 用户1336
- 访客10974131
每日一句
Qingming Festival invites us to honor ancestors with quiet reflection and respect.
清明节邀请我们以静思与敬意祭奠祖先。
清明节邀请我们以静思与敬意祭奠祖先。
UAC的限制引起WM_DROPFILES无法响应的解决办法
MeasureSpec中三种模式:UNSPECIFIED,AT_MOST,EXACTLY
发几个实用的chrome插件
CentOS下使用 svnsync迁移SVN代码库
仙剑奇侠传3d回合-PC端辅助
【转载】C++实现EXE加载到内存执行
【收藏】OpenCV一些常用库函数
《闲来麻将》搭建教程
文本转语音系统Spark-TTS
wordpress转xiuno附件自动插入工具
Mac OS最简单及(Karabiner)快捷键设置
使用Putty上传文件?
ndk神奇问题之non-numeric second argument to `wordlist' function: '8.7z'
新会员