android 落叶效果,安卓版落叶特效-canvas篇(一)

package net.cloudelevator.anim;

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.os.Handler;

import android.os.Message;

import android.util.AttributeSet;

import android.view.View;

import androidx.annotation.NonNull;

import androidx.annotation.Nullable;

import java.util.ArrayList;

import java.util.List

public class MyCanvas extends View {

Paint paint;

Paint tpaint;

private int[] list = new int[192];

Handler handler = new Handler() {

@Override

public void handleMessage(@NonNull Message msg) {

switch (msg.what) {

case 1:

postInvalidate();

handler.sendEmptyMessageDelayed(1, 0);

break;

}

}

};

public MyCanvas(Context context) {

super(context);

for (int i = 0; i < 192; i++) {

list[i] = 1;

}

for (int j = 0; j < 100; j++) {

for (int i = 0; i < list.length; i++) {

if (list[i] > 758 + Math.random() * 1e4) {

list[i] = 0;

} else {

list[i] = list[i] + 10;

}

}

}

handler.sendEmptyMessageDelayed(1, 0);

}

public MyCanvas(Context context, @Nullable AttributeSet attrs) {

super(context, attrs);

for (int i = 0; i < 192; i++) {

list[i] = 1;

}

for (int j = 0; j < 100; j++) {

for (int i = 0; i < list.length; i++) {

if (list[i] > 758 + Math.random() * 1e4) {

list[i] = 0;

} else {

list[i] = list[i] + 10;

}

}

}

handler.sendEmptyMessageDelayed(1, 0);

}

public MyCanvas(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

for (int i = 0; i < 192; i++) {

list[i] = 1;

}

for (int j = 0; j < 100; j++) {

for (int i = 0; i < list.length; i++) {

if (list[i] > 758 + Math.random() * 1e4) {

list[i] = 0;

} else {

list[i] = list[i] + 10;

}

}

}

handler.sendEmptyMessageDelayed(1, 0);

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

paint = new Paint();

tpaint = new Paint();

tpaint.setColor(Color.WHITE);

paint.setStrokeWidth(0);

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bg);

canvas.drawBitmap(bitmap, 0, 0, paint);

for (int i = 0; i < list.length; i++) {

canvas.drawText("\uD83C\uDF42", i * 50, list[i], tpaint);

if (list[i] > 758 + Math.random() * 1e4) {

list[i] = 0;

} else {

list[i] = list[i] + 10;

}

}

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值