问题
I am trying to draw lines between two views with some dynamic data getting from networking calls.
But when I am calling drawview
class its not working properly.
In this class I am creating buttons dynamically.
public class MatchTheFollowingAttempted extends RelativeLayout {
private Context mContext;
int numberOfOneSideButtons = 6;
ArrayList<Button> leftSideButtons = new ArrayList<>();
ArrayList<Button> rightSideButtons = new ArrayList<>();
ArrayList<Button> rightSideButtons2 = new ArrayList<>();
ArrayList<Button> rightSideButtons3 = new ArrayList<>();
ArrayList<Button> rightSideButtons4 = new ArrayList<>();
ArrayList<Button> rightSideButtons5 = new ArrayList<>();
ArrayList<Button> rightSideButtons6 = new ArrayList<>();
ArrayList<Button> allButtonsArraylist = new ArrayList<>();
// ArrayList<MatchTheColumnDrawView> matchTheColumnDrawViewArrayList = new ArrayList<>();
HashMap<String, Sample> hm = new HashMap<String, Sample>();
HashMap<String, Sample> hm1 = new HashMap<String, Sample>();
HashMap<String, Sample> hm2 = new HashMap<String, Sample>();
HashMap<String, Sample> hm3 = new HashMap<String, Sample>();
HashMap<String, Sample> hm4 = new HashMap<String, Sample>();
HashMap<String, Sample> hm5 = new HashMap<String, Sample>();
String[] colors = {
"#39add1", // light blue
"#3079ab", // dark blue
"#c25975", // mauve
"#e15258", // red
"#f9845b", // orange
"#838cc7" // lavender
/* "#7d669e", // purple
"#53bbb4", // aqua
"#51b46d", // green
"#e0ab18", // mustard
"#637a91", // dark gray
"#f092b0", // pink
"#b7c0c7" // light gray*/
};
String[] colors2 = {
"#3079ab",
"#e15258",
"#f9845b",
"#c25975",
"#39add1", // light blue
"#838cc7" // dark blue
//"#838cc7"
/* "#39add1", // light blue
"#3079ab", // dark blue
"#c25975", // mauve
"#e15258", // red
"#f9845b", // orange
"#838cc7", // lavender
"#7d669e" // purple
*/
};
private TextView textView,rightText,rightText2,rightText3,rightText4,rightText5,rightText6;
public MatchTheFollowingAttempted(Context context) {
super(context);
mContext = context;
// initialiseView();
}
public MatchTheFollowingAttempted(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
// initialiseView();
}
public MatchTheFollowingAttempted(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
// initialiseView();
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public MatchTheFollowingAttempted(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
mContext = context;
// initialiseView();
}
public void initialiseView() {
// ArrayList<MatchTheColumnDrawView> matchTheColumnDrawViewArrayList1 = new ArrayList<>();
leftSideButtonAdding();
rightSideButtonAdding();
/* rightSideButtons2Adding();
rightSideButtons3Adding();
rightSideButtons4Adding();
rightSideButtons5Adding();
rightSideButtons6Adding();
*/
matchtheLinedrawView(leftSideButtons, hm, hm1);
matchtheLinedrawView(rightSideButtons, hm1, hm2);
/* matchtheLinedrawView(rightSideButtons2);
matchtheLinedrawView(rightSideButtons3);
matchtheLinedrawView(rightSideButtons4);
matchtheLinedrawView(rightSideButtons5);*/
}
private void rightSideButtonAdding() {
for (int i = 0; i < numberOfOneSideButtons; i++) {
LayoutParams layoutParams = new LayoutParams(400, 150);
if (i == 0) {
rightText = new TextView(mContext);
rightText.setText("Test Heading 2 ");
rightText.setTextSize(18);
rightText.setId(View.generateViewId());
LayoutParams layoutParamstext = new LayoutParams(500, 200);
layoutParamstext.addRule(RIGHT_OF, textView.getId());
layoutParamstext.setMargins(200, 0, 0, 0);
rightText.setLayoutParams(layoutParamstext);
addView(rightText);
layoutParams.addRule(BELOW, rightText.getId());
}
Button mButton = new Button(mContext);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
mButton.setId(View.generateViewId());
}
Sample sample2 = new Sample(mButton.getId(), colors2[i], "Test" + i);
hm1.put(String.valueOf(i), sample2);
layoutParams.addRule(RIGHT_OF, leftSideButtons.get(i).getId());
if (i != 0) {
layoutParams.addRule(BELOW, rightSideButtons.get(i - 1).getId());
}
layoutParams.setMargins(250, 0, 10, 0);
mButton.setLayoutParams(layoutParams);
mButton.setText(sample2.getText());
mButton.setTextColor(Color.parseColor("#ffffff"));
mButton.setGravity(Gravity.CENTER_VERTICAL);
mButton.setPadding(40, 0, 0, 0);
mButton.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.right2x, 0);
GradientDrawable gradientDrawable = new GradientDrawable();
gradientDrawable.setColor(Color.parseColor(sample2.getColor()));
gradientDrawable.setCornerRadius(10);
gradientDrawable.setStroke(20, Color.parseColor(sample2.getColor()));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
mButton.setBackground(gradientDrawable);
}
rightSideButtons.add(mButton);
sample2.setBtn(mButton);
hm1.put(String.valueOf(i), sample2);
addView(mButton);
//sv.setLayoutParams(layoutParams);
}
}
private void leftSideButtonAdding() {
for (int i = 0; i < numberOfOneSideButtons; i++) {
LayoutParams layoutParams = new LayoutParams(400, 150);
if (i == 0) {
textView = new TextView(mContext);
textView.setText("Test Heading 1");
textView.setTextSize(18);
textView.setMaxLines(2);
textView.setId(View.generateViewId());
LayoutParams layoutParamstext = new LayoutParams(500, 200);
layoutParamstext.addRule(RelativeLayout.ALIGN_PARENT_TOP, TRUE);
textView.setLayoutParams(layoutParamstext);
addView(textView);
layoutParams.addRule(BELOW, textView.getId());
}
Button mButton = new Button(mContext);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
mButton.setId(View.generateViewId());
}
if (i != 0) {
layoutParams.addRule(BELOW, leftSideButtons.get(i - 1).getId());
}
mButton.setLayoutParams(layoutParams);
Sample sample = new Sample(mButton.getId(), colors[i], "Test" + i);
hm.put(String.valueOf(i), sample);
mButton.setText(sample.getText());
mButton.setTextColor(Color.parseColor("#ffffff"));
mButton.setGravity(Gravity.CENTER_VERTICAL);
mButton.setPadding(40, 0, 0, 0);
mButton.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.right2x, 0);
GradientDrawable gradientDrawable = new GradientDrawable();
gradientDrawable.setColor(Color.parseColor(sample.getColor()));
gradientDrawable.setCornerRadius(10);
gradientDrawable.setStroke(20, Color.parseColor(colors[i]));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
mButton.setBackground(gradientDrawable);
}
leftSideButtons.add(mButton);
sample.setBtn(mButton);
hm.put(String.valueOf(i), sample);
Point point = getPointOfView(mButton);
Log.d("TAG", "view point x,y (" + point.x + ", " + point.y + ")");
int[] location = new int[2];
mButton.getLocationInWindow(location);
Log.v("TAG,", "Button location\nx = " + location[0] + "\ny = " + location[1]);
addView(mButton);
}
}
private Point getPointOfView(View view) {
int[] location = new int[2];
view.getLocationInWindow(location);
return new Point(location[0], location[1]);
}
private void matchtheLinedrawView(ArrayList<Button> startButtons, HashMap<String, Sample> hmStart, HashMap<String, Sample> hmEnd) {
Sample getObject1 = null;
for (int i = 0; i < numberOfOneSideButtons; i++) {
Sample getObject = hmStart.get(String.valueOf(i));
for (int k = 0; k < numberOfOneSideButtons; k++) {
getObject1 = new ArrayList<Sample>(hmEnd.values()).get(k);
if (getObject.getColor().equals(getObject1.getColor())) {
MatchTheColumnDrawView matchTheColumnDrawView = new MatchTheColumnDrawView(
mContext, getObject.getBtn(),
getObject1.getBtn(),
getObject.getColor(), null, 10
, MatchTheColumnDrawView.LEFT_TO_RIGHT);
LayoutParams layoutParams = new LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams.addRule(ALIGN_BOTTOM,
startButtons.get(numberOfOneSideButtons - 1).getId());
matchTheColumnDrawView.setLayoutParams(layoutParams);
// matchTheColumnDrawViewArrayList.add(matchTheColumnDrawView);
addView(matchTheColumnDrawView);
}
}
}
}
}
In this class I am drawing lines between two buttons of separate columns:
@SuppressLint("ViewConstructor")
public class MatchTheColumnDrawView extends View {
public static final int LEFT_TO_RIGHT = 1, RIGHT_TO_LEFT = 2;
private Paint mLinePaint, mCirclePaint;
private View startView, endView;
private int direction;
private Canvas canvas;
private float dashWidth = 15f;
private float dashGap = 8f;
float[] intervals = new float[]{dashWidth, dashGap};
float phase = 0;
private int LINE_COLOR = Color.parseColor("#BEBEBE");
private int END_CIRCLE_COLOR = Color.parseColor("#FF99CC00");
/**
*
* parametrised constructor draws line from
* @startView
* to
* @endView
* as per
* @direction
* and
* @lineColor
* and
* @endCircleColor
*
* when
* @lineColor == null
* default lineColor is gray
*
* when
* @endCircleColor == null
* default endCircleColor is green
* */
public MatchTheColumnDrawView(Context context,
View startView, View endView,
String lineColor, String endCircleColor,
float thickness, int direction) {
super(context);
Log.v("TAG","columns");
mLinePaint = new Paint();
if (lineColor != null) LINE_COLOR = Color.parseColor(lineColor);
mLinePaint.setColor(LINE_COLOR);
mLinePaint.setStrokeWidth(thickness);
// mLinePaint.set
mLinePaint.setStyle(Paint.Style.STROKE);
mCirclePaint = new Paint();
if (endCircleColor != null) END_CIRCLE_COLOR = Color.parseColor(endCircleColor);
mCirclePaint.setColor(END_CIRCLE_COLOR);
mCirclePaint.setStrokeWidth(thickness);
this.startView = startView;
this.endView = endView;
this.direction = direction;
//setBackgroundColor To Transparent
super.setBackgroundColor(Color.TRANSPARENT);
}
public View getStartView() {
return startView;
}
public void setStartView(View startView) {
this.startView = startView;
}
public View getEndView() {
return endView;
}
public void setEndView(View endView) {
this.endView = endView;
}
public Canvas getCanvas() {
return canvas;
}
public int getDirection() {
return direction;
}
public void setDirection(int direction) {
this.direction = direction;
}
@Override
protected void onDraw(Canvas canvas) {
this.canvas = canvas;
Log.d("Direction", String.valueOf(direction));
Log.d("Start View Y:", String.valueOf(startView.getY()));
Log.d("Start View H:", String.valueOf(startView.getHeight()));
Log.d("End View Y:", String.valueOf(endView.getY()));
Log.d("End View H:", String.valueOf(endView.getHeight()));
//By default takes LEFT_TO_RIGHT
if (direction == RIGHT_TO_LEFT) {
//For RIGHT TO LEFT
//Calculating Left X And Mid Of Height Y
/*
* ______________
* | |
* This Point ==>> .| |
* | |
* |______________|
* */
float startViewLeftX = startView.getX();
float startViewMidHeightY = startView.getY() + startView.getHeight() / 2;
//Calculating Right X And Mid Of Height Y
/*
* ______________
* | |
* | |. <<== This Point
* | |
* |______________|
* */
float endViewRightX = endView.getX() + endView.getWidth(); //20 is just to remove unwanted padding on Right Side
float endViewMidHeightY = endView.getY() + endView.getHeight() / 2;
Path mPath = new Path();
mPath.moveTo(startViewLeftX, startViewMidHeightY);
mPath.lineTo(endViewRightX, endViewMidHeightY);
DashPathEffect dashPathEffect = new DashPathEffect(intervals, phase);
mLinePaint.setPathEffect(dashPathEffect);
canvas.drawPath(mPath, mLinePaint);
canvas.drawLine(startViewLeftX, startViewMidHeightY, endViewRightX, endViewMidHeightY, mLinePaint);
canvas.drawCircle(startViewLeftX, startViewMidHeightY, 5, mCirclePaint);
canvas.drawCircle(endViewRightX, endViewMidHeightY, 5, mCirclePaint);
} else {
//FOR LEFT_TO_RIGHT
//Calculating Right X And Mid Of Height Y
/*
* ______________
* | |
* | |. <<== This Point
* | |
* |______________|
* */
float startViewRightX = startView.getX() + startView.getWidth(); //20 is just to remove unwanted padding on Right Side
float startViewMidHeightY = startView.getY() + startView.getHeight()/2;
/* float startViewRightX = startView.getRight() ;
float startViewMidHeightY = startView.getBottom();*/
//Calculating Left X And Mid Of Height Y
/*
* ______________
* | |
* This Point ==>> .| |
* | |
* |______________|
* */
float endViewLeftX = endView.getX();
float endViewMidHeightY = endView.getY()+endView.getHeight()/2;
// float endViewMidHeightY = endView.getY();
Path mPath = new Path();
mPath.moveTo(startViewRightX, startViewMidHeightY);
mPath.lineTo(endViewLeftX, endViewMidHeightY);
Log.d("riughtx", String.valueOf(startViewRightX));
/*DashPathEffect dashPathEffect = new DashPathEffect(intervals, phase);
mLinePaint.setPathEffect(dashPathEffect);*/
Log.d("Direction", "Left to right");
// Log.d("Path", mPath.get);
canvas.drawPath(mPath, mLinePaint);
// canvas.rotate(45);
// canvas.drawRect(166, 748, 314, 890, mLinePaint);
canvas.drawLine(startViewRightX, startViewMidHeightY, endViewLeftX, endViewMidHeightY, mLinePaint);
canvas.drawCircle(startViewRightX, startViewMidHeightY, 5, mCirclePaint);
canvas.drawCircle(endViewLeftX, endViewMidHeightY, 5, mCirclePaint);
}
}
@Override
public void setBackgroundColor(int color) {
super.setBackgroundColor(color);
}
}
Depends on screen size I am able to draw the lines. by using scroll view, I am unable to draw the lines. Please check my updated question. Give me any solution.
来源:https://stackoverflow.com/questions/59698156/on-draw-method-is-not-calling-in-my-application-with-scrollview