如何在布局中获得此类视图?
//我的DrawView类
public class DrawViewEdit extends View {
Paint paint = new Paint();
JSONObject layersJsonObject;
private void init() {
this.setWillNotDraw(false);
paint.setColor(Color.BLACK);
paint.setStrokeWidth(5);
}
public DrawViewEdit(Context context, JSONObject layersJsonObject) {
super(context);
this.layersJsonObject = layersJsonObject;
init();
}
public DrawViewEdit(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public DrawViewEdit(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(500, 800);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
try {
canvas.drawLine(Float.parseFloat("" + Integer.parseInt(layersJsonObject.getString("x1")) * 1.5),
Float.parseFloat("" + Integer.parseInt(layersJsonObject.getString("y1")) * 1.5),
Float.parseFloat("" + Integer.parseInt(layersJsonObject.getString("x2")) * 1.5),
Float.parseFloat("" + Integer.parseInt(layersJsonObject.getString("y2")) * 1.5), paint);
canvas.drawLine(Float.parseFloat("" + Integer.parseInt(layersJsonObject.getString("x2")) * 1.5),
Float.parseFloat("" + Integer.parseInt(layersJsonObject.getString("y2")) * 1.5),
Float.parseFloat("" + Integer.parseInt(layersJsonObject.getString("x3")) * 1.5),
Float.parseFloat("" + Integer.parseInt(layersJsonObject.getString("y3")) * 1.5), paint);
canvas.drawLine(Float.parseFloat("" + Integer.parseInt(layersJsonObject.getString("x3")) * 1.5),
Float.parseFloat("" + Integer.parseInt(layersJsonObject.getString("y3")) * 1.5),
Float.parseFloat("" + Integer.parseInt(layersJsonObject.getString("x4")) * 1.5),
Float.parseFloat("" + Integer.parseInt(layersJsonObject.getString("y4")) * 1.5), paint);
canvas.drawLine(Float.parseFloat("" + Integer.parseInt(layersJsonObject.getString("x4")) * 1.5),
Float.parseFloat("" + Integer.parseInt(layersJsonObject.getString("y4")) * 1.5),
Float.parseFloat("" + Integer.parseInt(layersJsonObject.getString("x1")) * 1.5),
Float.parseFloat("" + Integer.parseInt(layersJsonObject.getString("y1")) * 1.5), paint);
} catch (JSONException e1) {
e1.printStackTrace();
}
}
}
我尝试过使用canvas和frameLayout但没有得到正确的结果,所以有人能告诉我如何获得这种类型的视图吗? I got this result