十分钟搞定酷炫动画,万圣节惊悚的聊天界面

MyView child = new MyView(view.getContext());
suitableParent.addView(child);
}

private static ViewGroup findSuitableParent(View view) {
ViewGroup fallback = null;
do {
if (view instanceof FrameLayout) {
if (view.getId() == android.R.id.content) {
return (ViewGroup) view;
} else {
fallback = (ViewGroup) view;
}
}
if (view != null) {
final ViewParent parent = view.getParent();
view = parent instanceof View ? (View) parent : null;
}
} while (view != null);
return fallback;
}

private static class MyView extends View implements View.OnClickListener {

private Bitmap mIcon;
private Paint mPaint;
private int mWidth;
private int mHeight;
private int showCount;
private int shake;
private ArrayList mInfo = new ArrayList<>();
Matrix mMatrix = new Matrix();

public MyView(Context context) {
super(context);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
mWidth = MeasureSpec.getSize(widthMeasureSpec);
mHeight = MeasureSpec.getSize(heightMeasureSpec);
init();
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

private void init() {
if (mWidth == 0)
return;
mIcon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_face_shock);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(Color.WHITE);

mInfo.clear();

int width = (int) (mIcon.getWidth() * 0.5);
int height = (int) (mIcon.getHeight() * 0.5);
int centerX = mWidth / 2 - width / 2;
mInfo.add(new AnimationInfo(0.5F, 0, centerX + width * 4F, mHeight * 3 / 4 - height));
mInfo.add(new AnimationInfo(0.55F, 20, centerX + width * 3.4F, mHeight * 3 / 4 - height * 2.2F));
mInfo.add(new AnimationInfo(0.6F, 340, centerX + width * 2.6F, mHeight * 3 / 4 - height * 3.5F));

mInfo.add(new AnimationInfo(0.65F, 20, centerX - width, mHeight / 2 - height));
mInfo.add(new AnimationInfo(0.6F, 340, centerX - width * 2.8F, mHeight / 2 + height));

mInfo.add(new AnimationInfo(0.5F, 20, centerX + width * 0.5F, mHeight / 4 - height * 2F));

mInfo.add(new AnimationInfo(0.7F, 320, centerX, mHeight / 2F));

mInfo.add(new AnimationInfo(0.5F, 40, centerX - width * 0.8F, mHeight / 2 + height * 3F));

mInfo.add(new AnimationInfo(0.7F, 250, centerX - width * 2F, mHeight / 2 - height * 2F));

mInfo.add(new AnimationInfo(0.6F, 320, centerX - width * 3F, mHeight / 2 - height * 1.5F));

mInfo.add(new AnimationInfo(0.7F, 45, centerX + width * 3F, mHeight / 2 - height * 2F));

mInfo.add(new AnimationInfo(0.75F, 20, centerX, mHeight / 2 - height * 2.5F));

mInfo.add(new AnimationInfo(0.6F, 320, centerX + width * 1.5F, mHeight / 2 - height * 4F));

mInfo.add(new AnimationInfo(0.6F, 45, centerX + width * 0.5F, mHeight / 2 - height * 4.5F));

mInfo.add(new AnimationInfo(0.5F, 320, centerX - width * 1.8F, mHeight / 2 - height * 5F));

mInfo.add(new AnimationInfo(0.6F, 100, centerX + width * 1.8F, mHeight / 2 + height * 3F));

mInfo.add(new AnimationInfo(0.5F, 320, centerX, mHeight / 2 + height * 5F));

mInfo.add(new AnimationInfo(0.6F, 10, centerX - width * 0.5F, mHeight / 2 + height * 1.5F));

showCount = 0;

setOnClickListener(this);

ObjectAnimator animator1, animator2, animator3;

animator1 = ObjectAnimator.ofInt(this, “showCount”, mInfo.size());
animator1.setDuration(mInfo.size() * 35);

animator2 = ObjectAnimator.ofInt(this, “shake”, 0, 20, 0, -20, 0, 20, 0, -20);
animator2.setDuration(300);

PropertyValuesHolder scaleXValuesHolder = PropertyValuesHolder.ofFloat(“scaleX”, 1, 8);
PropertyValuesHolder scaleYValuesHolder = PropertyValuesHolder.ofFloat(“scaleY”, 1, 8);
Keyframe keyframe1 = Keyframe.ofFloat(0, 1);
Keyframe keyframe2 = Keyframe.ofFloat(0.5f, 1);
Keyframe keyframe3 = Keyframe.ofFloat(1, 0);
PropertyValuesHolder alphaValuesHolder = PropertyValuesHolder.ofKeyframe(“alpha”, keyframe1, keyframe2, keyframe3);
animator3 = ObjectAnimator.ofPropertyValuesHolder(this, scaleXValuesHolder, scaleYValuesHolder, alphaValuesHolder);
animator3.setDuration(200);

AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playSequentially(animator1, animator2, animator3);
animatorSet.start();
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

if (showCount < mInfo.size()) {
drawStep1(canvas);
} else {
drawStep2(canvas);
}

}

private void drawStep1(Canvas canvas) {
for (int i = 0; i < showCount && i < mInfo.size(); i++) {
AnimationInfo info = mInfo.get(i);
canvas.save();
mMatrix.reset();
mMatrix.postScale(info.scale, info.scale, info.x, info.y);
mMatrix.postRotate(info.rotate, info.x, info.y);
canvas.concat(mMatrix);
canvas.drawBitmap(mIcon, info.x, info.y, mPaint);
canvas.restore();
}
}

private void drawStep2(Canvas canvas) {
for (int i = 0; i < showCount && i < mInfo.size(); i++) {
AnimationInfo info = mInfo.get(i);
canvas.save();
mMatrix.reset();
float x = info.calculateTranslationX(shake);
float y = info.calculateTranslationY(shake);
mMatrix.postScale(info.scale, info.scale, x, y);
mMatrix.postRotate(info.rotate, x, y);
canvas.concat(mMatrix);
canvas.drawBitmap(mIcon, x, y, mPaint);
canvas.restore();
}
}

@Override
public void onClick(View v) {
ViewGroup parent = (ViewGroup) v.getParent();
parent.removeView(v);
}

@Keep
private void setShowCount(int showCount) {
this.showCount = showCount;
invalidate();
}

@Keep
private void setShake(int shake) {
this.shake = shake;
invalidate();
}

}

private static class AnimationInfo {
float scale;
float rotate;
float x;
float y;

public AnimationInfo(float scale, float rotate, float x, float y) {
this.scale = scale;
this.rotate = rotate;
this.x = x;
this.y = y;
}

public float calculateTranslationX(float length) {
return (float) Math.cos(rotate) * length + x;
}

public float calculateTranslationY(float length) {
return (float) Math.sin(rotate) * length + y;
}

}
}
代码比较简单,我就不写注释了。
有几个点我想提一下~

  • 正常情况下,MyView 命名是不规范的,MyView 也不应该作为 AnimationHelper 的内部类,可以把 MyView单独提出了,AnimationInfo作为 MyView 的内部类。
  • AnimationHelper 里面的findSuitableParent 方法拷贝自 SnackBar,这个方法我在分析 SnackBar 源码的时候讲过,就不再赘述了。
  • onDraw 方法里面的drawStep1 和drawStep2其实可以合并成一个方法,我为了便于大家理解所以两个方法分开写了。
  • 如果你还有什么可以优化的代码,尽管拍砖,我都接着~
  • 绘制里面用到了 canvas 和Matrix 相关的代码如果看不懂可以去看扔物线的文章。
  • 以后想到再补充~
    喜欢记得点个关注哦~

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

尾声

对于很多初中级Android工程师而言,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长且无助。 整理的这些架构技术希望对Android开发的朋友们有所参考以及少走弯路,本文的重点是你有没有收获与成长,其余的都不重要,希望读者们能谨记这一点。

最后想要拿高薪实现技术提升薪水得到质的飞跃。最快捷的方式,就是有人可以带着你一起分析,这样学习起来最为高效,所以为了大家能够顺利进阶中高级、架构师,我特地为大家准备了一套高手学习的源码和框架视频等精品Android架构师教程,保证你学了以后保证薪资上升一个台阶。

当你有了学习线路,学习哪些内容,也知道以后的路怎么走了,理论看多了总要实践的。

进阶学习视频

附上:我们之前因为秋招收集的二十套一二线互联网公司Android面试真题 (含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)

[外链图片转存中…(img-guxKUAAb-1713527196872)]

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 24
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值