我想使用布尔变量来绘制一个或另一个类。我有2个类(例如A和B),它们扩展了具有onDraw方法的View。现在我想创建一个通过布尔值控制的类,如果我绘制A或B;
当我创建我的活动时,我想创建一个这个类的视图,如果我将它的布尔变量设置为true / false来绘制A / B;
我可以把所有东西放在一个类中,并有两个方法来绘制不同的元素。但我想要两个单独的课程
我不知道继承是否是正确答案,我不知道该怎么做
public class Parent extends View {
private boolean modeCell;
private A cellNote;
private B cellTest;
public Parent(Context context) {
super(context);
}
public Parent(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (modeCell)
{
cellTest.draw(canvas);//??
}
else{
cellNote.draw(canvas);//??
}
}
public boolean isModeCell() {
return modeCell;
}
public void setModeCell(boolean modeCell) {
this.modeCell = modeCell;
}
public A getCellNote() {
return cellNote;
}
public void setCellNote(A cellNote) {
this.cellNote = cellNote;
}
public B getCellTest() {
return cellTest;
}
public void setCellTest(B cellTest) {
this.cellTest = cellTest;
}
}
public class A extends Parent {
private int[] noteNumbers;
private int cellNoteSize;
private int width;
private int height;
private int noteWidth;
private int noteHeight;
private int paddingLeft;
private int paddingRight;
private int paddingTop;
private int paddingBottom;
public A(Context context) {
super(context);
noteNumbers = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9};
cellNoteSize = (int) Math.sqrt(noteNumbers.length);
}
public A(Context context, AttributeSet attrs) {
super(context, attrs);
noteNumbers = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9};
cellNoteSize = (int) Math.sqrt(noteNumbers.length);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
this.width = getMeasuredWidth();
this.height = getMeasuredHeight();
this.width = width - paddingLeft - paddingRight;
this.height = height - paddingTop - paddingBottom;
this.paddingLeft = getPaddingLeft();
this.paddingRight = getPaddingRight();
this.paddingTop = getPaddingTop();
this.paddingBottom = getPaddingBottom();
this.noteWidth = width / cellNoteSize;
this.noteHeight = height / cellNoteSize;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint mCellNotePaint = new Paint();
mCellNotePaint.setAntiAlias(true);
Paint rectPaint = new Paint();
RectF rect = new RectF(paddingLeft, paddingTop, paddingLeft + width, paddingTop + height);
rectPaint.setColor(Color.YELLOW);
rectPaint.setStyle(Paint.Style.FILL);
canvas.drawRect(rect, rectPaint);
Rect rectNote = new Rect(paddingLeft, paddingTop, paddingLeft + noteWidth, paddingTop + noteHeight);
rectPaint.setColor(Color.RED);
rectPaint.setStyle(Paint.Style.FILL);
canvas.drawRect(rectNote, rectPaint);
String textSample = (cellNoteSize <= 3) ? "9" : "16";
float fontSize = adjustFontSize(mCellNotePaint, textSample, noteHeight, 80);
Rect bounds = new Rect();
mCellNotePaint.getTextBounds(textSample, 0, textSample.length(), bounds);
float measureText = mCellNotePaint.measureText(textSample);
float xOffset = (noteWidth - measureText) / 2;
float yOffset = (noteHeight + bounds.height()) / 2;
mCellNotePaint.setTextSize(fontSize);
for (int number = 1; number <= noteNumbers.length; number++) {
int n = number - 1;
int c = n % cellNoteSize;
int r = n / cellNoteSize;
String textValue = String.valueOf(number);
canvas.drawText(textValue, paddingLeft + c * noteWidth + xOffset, paddingTop + r * noteHeight + yOffset, mCellNotePaint);
}
}
public int getCellNoteSize() {
return cellNoteSize;
}
public void setCellNoteSize(int cellNoteSize) {
this.cellNoteSize = cellNoteSize;
}
private float adjustFontSize(Paint paint, String text, float ViewHeight, float percentage) {
paint.setTextSize(100);
paint.setTextScaleX(1.0f);
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
int h = bounds.bottom - bounds.top;
float target = (float) ViewHeight * percentage / 100f;
float size = ((target / h) * 100f);
return size;
}
}
public class B extends Parent {
//variables
//onDraw something else
}
布局在我的活动布局中,我希望有1个父视图来绘制A / B.
a = new A(this);
b = new B(this);
parent = findViewById(R.id.idparent);
parent.setModeCell(true);//or false
parent.setA(a);
parent.setB(b);
发布于 2019-06-10 14:23:22
你可以用这个方法做 check the type of View is A or B
if (view instanceof A){
//do something
}
else if (view instanceof B){
//do something
}
https://stackoverflow.com/questions/-100006967
复制相似问题