我在对话框片段上有13个按钮,其中12个以圆圈的形式布置,一个在中间。我正在使用约束布局来排列按钮。
使用自定义视图,我在12个按钮中的每个按钮与中间覆盖ondraw的按钮之间划了一条线。 在每行的末尾,我添加了另一个自定义视图,该视图使用路径通过覆盖ondraw来绘制箭头。
单击按钮时,将显示一个文本视图并将其相对于该按钮放置。我为每个按钮使用相同的文本视图,然后将其重新放置在代码中。
单击按钮后,响应速度很慢,单击和显示自身的文本视图之间存在明显的犹豫。顺便说一句,这仅在我的Galaxy a10平板电脑上可见。尽管在Galaxy S8智能手机上进行测试时,它并不明显。
似乎随着重复重画屏幕而陷入困境。虽然我不知道如何诊断。
从代码的角度来看,我真的不确定要发布什么。首先,我在对话框片段中(对于前两个按钮)列出了自定义行,自定义箭头和onclick事件。
请让我知道这些内容是否看起来很奇怪,或者是否要查看其他内容。我不是经验丰富的android程序员,而是通过stackoverflow和其他android站点来学习的。
public class LineView extends View {
private int mXStart, mYStart , mXEnd, mYEnd;
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Globals g =Globals.getInstance();
Resources res = getResources();
float lineStrokePercent;
MainActivity mMainActivit = new MainActivity();
int stroke;
public LineView(Context context) {
super(context);
}
public LineView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public LineView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
@Override
public Parcelable onSaveInstanceState() {
super.onSaveInstanceState();
Bundle bundle = new Bundle();
// Save current View's state here
return bundle;
}
@Override
public void onRestoreInstanceState(Parcelable state) {
super.onRestoreInstanceState(state);
}
private void init(){
int color = ContextCompat.getColor(getContext(), R.color.GradientStart);
paint.setColor(color);
paint.setAntiAlias(true);
boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
if(tabletSize){
stroke=getResources().getInteger(R.integer.lineStrokeTablet);
paint.setStrokeWidth(stroke);}
else
{stroke=getResources().getInteger(R.integer.lineStrokeSmartPhone);
paint.setStrokeWidth(stroke); }}
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawLine(mXStart, mYStart, mXEnd, mYEnd, paint);
}
public void setCoords(int xStart, int yStart, int xEnd, int yEnd)
{ mXStart = xStart;
mYStart = yStart;
mXEnd = xEnd;
mYEnd = yEnd;
}}
这是我自定义的箭头
public class Triangle extends View {
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private Path path;
private int x,y,width,halfWidth;
Globals g =Globals.getInstance();
Resources res = getResources();
float lineStrokePercent;
int stroke;
public Triangle(Context context) {
super(context);
}
public Triangle(Context context, AttributeSet attrs) {
super(context, attrs);
init();
int x= 30; int y= 30;int width=30;
halfWidth = width / 2;
path = new Path();
path.moveTo(x, y - halfWidth); // Top
path.lineTo(x - halfWidth, y + halfWidth); // Bottom left
path.lineTo(x + halfWidth, y + halfWidth); // Bottom right
path.lineTo(x, y - halfWidth); // Back to Top
path.close();
}
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(path, paint);
}
private void init(){
int color = ContextCompat.getColor(getContext(), R.color.GradientStart);
paint.setColor(color);
paint.setAntiAlias(true);
paint.setDither(true);
boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
if(tabletSize)
{stroke=getResources().getInteger(R.integer.lineStrokeTablet);}
else
{stroke=getResources().getInteger(R.integer.lineStrokeSmartPhone);}
paint.setStrokeWidth(stroke);
}
public void setTriCoords(int xStart, int yStart, int triwidth,int angle) {
init();
boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
if(tabletSize)
{stroke=getResources().getInteger(R.integer.lineStrokeTablet);}
else
{stroke=getResources().getInteger(R.integer.lineStrokeSmartPhone);}
x = xStart;//+(stroke/2);
y = yStart;//+(stroke/2);
width = triwidth;
halfWidth = width / 2;
path = new Path();
path.moveTo(x, y - halfWidth); // Top
path.lineTo(x - halfWidth, y + halfWidth); // Bottom left
path.lineTo(x + halfWidth, y + halfWidth); // Bottom right
path.lineTo(x, y - halfWidth); // Back to Top
path.close();
Matrix mMatrix = new Matrix();
RectF bounds = new RectF();
path.computeBounds(bounds, true);
mMatrix.postRotate(angle, bounds.centerX(), bounds.centerY());
path.transform(mMatrix);
}
这是对话框片段中前两个按钮的onclick。
// Balance ring text boxes
@Override
public void onClick(View v) {
ConstraintLayout mConstraintLayout;
textViewheader = getView().findViewById(R.id.balancetextheader);
textView = getView().findViewById(R.id.balancetextt);
textViewheader.setElevation(50);
textView.setElevation(50);
if (textViewheader.getVisibility() == View.VISIBLE) {
textViewheader.setVisibility(View.INVISIBLE);
textView.setVisibility(View.INVISIBLE);
} else {
textViewheader.setVisibility(View.VISIBLE);
textView.setVisibility(View.VISIBLE);
btn0width = btn0.getWidth();
balancetextwidth = (int) (diameter * 1.2);
balancetextheight = (int) (diameter * 1);
textView.setMovementMethod(new ScrollingMovementMethod());
//set baltext width and height
mConstraintLayout = (ConstraintLayout) getView().findViewById(R.id.mybalancelayout);
mConstraintSetBaltext.clone(mConstraintLayout);
mConstraintSetBaltext.constrainWidth(R.id.balancetextheader,balancetextwidth);
mConstraintSetBaltext.constrainWidth(R.id.balancetextt,balancetextwidth);
mConstraintSetBaltext.constrainMaxHeight(R.id.balancetextt,balancetextheight);
mConstraintSetBaltext.applyTo(mConstraintLayout);
//view_instance.bringToFront();
float angle = 0;
Resources res = getResources();
boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
float HorizontalGuidePos1 =.26f;
float HorizontalGuidePos2 =.38f;
float HorizontalGuidePos3 =.52f;
float VerticalGuidePos1=.32f;
float VerticalGuidePos2=.4f;
float VerticalGuidePos3=.47f;
switch (v.getId()) {
case R.id.x0:
textView.setText(getString(R.string.BalanceTxt));
textViewheader.setText(btn0.getText());
//guideline
if (tabletSize) {
mConstraintSetBaltext.setGuidelinePercent(R.id.guidelinebalTextHoriz, .1f);
mConstraintSetBaltext.setGuidelinePercent(R.id.guidelinebalTextVer, .1f);
}
else {
mConstraintSetBaltext.setGuidelinePercent(R.id.guidelinebalTextHoriz,0.1f);
mConstraintSetBaltext.setGuidelinePercent(R.id.guidelinebalTextVer, 0.6f);
}
//balance text
mConstraintSetBaltext.connect(R.id.balancetextheader, ConstraintSet.START, R.id.guidelinebalTextVer, ConstraintSet.END);
mConstraintSetBaltext.connect(R.id.balancetextheader, ConstraintSet.TOP, R.id.guidelinebalTextHoriz, ConstraintSet.BOTTOM);
mConstraintSetBaltext.applyTo(mConstraintLayout);
break;
case R.id.x1:
textView.setText(getString(R.string.SweetTxt));
textViewheader.setText(btn1.getText());
//guideline
if (tabletSize) {
mConstraintSetBaltext.setGuidelinePercent(R.id.guidelinebalTextHoriz, HorizontalGuidePos1);
mConstraintSetBaltext.setGuidelinePercent(R.id.guidelinebalTextVer, VerticalGuidePos2);
}
else {
mConstraintSetBaltext.setGuidelinePercent(R.id.guidelinebalTextHoriz,HorizontalGuidePos1);
mConstraintSetBaltext.setGuidelinePercent(R.id.guidelinebalTextVer, VerticalGuidePos2);
}
mConstraintSetBaltext.applyTo(mConstraintLayout);
break;
感谢您的帮助。