我正在android studio中执行我的应用程序, 我有一个扩展视图的类,我用它来绘制一些路径,我想在画布上添加一个SVG,
我知道有canvas.drawBitmap();
和canvas.drawPicture();
,也许他们可以帮助我在特定位置绘制SVG?
我最终想要一种在视图中显示SVG的方法。
这是我的画布:
public class ChessBordWidget extends View {
public class ChessSlice extends Path {
public ChessSlice() {
isInThreat = false;
threatenedBy = "";
isOcupied = false;
color = "white";
}
public boolean isInThreat;
public boolean isOcupied;
public String threatenedBy;
public String color;
}
Paint paintChessSquareWhite, paintChessSquareBlack, paintChessOuterRim;
Boolean isTouching = false;
float touchY, touchX;
ChessSlice chessSquare;
ChessSlice framePath;
ArrayList<ChessSlice> chessPathsArray = new ArrayList<ChessSlice>();
int width = 340; // default numbers until the screen will change it when it gets the actuall view
int height = 1200; // default numbers until the screen will change it when it gets the actuall view
int ROWS_COUNT = 8;
int COLUMS_COUNT = 8;
String TAG = "alignmentWidget";
/**
* construuctor
*
* @param context
*/
public ChessBordWidget(Context context) {
super(context);
this.postInvalidate();
init();
}
/**
* constructor
*
* @param context
* @param attrs
*/
public ChessBordWidget(Context context, AttributeSet attrs) {
super(context, attrs);
this.postInvalidate();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
width = getWidth();
height = getHeight();
init();
}
/**
* constructor
*
* @param context
* @param attrs
* @param defStyleAttr
*/
public ChessBordWidget(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.postInvalidate();
}
//////////////////////////////////
//////////////On Draw//////////////
//////////////////////////////////
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (chessPathsArray.size() == 0) { // initlizes the init
init();
}
// draws the first circle
////////////////////////////////////////////
// canvas.drawCircle(width / 2f, height / 2f, OUTER_RING_DIAMETER, firstCirclePaint);
//go through the array and paints the corisponding cells
///////////////////////////////////////////
for (int i = 0; i < chessPathsArray.size(); i++) {
if (chessPathsArray.get(i).color.equals("white")) {
canvas.drawPath(chessPathsArray.get(i), paintChessSquareWhite);
} else {
canvas.drawPath(chessPathsArray.get(i), paintChessSquareBlack);
}
}
//draw the frame
canvas.drawPath(framePath, paintChessOuterRim);
}
private void init() {
if (chessPathsArray.size() > 0) { // initlizes the init
return;
}
//gets teh width and height, initlized only after ondraw happend so it wouldn't be 0 0
width = getWidth();
height = getHeight();
//defining paints
///////////////////////////////////////////
paintChessSquareWhite = new Paint();
paintChessSquareWhite.setColor(getResources().getColor(R.color.lightChessPiece));
paintChessSquareWhite.setStyle(Paint.Style.FILL_AND_STROKE);
paintChessSquareBlack = new Paint();
paintChessSquareBlack.setColor(getResources().getColor(R.color.darkChessPiece));
paintChessSquareBlack.setStyle(Paint.Style.FILL_AND_STROKE);
paintChessOuterRim = new Paint();
paintChessOuterRim.setStrokeWidth(10f);
paintChessOuterRim.setColor(getResources().getColor(R.color.colorAccent));
paintChessOuterRim.setStyle(Paint.Style.STROKE);
// applyes hardware Acceleration
///////////////////////////////////////////
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setLayerType(LAYER_TYPE_SOFTWARE, paintChessSquareBlack);
}
// gets the main slices paths
///////////////////////////////////////////
for (int i = 0; i < ROWS_COUNT; i++) {
for (int j = 0; j < COLUMS_COUNT; j++) {
chessPathsArray.add(getSlicesPaths(i, j));
}
}
//draw frame
framePath = new ChessSlice();
framePath.moveTo(0, 0);
framePath.lineTo(width, 0);
framePath.lineTo(width, height);
framePath.lineTo(0, height);
framePath.lineTo(0, 0);
}
public ChessSlice getSlicesPaths(int i, int j) {
int columWidth = width/COLUMS_COUNT;
int rowHeight = height/ROWS_COUNT;
int startX = 0+(i*columWidth);
int startY = 0+(j*rowHeight);
ChessSlice segmentPath = new ChessSlice();
segmentPath.moveTo(startX, startY);
segmentPath.lineTo(startX+columWidth, startY);
segmentPath.lineTo(startX+columWidth, startY+rowHeight);
segmentPath.lineTo(startX, startY+rowHeight);
segmentPath.lineTo(startX, startY);
if ((i % 2) == 0) {
if ((j % 2) == 0) {
segmentPath.color = "white";
} else {
segmentPath.color = "black";
}
} else {
if ((j % 2) == 0) {
segmentPath.color = "black";
} else {
segmentPath.color = "white";
}
}
return segmentPath;
}
private TouchModel calculateTouchSquare (Float touchX, Float touchY) {
int columWidth = width/COLUMS_COUNT;
int rowHeight = height/ROWS_COUNT;
int selectectColum = (int) Math.floor(touchX/columWidth)+1;
int selectectRow = (int) Math.floor(touchY/rowHeight)+1;
TouchModel touchModel = new TouchModel(selectectColum, selectectRow);
return touchModel;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
switch(action){
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_DOWN:
touchX = event.getX();
touchY = event.getY();
isTouching = true;
TouchModel selectedCells = calculateTouchSquare(touchX, touchY);
Log.d (TAG, "touched: "+touchX+" and: "+touchY);
Log.d (TAG, "x: "+selectedCells.touchX+" y: "+selectedCells.touchY);
break;
default:
isTouching = false;
}
invalidate();
return true;
}
}
答案 0 :(得分:0)
使用:
修复它@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static Bitmap getBitmap(VectorDrawable vectorDrawable) {
Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
vectorDrawable.draw(canvas);
return bitmap;
}
private static Bitmap getBitmap(Context context, int drawableId) {
Drawable drawable = ContextCompat.getDrawable(context, drawableId);
if (drawable instanceof BitmapDrawable) {
return BitmapFactory.decodeResource(context.getResources(), drawableId);
} else if (drawable instanceof VectorDrawable) {
return getBitmap((VectorDrawable) drawable);
} else {
throw new IllegalArgumentException("unsupported drawable type");
}
}