Skip to content

Instantly share code, notes, and snippets.

@jebai0521
Last active February 21, 2025 15:45
Show Gist options
  • Save jebai0521/8d02160a2e15b9fffc0ee97b0e7a63e5 to your computer and use it in GitHub Desktop.
Save jebai0521/8d02160a2e15b9fffc0ee97b0e7a63e5 to your computer and use it in GitHub Desktop.
底盘电池 3D 投影效果
package com.skywell.car.myapplication.tanklevel;
import android.content.Context;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.Nullable;
public class BatteryView extends View {
Paint mPaint;
Camera camera = new Camera();
Matrix matrix = new Matrix();
public BatteryView(Context context) {
super(context);
init();
}
public BatteryView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public BatteryView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public BatteryView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
private void init() {
mPaint = new Paint();
mPaint.setColor(Color.CYAN);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setStrokeWidth(5);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// canvas.drawRect();
int paddingH = 40;
int paddingV = 40;
int width = getWidth() - paddingH * 2;
int height = getWidth() - paddingV * 2;
// int paddingHHalf = paddingH / 2;
// int paddingVHalf = paddingV / 2;
// int center1X = paddingH + (getWidth() - paddingH * 2) / 2;
// int center1Y = paddingH + (getHeight() - paddingV * 2) / 2;
int center1X = paddingH + width / 2;
int center1Y = paddingV + height / 2;
int center2X = 10 + getWidth() / 2;
int center2Y = 10 + getHeight() / 2;
mPaint.setColor(Color.BLUE);
mPaint.setAlpha(255);
canvas.drawRoundRect(paddingH, paddingV, getWidth() - paddingH, getHeight() - paddingV, 20, 20, mPaint);
mPaint.setColor(Color.CYAN);
mPaint.setAlpha(128);
camera.save();
matrix.reset();
camera.rotateX(30);
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-center1X, -center1Y);
matrix.postTranslate(center1X, center1Y);
canvas.save();
canvas.concat(matrix);
canvas.drawRoundRect(paddingH, paddingV, getWidth() - paddingH, getHeight() - paddingV, 10, 10, mPaint);
canvas.restore();
//
// camera.save();
// matrix.reset();
// camera.rotateY(30);
// camera.getMatrix(matrix);
// camera.restore();
// matrix.preTranslate(-center2X, -center2Y);
// matrix.postTranslate(center2X, center2Y);
// canvas.save();
// canvas.concat(matrix);
// canvas.drawRect(0, 0, getWidth(), getHeight(), mPaint);
// canvas.restore();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment