只能原地动的小鲤鱼/(ㄒoㄒ)/~~

我是一边听b站上老师讲的课程一边打出来的代码,以至于他为什么不可以任意方向的游动,没有水波纹,因为老师的教程里面没有教/(ㄒoㄒ)/~~后面我会努力写出剩下的代码,具体时间,就不一定了。

我这个,因为我设置的参数可能不对,所以导致,他摆动起来,有点子怪异。

 

这个是文件列表

 MainActivity.java

package com.example.fishdrawable;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView ivfish=findViewById(R.id.iv_fish);
        ivfish.setImageDrawable(new FishDrawable());

    }
}

FishDrawable.java

package com.example.fishdrawable;

import android.animation.ValueAnimator;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.animation.LinearInterpolator;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class FishDrawable extends Drawable {

    private Path mPath;
    private Paint mPaint;

    private final static int OTHER_ALPHA = 110;//身体外的透明度
    private final static int BOOY_ALPHA = 160;//身体内的透明度

    public static final int Head_radius = 100;

    private PointF middlePoint;

    private float fishMainAngle = 90;//鱼刚开始的角度

    private final static float BODY_length = 3.2f * Head_radius;//鱼身的长度

    private final static float FIND_fins_length = 0.9f * Head_radius;//寻找鱼鳍起点的线长
    private final static float FINS_length = 1.3f * Head_radius;

    //-----------鱼尾------------
    //尾部大圆的半径(圆心就是身体底部的中点)
    private final float BIG_CIRCLE_RADIUS = Head_radius * 0.7f;
    //尾部中圆的半径
    private final float MIDDLE_CIRCLE_RADIUS = BIG_CIRCLE_RADIUS * 0.6f;
    //尾部小圆的半径
    private final float SMALL_CIRCLE_RADIUS = MIDDLE_CIRCLE_RADIUS * 0.4f;
    //寻找尾部中圆圆心的线长
    private final float FIND_MIDDLE_CIRCLE_LENGTH = BIG_CIRCLE_RADIUS + MIDDLE_CIRCLE_RADIUS;
    //寻找尾部小圆圆心的线长
    private final float FIND_SMALL_CIRCLE_LENGTH = MIDDLE_CIRCLE_RADIUS * (0.4f + 2.7f);
    //寻找大三角形底边中心点的线长
    private final float FIND_TRIANGLE_LENGTH = MIDDLE_CIRCLE_RADIUS * 2.7f;

    private float currentValue = 0;
    private float currentValue1 = 0;
    private float currentValue2 = 0;

    public FishDrawable() {
        init();
    }

    private void init() {
        mPath = new Path();//路径
        mPaint = new Paint();//画笔
        mPaint.setAntiAlias(true);//抗锯齿
        mPaint.setStyle(Paint.Style.FILL);//画笔类型填充

        mPaint.setARGB(OTHER_ALPHA, 244, 92, 71);//设置颜色

        middlePoint = new PointF(4.18f * Head_radius, 4.18f * Head_radius);//鱼的重心的坐标

        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 360);//鱼身子,鱼节肢,鱼尾巴的摆动速度不同,可以使用三次这些的代码,更改速度就可以了,例如下面注释的一个
        valueAnimator.setDuration(1000);//摆动的速度
        valueAnimator.setRepeatMode(ValueAnimator.RESTART);
        valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {//监听器
            @Override
            public void onAnimationUpdate(ValueAnimator animator) {
                currentValue = (float) animator.getAnimatedValue();
                //每变化一次,就要刷新一次
                invalidateSelf();
            }
        });
        valueAnimator.start();//启动动画,想要有变化,就一定要有这个,当动画没有变动的时候,就先想这行代码有没有加上


//        ValueAnimator  valueAnimator1=ValueAnimator.ofFloat(-1,1);//
//        valueAnimator1.setDuration(1500);//摆动的速度
//        valueAnimator1.setRepeatMode(ValueAnimator.REVERSE);
//        valueAnimator1.setRepeatCount(ValueAnimator.INFINITE);
//        valueAnimator1.setInterpolator(new LinearInterpolator());
//        valueAnimator1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {//监听器
//            @Override
//            public void onAnimationUpdate(ValueAnimator animator) {
//                currentValue1=(float)animator.getAnimatedValue();
//                //每变化一次,就要刷新一次
//                invalidateSelf();
//            }
//        });
//        valueAnimator1.start();

    }

    /**
     * @param startPoint 起始点的坐标
     * @param length     两点之间的长度
     * @param angle      两点连线和x坐标的夹角
     * @return
     */
    public static PointF calculatPoint(PointF startPoint, float length, float angle) {
        float deltax = (float) Math.cos(Math.toRadians(angle)) * length;//得到了x坐标,可正可负
        float deltay = (float) Math.sin(Math.toRadians(angle - 180)) * length;//得到了y坐标,安卓中y的坐标是向下的,数学中是负的
        return new PointF(startPoint.x + deltax, startPoint.y + deltay);
    }

    //类似自定义View onDraw方法
    @Override
    public void draw(@NonNull Canvas canvas) {
        float fishAngle = (float) (fishMainAngle + Math.sin(Math.toRadians(currentValue)) * 10);//旋转的角度是从-10到10的变化,sin是从-1到1

        PointF headPoint = calculatPoint(middlePoint, BODY_length / 2, fishAngle);
        canvas.drawCircle(headPoint.x, headPoint.y, Head_radius, mPaint);//绘制圆

        //右鱼鳍
        PointF rigthFishPoint = calculatPoint(headPoint, FIND_fins_length, fishAngle - 110);//减去110是因为是负的110,右鱼鳍
        makeFins(canvas, rigthFishPoint, fishAngle, true);//绘制鱼鳍的方法,贝塞尔曲线

        //左鱼鳍
        PointF leftFishPoint = calculatPoint(headPoint, FIND_fins_length, fishAngle + 110);//减去110是因为是负的110,右鱼鳍
        makeFins(canvas, leftFishPoint, fishAngle, false);//绘制鱼鳍的方法,贝塞尔曲线

        //身体底部的中心点
        PointF bodyBottomCenterPoint = calculatPoint(headPoint, BODY_length, fishAngle - 180);
        //画节肢1
        PointF middleCircleCenterPoint = makeSegment(canvas, bodyBottomCenterPoint, BIG_CIRCLE_RADIUS, MIDDLE_CIRCLE_RADIUS,
                FIND_MIDDLE_CIRCLE_LENGTH, fishAngle, true);

        //画节肢2
        makeSegment(canvas, middleCircleCenterPoint, MIDDLE_CIRCLE_RADIUS, SMALL_CIRCLE_RADIUS,
                FIND_MIDDLE_CIRCLE_LENGTH, fishAngle, false);

        //画尾巴  findEdgeLength是一个变化值
        makeTriangle(canvas, middleCircleCenterPoint, FIND_TRIANGLE_LENGTH,
                BIG_CIRCLE_RADIUS, fishAngle);
        makeTriangle(canvas, middleCircleCenterPoint, FIND_TRIANGLE_LENGTH - 10,
                BIG_CIRCLE_RADIUS - 20, fishAngle);

        //画身体
        makeBody(canvas, headPoint, bodyBottomCenterPoint, fishAngle);


    }

    private void makeBody(Canvas canvas, PointF headPoint,
                          PointF bodyBottomCenterPoint, float fishAngle) {
        //身体的四个点
        PointF topLeftPoint = calculatPoint(headPoint, Head_radius, fishAngle + 90);
        PointF topRightPoint = calculatPoint(headPoint, Head_radius, fishAngle - 90);
        PointF bottomLeftPoint = calculatPoint(bodyBottomCenterPoint, BIG_CIRCLE_RADIUS, fishAngle + 90);
        PointF bottomRightPoint = calculatPoint(bodyBottomCenterPoint, BIG_CIRCLE_RADIUS, fishAngle - 90);

        //二阶贝塞尔曲线的控制点,决定鱼的胖瘦
        PointF controlLeft = calculatPoint(headPoint, BODY_length * 0.56f, fishAngle + 130);
        PointF controlRight = calculatPoint(headPoint, BODY_length * 0.56f, fishAngle - 130);

        //画身体
        mPath.reset();
        mPath.moveTo(topLeftPoint.x, topLeftPoint.y);
        mPath.quadTo(controlLeft.x, controlLeft.y, bottomLeftPoint.x, bottomLeftPoint.y);
        mPath.lineTo(bottomRightPoint.x, bottomRightPoint.y);
        mPath.quadTo(controlRight.x, controlRight.y, topRightPoint.x, topRightPoint.y);
        mPaint.setAlpha(BOOY_ALPHA);
        canvas.drawPath(mPath, mPaint);
    }

    private void makeTriangle(Canvas canvas, PointF startPoint, float findCenterLength,
                              float findEdgeLength, float fishAngle) {

        float segmentAngle;
        segmentAngle = (float) (fishAngle + Math.sin(Math.toRadians(currentValue * 1.1)) * 30);

        //三角形底边的中心点
        PointF centerPoint = calculatPoint(startPoint, findCenterLength, segmentAngle - 180);
        //三角形底边的两点
        PointF leftPoint = calculatPoint(centerPoint, findEdgeLength, segmentAngle + 90);
        PointF rightPoint = calculatPoint(centerPoint, findEdgeLength, segmentAngle - 90);
        //绘制三角形
        mPath.reset();
        mPath.moveTo(startPoint.x, startPoint.y);
        mPath.lineTo(leftPoint.x, leftPoint.y);
        mPath.lineTo(rightPoint.x, rightPoint.y);
        canvas.drawPath(mPath, mPaint);
    }

    private PointF makeSegment(Canvas canvas, PointF bottomCenterPoint,
                               float bigRadius, float smallRadius,
                               float findSmallCircleLength, float fishAngle, boolean hasBigCircle) {

        float segmentAngle;

        if (hasBigCircle) {
            segmentAngle = (float) (fishAngle + Math.sin(Math.toRadians(currentValue * 1.1)) * 30);//节肢1,*2是原来频率的二倍
        } else {
            segmentAngle = (float) (fishAngle + Math.sin(Math.toRadians(currentValue * 1.3)) * 50);//节肢2,*3是原来频率的三倍
        }

        //梯形上底的中心点(中等大的圆的圆心)
        PointF upperCenterPoint = calculatPoint(bottomCenterPoint, findSmallCircleLength, segmentAngle - 180);
        //梯形的四个点
        PointF bottomLeftPoint = calculatPoint(bottomCenterPoint, bigRadius, segmentAngle + 90);
        PointF bottomRightPoint = calculatPoint(bottomCenterPoint, bigRadius, segmentAngle - 90);
        PointF upperLeftPoint = calculatPoint(upperCenterPoint, smallRadius, segmentAngle + 90);
        PointF upperRightPoint = calculatPoint(upperCenterPoint, smallRadius, segmentAngle - 90);
        if (hasBigCircle) {
            //画大圆
            canvas.drawCircle(bottomCenterPoint.x, bottomCenterPoint.y, bigRadius, mPaint);
        }
        //画小圆
        canvas.drawCircle(upperCenterPoint.x, upperCenterPoint.y, smallRadius, mPaint);
        //画梯形
        mPath.reset();
        mPath.moveTo(bottomLeftPoint.x, bottomLeftPoint.y);
        mPath.lineTo(upperLeftPoint.x, upperLeftPoint.y);
        mPath.lineTo(upperRightPoint.x, upperRightPoint.y);
        mPath.lineTo(bottomRightPoint.x, bottomRightPoint.y);
        canvas.drawPath(mPath, mPaint);

        //绘制节肢2和三角形的起始点,必须在这儿返回
        return upperCenterPoint;
    }


    //绘制鱼鳍的方法,贝塞尔曲线
    private void makeFins(Canvas canvas, PointF startPoint, float fishAngle, boolean isRightFins) {
        float controlAngle = 115;

        //二阶贝塞尔曲线的控制点的坐标
        PointF contrloPoint = calculatPoint(startPoint, 1.8f * FIND_fins_length,
                isRightFins ? fishAngle - controlAngle : fishAngle + controlAngle);

        //结束点坐标
        PointF endPoint = calculatPoint(startPoint, FIND_fins_length, fishAngle - 180);

        mPath.reset();
        mPath.moveTo(startPoint.x, startPoint.y);
        mPath.quadTo(contrloPoint.x, contrloPoint.y, endPoint.x, endPoint.y);
        canvas.drawPath(mPath, mPaint);
    }

    @Override
    public void setAlpha(int i) {
        mPaint.setAlpha(i);
    }

    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {
        mPaint.setColorFilter(colorFilter);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }

    @Override
    public int getIntrinsicWidth() {//活动的区域是一个正方形,可以旋转
        return (int) (8.38f * Head_radius);//8.38
    }

    @Override
    public int getIntrinsicHeight() {//活动的区域是一个正方形,可以旋转
        return (int) (8.38f * Head_radius);
    }



}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <ImageView
        android:id="@+id/iv_fish"
        android:layout_width="33dp"
        android:layout_height="35dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张子怡です

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值