CustomTextView canvas not drawing - android

Drawable not drawing in custom TextView. Can anyone please help me in sorting the problem out. What i was trying to build is a texview that shows progress. The below mention code is just a sample.
public class ProgressableTextview extends TextView {
Paint mPaint;
int mProgress = 70;
int maxProgress = 100;
int mRadiud = 10;
public ProgressableTextview(Context context) {
super(context);
init();
}
public ProgressableTextview(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ProgressableTextview(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public void init(){
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(Color.BLACK);
mPaint.setStyle(Paint.Style.FILL);
}
#Override
public void onDraw(Canvas canvas) {
canvas.drawColor(Color.GRAY);
canvas.drawRect(0, 0, (mProgress/maxProgress)*getWidth(), getMeasuredHeight(), mPaint);
canvas.save();
canvas.translate(getLeft(), 0);
super.onDraw(canvas);
canvas.restore();
}
}

Ok i figured out what was the problem, it was the mistake in drawRect function. drawRect function need to changed as :
canvas.drawRect(0, 0, ((float) mProgress/maxProgress) * getWidth(), getMeasureHeight(), mPaint);
since mProgress was a integer dividing it by integer returns zero and that was the problem.

Related

Draw rounded corner line using canvas

I am trying to draw rounded corner line using canvas. Like eg:
public class MyView extends View {
Paint paint;
Path path;
public MyView(Context context) {
super(context);
init();
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
paint = new Paint();
paint.setColor(Color.BLUE);
paint.setStrokeWidth(10);
paint.setStyle(Paint.Style.STROKE);
path = new Path();
path.moveTo(50, 50);
path.lineTo(50, 500);
path.lineTo(200, 500);
path.lineTo(200, 300);
path.lineTo(350, 300);
float radius = 50.0f;
CornerPathEffect cornerPathEffect =
new CornerPathEffect(radius);
paint.setPathEffect(cornerPathEffect);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(path, paint);
}
}
I am able to draw corner line using paint as shown above, but not from the canvas.
I tried canvas.drawLine() and canvas.drawArc() to achieve the same result but failed to do that.
Can somebody help me to solve the issue?

how to draw rectangle canvas?

In this example code I give a nice square in the form I want. canvas.drawRect (100, 300, 600, 800, paint); values work. But what I want is to call these values from the Activity class. So I want to send these values to the Draw class in the activity class. How can I do that ? For example, I want to send an activity class as drawRect (100,100,100,100, Color.BLUE). I do not want to write these values in the Draw class.
Draw.java
public class Draw extends View {
Paint paint;
Path path;
public Draw(Context context) {
super(context);
init();
}
public Draw(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public Draw(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public void init(){
paint = new Paint();
paint.setColor(Color.BLACK);
paint.setStrokeWidth(10);
paint.setStyle(Paint.Style.STROKE);
}
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawRect(100, 300, 600, 800, paint);
}
}
Activity.java
constraintLayout=findViewById(R.id.constraint);
Draw draw = new Draw(this);
constraintLayout.addView(draw);
You need to make method and pass value from activity to draw class:-
Draw draw = new Draw(this,100, 300, 600, 800);
constraintLayout.addView(draw);
Draw class
public class Draw extends View {
Paint paint;
Path path;
float left;
float top;
float right;
float bottom;
public Draw(Context context,float left, float top, float right, float bottom) {
super(context);
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
init();
}
public Draw(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public Draw(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public void init(){
paint = new Paint();
paint.setColor(Color.BLACK);
paint.setStrokeWidth(10);
paint.setStyle(Paint.Style.STROKE);
}
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawRect(left, top, right, bottom, paint);
}
}
You can create local variables for the bounds and set them using setters or init function before adding that view to a viewgroup.( constraintLayout.addView(draw) in your case

How to use new version of getresources().getColor?

I am trying to get a color element using getresources.getColor(resource id) but android is telling me that it has been deprecated use getresources.getColor(resource id, theme).
How do I tell it what theme to use? I have tried R.style.AppTheme but I get an error as this is an int value
public class TodoListItemView extends AppCompatTextView {
public TodoListItemView(Context context, AttributeSet attributeSet, int ds) {
super(context, attributeSet, ds);
init();
}
public TodoListItemView(Context context) {
super(context);
init();
}
public TodoListItemView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
init();
}
private Paint marginPaint;
private Paint linePaint;
private int paperColor;
private float margin;
private void init() {
Resources myResources = getResources();
marginPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
marginPaint.setColor(getResources().getColor(R.color.notepad_margin));
linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
linePaint.setColor(myResources.getColor(R.color.notepad_lines));
paperColor = myResources.getColor(R.color.notepad_paper);
margin = myResources.getDimension(R.dimen.notepad_margin);
}
#Override
public void onDraw(Canvas canvas) {
canvas.drawColor(paperColor);
canvas.drawLine(0, 0, getMeasuredHeight(), 0, linePaint);
canvas.drawLine(0, getMeasuredHeight(), getMeasuredWidth(), getMeasuredHeight(), linePaint);
canvas.drawLine(margin, 0, margin, getMeasuredHeight(), marginPaint);
canvas.save();
canvas.translate(margin, 0);
super.onDraw(canvas);
canvas.restore();
}
}
Any help is greatly appreciated.
Use ContextCompat.getColor(context, R.color.your_color);
Thanks for all the input. ContextCompat.getColor worked as far as fixing the errors. The interface, enum errors were due to an extraneous closing bracket.
My only complaint is that now the app does not run. A message appears on the emulator stating the the app has stopped working. No chance to test the changes.

EditText's onDraw canvas gets shifted to the left when single line is set

I have a very simple customized EditText like this:
public class MyEditText extends EditText {
private Paint mPaint;
public MyEditText(Context context) {
super(context);
init();
}
public MyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
setSingleLine();
mPaint = new Paint();
mPaint.setColor(Color.RED);
mPaint.setStrokeWidth(6);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int y = canvas.getHeight() * 6 / 7;
canvas.drawLine(0, y, canvas.getWidth(), y, mPaint);
}
}
As I set single line mode on, when text is short everything is fine, it would look like this:
But once the text gets longer (more than width of the EditText), the red line is shifted to the left like this:
As you'll probably notice I used 0 and canvas.getWidth() to connect both sides of the view, but the line gets shifted, so doesn't it mean the whole canvas gets shifted as well?
If so it's such a strange behavior that I didn't expect from a View descendant. Can someone shed me some light on this strange behavior?
Thanks
Change your onDraw() method like below...
#Override
protected void onDraw(Canvas canvas) {
Rect r = new Rect();
int baseline = getLineBounds(0, r);
int y = canvas.getHeight() * 6 / 7;
canvas.drawLine(r.left, y, r.right, y, mPaint);
super.onDraw(canvas);
}

How do you overline numbers in android xml?

I am developing a maths app in which i have to use numbers with bar on top. check this link http://en.wikipedia.org/wiki/Overline
Also how to use Exponentiation like in the following link http://en.wikipedia.org/wiki/Exponentiation in xml file.
This is not the perfect solution but it will give you the idea for text overline.
public class OverLineTextView extends TextView {
private Paint paint;
public OverLineTextView(Context context) {
super(context);
init();
}
public OverLineTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public OverLineTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.GREEN);
paint.setStyle(Style.STROKE);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float width = getPaint().measureText(getText().toString());
canvas.drawLine(getTotalPaddingLeft(), getTotalPaddingTop() + 1,
getTotalPaddingLeft() + width, getTotalPaddingTop() + 1, paint);
}
}

Categories

Resources