效果
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();
}
}