安卓动画爱心雨和大风车动画+擦除动画

爱心雨动画 使用第三方fallingview实现

先看效果:

安卓动画爱心雨和大风车动画+擦除动画_第1张图片

第一步:导入依赖

implementation 'com.dingmouren.fallingview:fallingview:1.0.2'

第二步:布局文件

安卓动画爱心雨和大风车动画+擦除动画_第2张图片

主类:

   fallingView=findViewById(R.id.fall_view);
     fallingView.setImageResource(R.drawable.a_3);//设置碎片的图片,默认的图片是雪花
     fallingView.setDensity(80);//设置密度,数值越大,碎片越密集,默认值是 80
     fallingView.setScale(100);//设置碎片的大小,数值越大,碎片越小,默认值是 3
     fallingView.setDelay(10);//设置碎片飘落的速度,数值越大,飘落的越慢,默认值是 10

这样爱心雨就出来啦!

2.大风车动画自定义View

看一下效果:

自定义View类

package com.example.study_03_view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

        public class WindView extends View implements Runnable {

            Context context;
            Paint paint;
            Paint paint2;
            Paint paint3;
            Paint paintLine;


            int height; //当前的高度
            int width;//当前的宽度

            int sector1;
            int sector2;
            int sector3;

            public WindView(Context context, AttributeSet attrs) {
                super(context, attrs);

                //初始化画笔
                paint=new Paint();
                paint.setAntiAlias(true);
                paint.setColor(Color.RED);
                paint.setStrokeWidth(3);
                paint.setStyle(Paint.Style.FILL);

                paint2=new Paint();
                paint2.setAntiAlias(true);
                paint2.setColor(Color.GREEN);
                paint2.setStrokeWidth(3);
                paint2.setStyle(Paint.Style.FILL);

                paint3=new Paint();
                paint3.setAntiAlias(true);
                paint3.setColor(Color.BLUE);
                paint3.setStrokeWidth(3);
                paint3.setStyle(Paint.Style.FILL);

                paintLine=new Paint();
                paintLine.setAntiAlias(true);
                paintLine.setColor(Color.BLACK);
                paintLine.setStrokeWidth(3);
                paintLine.setStyle(Paint.Style.FILL);


            }

            @Override
            protected void onSizeChanged(int w, int h, int oldw, int oldh) {
                super.onSizeChanged(w, h, oldw, oldh);
                //获取当前正在改变的宽高
                width=w;
                height=h;
            }

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

                //画个线
        canvas.drawLine(width/2,height/2,width/2,height/2-100,paintLine);

        //画三个扇形
        RectF rectF = new RectF(-(width/4), -(width/4), (width/4), (width/4));
        canvas.translate(width/2,height/2);
        canvas.drawArc(rectF,sector1,30,true,paint);
        canvas.drawArc(rectF,sector2,30,true,paint2);
        canvas.drawArc(rectF,sector3,30,true,paint3);

    }


    @Override
    public void run() {
        //重绘   invalidate();
        //异步重绘   postInvalidate();

        while (true) {
            sector1 +=20;
            sector2 = sector1 + 120;
            sector3 = sector2 + 120;
            postInvalidate();
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}

布局文件:这是我们自定义View的名字

安卓动画爱心雨和大风车动画+擦除动画_第3张图片

测试类: 因为风车是动的吗所以我们使用线程操作他

在这里插入图片描述

自定义擦除动画

package com.example.study_06_view;

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.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

public class WipeView extends View {

private Context context;
private Paint paint;
private Path path;
private Canvas mycanvas;
private int gao;
private int width;
private Bitmap bfBit;
private Bitmap afBit;

public WipeView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context=context;
    init();
}

private void init() {
    //获取窗口的管理者的宽和高
    WindowManager windowManager =(WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics displayMetrics = new DisplayMetrics();
    windowManager.getDefaultDisplay().getMetrics(displayMetrics);
    gao = displayMetrics.heightPixels;
    width = displayMetrics.widthPixels;

    //初始化画笔
    paint=new Paint();
    //防锯齿
    paint.setAntiAlias(true);
    //防抖动
    paint.setDither(true);
    //样式
    paint.setStyle(Paint.Style.STROKE);
    //透明度
    paint.setARGB(128,255,0,0);
    //设置路径结合处样式
    paint.setStrokeJoin(Paint.Join.ROUND);
    //设置混合模式  DSI_IH
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    paint.setStrokeWidth(40);
    //设置笔触类型
    paint.setStrokeCap(Paint.Cap.ROUND);




    //生成前景的bitmap 设置图片质量参数
    bfBit = Bitmap.createBitmap(width,gao,Bitmap.Config.ARGB_8888);
    //将其注入画布
    mycanvas=new Canvas(bfBit);
    //灰色
    mycanvas.drawColor(Color.GRAY);
   //背景图
    afBit= BitmapFactory.decodeResource(context.getResources(), R.drawable.hai1);
    afBit = Bitmap.createScaledBitmap(afBit,width, gao, true);
    path=new Path();

}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    //设置背景
    canvas.drawBitmap(afBit,0,0,null);
    //设置前景
    canvas.drawBitmap(bfBit,0,0,null);
    mycanvas.drawPath(path,paint);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX();
    float y = event.getY();
    switch (event.getAction()){
        case MotionEvent.ACTION_DOWN:
           path.moveTo(x,y);
            break;
        case MotionEvent.ACTION_MOVE:
            path.lineTo(x,y);
            break;
    }
    invalidate();
    return true;
}

}

效果:

安卓动画爱心雨和大风车动画+擦除动画_第4张图片

你可能感兴趣的:(安卓动画爱心雨和大风车动画+擦除动画)