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;

}

}

}

}

你可能感兴趣的:(android,落叶效果)