安卓java显示心形告白

发布于:2024-12-08 ⋅ 阅读:(87) ⋅ 点赞:(0)

HeartView .java

package com.example.xinxing;

import android.content.Context;
import android.graphics.*;
import android.util.AttributeSet;
import android.view.View;
import androidx.core.content.ContextCompat;

public class HeartView extends View {

    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private float heartWidth = 0f;
    private float heartHeight = 0f;
    private float centerX = 0f;
    private float centerY = 0f;
    private int currentColor;

    public HeartView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // 设置随机颜色
        currentColor = getRandomColor();
        paint.setColor(currentColor);
        paint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        heartWidth = w * 0.5f;  // 增大心形宽度
        heartHeight = heartWidth * 0.8f;  // 调整高度比例
        centerX = w / 2f;
        centerY = h / 2f;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 绘制心形
        canvas.drawPath(createHeartPath(), paint);
    }

    private Path createHeartPath() {
        Path path = new Path();
        path.moveTo(centerX, centerY + heartHeight / 4); // 起始点
        path.cubicTo(
                centerX - heartWidth / 2, centerY,
                centerX - heartWidth, centerY + heartHeight / 2,
                centerX, centerY + heartHeight // 左半边
        );
        path.cubicTo(
                centerX + heartWidth, centerY + heartHeight / 2,
                centerX + heartWidth / 2, centerY,
                centerX, centerY + heartHeight / 4 // 右半边
        );
        path.close();
        return path;
    }

    private int getRandomColor() {
        return ContextCompat.getColor(getContext(), android.R.color.holo_red_light);
    }
}

MainActivity

package com.example.xinxing;

import androidx.appcompat.app.AppCompatActivity;
import androidx.interpolator.view.animation.FastOutSlowInInterpolator;

import android.os.Bundle;

import android.view.animation.Animation;
import android.view.animation.Transformation;


public class MainActivity extends AppCompatActivity {

    private HeartView heartView;

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

        heartView = findViewById(R.id.heartView);

        // 启动心形动画
        startHeartAnimation();
    }

    private void startHeartAnimation() {
        final Animation animation = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                float scale = 0.5f + interpolatedTime * 1.5f; // 使得心形从0.5倍到2倍大小变化
                heartView.setScaleX(scale);
                heartView.setScaleY(scale);
                heartView.setAlpha(1 - interpolatedTime); // 透明度变化
            }

            @Override
            public void initialize(int width, int height, int parentWidth, int parentHeight) {
                super.initialize(width, height, parentWidth, parentHeight);
                setDuration(3000);
                setInterpolator(new FastOutSlowInInterpolator());
            }
        };

        animation.setRepeatCount(Animation.INFINITE); // 设置无限循环
        animation.setRepeatMode(Animation.REVERSE); // 设置反向播放

        heartView.startAnimation(animation);
    }
}

xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/shootingstar">

    <com.example.xinxing.HeartView
        android:id="@+id/heartView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

</FrameLayout>

在这里插入图片描述


网站公告

今日签到

点亮在社区的每一天
去签到