How to strike through obliquely on TextView - android

Is there any simple way how to draw obliquely strike through on TextView? Now I'm using this code:
textview.setPaintFlags(textview.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
But I need something like this:
I'm not very familiar with Paint API how to simply achive this so any help will be appreciated. Thank you.

It's pretty easy with a custom TextView class. Check out the Canvas guide from the documentation for more info on this. Note the comments where you can change the color/width:
public class ObliqueStrikeTextView extends TextView
{
private int dividerColor;
private Paint paint;
public ObliqueStrikeTextView(Context context)
{
super(context);
init(context);
}
public ObliqueStrikeTextView(Context context, AttributeSet attrs)
{
super(context, attrs);
init(context);
}
public ObliqueStrikeTextView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
init(context);
}
private void init(Context context)
{
Resources resources = context.getResources();
//replace with your color
dividerColor = resources.getColor(R.color.black);
paint = new Paint();
paint.setColor(dividerColor);
//replace with your desired width
paint.setStrokeWidth(resources.getDimension(R.dimen.vertical_divider_width));
}
#Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.drawLine(0, getHeight(), getWidth(), 0, paint);
}
}
You can use it in a layout file by fully qualifying the view with your package, like this:
<your.package.name.ObliqueStrikeTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1234567890"
android:textSize="20sp"/>
Here's the end result:

Related

drawing line with arrow between 2 buttons

I have 12 lines I've created using the following class
public class LineView extends View {
private Paint paint = new Paint();
private PointF pointA,pointB;
// private void init() {
// paint.setColor(Color.BLACK);
// }
public LineView(Context context) {
super(context);
// init();
}
public LineView(Context context, AttributeSet attrs) {
super(context, attrs);
// init();
}
public LineView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// init();
}
#SuppressLint("ResourceAsColor")
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
int color = R.color.GradientStart;
paint.setColor(color);
paint.setColor(Color.BLUE);
paint.setStrokeWidth(5);
//canvas.drawLine(x1, y1, x2, y2, paint);
canvas.drawLine(pointA.x, pointA.y, pointB.x, pointB.y, paint);
}
public void setPointA(PointF point){
pointA=point;
}
public void setPointB(PointF point){
pointB=point;
}
public void draw(){
invalidate();
requestLayout();
}
}
Instead of lines I what lines with arrows. The line with arrow will be drawn between buttons.
How can I add arrows to one end of my lines?
It would like like this when complete.
thanks
JN
I found two way for achieve your requirement.
1) To use nine patch image. I have tried to make nine patch image for you please use it
2) You can use vector image for it.
Tell me still you not getting solution.

Android - Background transparency sets also transparency on Paint object

Im creating a custom view (extending Linearlayout) which has as background color: #B3000000 (75% transparency = B3), and now I need to draw something on this view with full color and not with B3/75% transparency.
What I currently have:
public class MyCustomObject extends LinearLayout {
private Paint paint;
public MyCustomObject(Context context) {
super(context);
init();
}
public MyCustomObject(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyCustomObject(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public MyCustomObject(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
private void init() {
View v = View.inflate(getContext(), R.layout.custom_view, this);
paint = new Paint();
paint.setColor(Color.RED);
paint.setAlpha(255);
paint.setStrokeWidth(50);
setWillNotDraw(false);
}
#Override
protected void onDraw(Canvas canvas) {
// super.onDraw(canvas);
canvas.drawLine(0,0,getWidth(),getHeight(),paint);
Timber.e("Drawing...");
}
}
I would like to draw a RED line on top of this view, and at this moment it draw a red line but with a "black layer" on top, instead a vibrante RED color.
I found the solution here
#Override
protected void dispatchDraw(Canvas canvas) {
// draw here if you want to draw behind children
super.dispatchDraw(canvas);
// draw here if you want to draw over children
}

Android onDraw never called

I have a custom linear layout from which I extend. I have already called setWIllNotCacheDrawing but it wont work. Here is my Class:
public class ClippedLinearLayout extends LinearLayout {
public ClippedLinearLayout(Context context) {
super(context);
this.setWillNotCacheDrawing(false);
}
public ClippedLinearLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.setWillNotCacheDrawing(false);
}
public ClippedLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
this.setWillNotCacheDrawing(false);
}
#Override
protected void onDraw(Canvas canvas) {
Log.e("clipped?", "clipped?);
Path mPath = new Path();
mPath.addCircle(50, 50, 50, Path.Direction.CCW);
canvas.clipPath(mPath, Region.Op.INTERSECT);
super.onDraw(canvas);
}
}
You're extending a ViewGroup, and sometimes by default they won't draw. Have you tried calling View.setWillNotDraw(false) in your constructor?

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);
}
}

Create Custom View

I am trying to achieve the effect of creating a view with a custom shape(almost rectangular).
Here is what i tried to do:
public class CustomHeaderview extends View {
public CustomHeaderview(Context context) {
super(context);
}
public CustomHeaderview(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomHeaderview(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
Paint paint = new Paint();
#Override
public void draw(Canvas canvas) {
paint.setColor(Color.GRAY);
paint.setStyle(Style.FILL);
Path wallpath = new Path();
wallpath.reset(); // only needed when reusing this path for a new build
wallpath.moveTo(100, 100); // used for first point
wallpath.lineTo(100, 200);
wallpath.lineTo(200, 200);
wallpath.lineTo(150, 100);
wallpath.lineTo(100, 100);// there is a setLastPoint action but i found it not to work as expected
canvas.drawPath(wallpath, paint);
super.draw(canvas);
}
}
and the XML:
<CustomHeaderview
android:layout_width="152dp"
android:layout_height="152dp" />
EDIT
Thanks Dmitry, it works perfectly now!
You providing fancy coordinates for you rectangle:
x1: 100
y1: 100
x2: 100
y2: 120
So, you're getting rectangle with 0 width, thats why it is invisible.

Categories

Resources