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);
}
}
Related
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.
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
}
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?
I am trying to make a simple custom view which is not working. I refereed the custom view class from layout resource file. But its not working. Can anybody tell me where the problem is.
public class WriteOnScreenActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
public class CustomView extends View {
private Paint paint;
public TouchEventView(Context context) {
super(context);
paint = new Paint();
paint.setColor(Color.RED);
paint.setTextSize(25);
paint.setAntiAlias(true);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawText("Hello World", 5, 30, paint);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.touch.CustomView
android:id="#+id/TouchEventView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
Move the definition of your class TouchEventView into a new file.
And here's the fix.
public class TouchEventView extends View {
private Paint paint;
public TouchEventView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setup(context, attrs, defStyle);
}
public TouchEventView(Context context, AttributeSet attrs) {
super(context, attrs);
setup(context, attrs, 0);
}
public TouchEventView(Context context) {
super(context);
setup(context, null, 0);
}
private void setup(Context context, AttributeSet attrs, int defStyle)
{
paint = new Paint();
paint.setColor(Color.RED);
paint.setTextSize(25);
paint.setAntiAlias(true);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawText("Hello World", 5, 30, paint);
}
}
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: