android Canvas View GradientDrawable cornerRadii

效果
效果
标题
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.GradientDrawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

public class SanView extends View {
    int width;
    int radius;
    Paint paint;

    private void init() {
        paint = new Paint();
        radius = 10;
        paint.setAntiAlias(true);
        paint.setColor(Color.RED);
    }

    public SanView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        this.width = getWidth();
        paint.setColor(Color.BLACK);
//        canvas.drawRect(0, 0, width, width, paint);
        double sqrt = Math.sqrt(width * width / 2);
        int left = (int) ((width - sqrt) / 2);
        Rect rect = new Rect(left, left, this.width - left, this.width - left);
        paint.setColor(Color.GREEN);
//        canvas.drawRect(rect, paint);
        Bitmap bitmap = Bitmap.createBitmap(rect.width(), rect.height(), Bitmap.Config.ARGB_8888);
        Canvas canvas1 = new Canvas(bitmap);
        GradientDrawable drawable = new GradientDrawable();
        float[] floats = new float[8];
        floats[0] = 4;
        floats[1] = 4;
        floats[2] = 4;
        floats[3] = 4;
        floats[4] = 4;
        floats[5] = 4;
        floats[6] = 4;
        floats[7] = 4;
        float newdeg = degrees % 45;
        if (degrees >= 0 && degrees < 45) {
            floats[4] = rect.width() * newdeg / 45;
            floats[5] = rect.width() * newdeg / 45;
        } else if (degrees >= 45 && degrees < 90) {
            floats[4] = rect.width() - rect.width() * newdeg / 45;
            floats[5] = rect.width() - rect.width() * newdeg / 45;
        } else if (degrees >= 90 && degrees < 135) {
            floats[2] = rect.width() * newdeg / 45;
            floats[3] = rect.width() * newdeg / 45;
        } else if (degrees >= 135 && degrees < 180) {
            floats[2] = rect.width() - rect.width() * newdeg / 45;
            floats[3] = rect.width() - rect.width() * newdeg / 45;
        } else if (degrees >= 180 && degrees < 225) {
            floats[0] = rect.width() * newdeg / 45;
            floats[1] = rect.width() * newdeg / 45;
        } else if (degrees >= 225 && degrees < 270) {
            floats[0] = rect.width() - rect.width() * newdeg / 45;
            floats[1] = rect.width() - rect.width() * newdeg / 45;
        } else if (degrees >= 270 && degrees < 315) {
            floats[7] = rect.width() * newdeg / 45;
            floats[6] = rect.width() * newdeg / 45;
        } else {
            floats[7] = rect.width() - rect.width() * newdeg / 45;
            floats[6] = rect.width() - rect.width() * newdeg / 45;
        }
        drawable.setCornerRadii(floats);
        drawable.setColor(Color.RED);
        drawable.setBounds(0, 0, rect.width(), rect.height());
        drawable.draw(canvas1);
        double ty = rect.width() / 4 * Math.sqrt(2);
        int newdeg2 = (int) (degrees / 45);
        if (newdeg2 % 2 == 0) {
            canvas.translate(0, (float) (ty * (newdeg / 45)));
            canvas.scale(1, 1.0f - 0.1f * newdeg / 45);
        } else {
            canvas.translate(0, (float) (ty - ty * (newdeg / 45)));
            canvas.scale(1, 0.9f + 0.1f * newdeg / 45);
        }
        canvas.rotate(degrees, getWidth() / 2, getWidth() / 2);
        canvas.drawBitmap(bitmap, rect.left, rect.top, paint);
    }

    float degrees = 0;

    public void setDegrees(float degrees) {
        this.degrees = degrees;
        invalidate();
    }

    public void start() {
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0f, 360f);
        valueAnimator.setRepeatCount(100);
        valueAnimator.setDuration(2000 * 10);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.addUpdateListener(animation -> {
            float value = (float) animation.getAnimatedValue();
            setDegrees(value);
        });
        valueAnimator.start();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值