How to draw a filled triangle in android canvas?
You probably need to do something like :
Paint red = new Paint();
red.setColor(android.graphics.Color.RED);
red.setStyle(Paint.Style.FILL);
And use this color for your path, instead of your ARGB. Make sure the last point of your path ends on the first one, it makes sense also.
Tell me if it works please !
How to draw filled triangle on android Canvas
I've found the answer
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(android.graphics.Color.BLACK);
canvas.drawPaint(paint);
paint.setStrokeWidth(4);
paint.setColor(android.graphics.Color.RED);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setAntiAlias(true);
Point a = new Point(0, 0);
Point b = new Point(0, 100);
Point c = new Point(87, 50);
Path path = new Path();
path.setFillType(FillType.EVEN_ODD);
path.lineTo(b.x, b.y);
path.lineTo(c.x, c.y);
path.lineTo(a.x, a.y);
path.close();
canvas.drawPath(path, paint);
}
android canvas draw text in the triangle
You can try something like this
1) Measure the width of your text
Use measureText
2) From the point you are drawing calculate the width remaining to draw
3) Now depending on the use case you can curtail the length of text or scale the text as needed
int textWidthRequired = (int) drawPaint.measureText(textToDraw);
int widthRemainingToDraw = totalWidth/2 - textDrawX;
if(textWidthRequired > widthRemainingToDraw){
//handling
}
// draw text
tempCanvas.drawText(textToDraw,textDrawX, textDrawY, drawPaint);
How to draw a triangle, a star, a square or a heart on the canvas?
You have to find out the math behind that figures. The triangle and the star are quite easy to draw. Here is how you can draw a heart: http://www.mathematische-basteleien.de/heart.htm
To draw special paths you should create them by adding points, ellipses etc. The canvas supports a clipping mask of a specified path, so you can set the clipping mask of a heart, push the paths to the matrix, draw the content of the heart, and then pop it again.
That's what I'm doing to achieve a simulated 2D page curl effect on andriod: http://code.google.com/p/android-page-curl/
Hope this helps!
How to animate a filled triangle (drawn by a path) in Android Studio
Sorted! Just needed to add a path.reset() at the end of onDraw.
How can I draw a triangle with outline in canvas in Android application?
public void drawTriangle(int coordX, int coordY, int sideLen, String fillColor, int strokeWidth, String strokeColor) {
this.marginSTART = coordX;
this.marginTOP = coordY;
this.fillColor = fillColor;
this.strokeColor = strokeColor;
this.strokeWidth = strokeWidth;
//Съотношение:
double suotnoshenie = 0.857;
this.getLayoutParams().width = sideLen;
this.getLayoutParams().height = (int) Math.floor(sideLen*suotnoshenie);
this.setX(this.marginSTART);
this.setY(this.marginTOP);
//Първи тръигълник, който ще се запълни (реална големина)
Paint paint = new Paint();
Bitmap bitmap = Bitmap.createBitmap(sideLen, (int) Math.floor(sideLen*suotnoshenie), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
/*paint.setColor(Color.parseColor("#ff0000")); //testing
canvas.drawPaint(paint);*/
paint.setStrokeWidth(0);
paint.setColor(Color.parseColor(fillColor));
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
Path path = new Path();
path.setLastPoint(0, (float) (sideLen*suotnoshenie)); // A
path.lineTo(sideLen, (float) (sideLen*suotnoshenie)); // B
path.lineTo((float)(sideLen/2), 0); // C
path.lineTo(0, (float) (sideLen*suotnoshenie)); // A
path.close();
canvas.drawPath(path, paint);
//Втори тръигълник, който няма да се запълва, а само ще stroke-ва
// (линиите, който го очертават трябва да влизат вътре в големия до половината stroke, понеже самият stroke минава от двете страни на линиите...)
Paint paint2 = new Paint();
paint2.setStrokeWidth(strokeWidth);
paint2.setColor(Color.parseColor(strokeColor));
paint2.setStyle(Paint.Style.STROKE);
paint2.setAntiAlias(true);
Path path2 = new Path();
path2.setLastPoint((float) (0+(strokeWidth*1.75)/2), (float) ((sideLen*suotnoshenie)-strokeWidth/2)); // A
path2.lineTo((float) (sideLen-(strokeWidth*1.75)/2), (float) ((sideLen*suotnoshenie)-strokeWidth/2)); // B
path2.lineTo((float) (sideLen/2), (float) (0+strokeWidth)); // C
path2.lineTo((float) (0+(strokeWidth*1.75)/2), (float) ((sideLen*suotnoshenie)-strokeWidth/2)); // A
path2.close();
canvas.drawPath(path2, paint2);
BitmapDrawable bmpDrawable = new BitmapDrawable(getResources(), bitmap);
this.setBackground(bmpDrawable);
}
Android How to draw triangles inside a hexagon?
I assume that you have lines with thickness 2d
To avoid overwriting these lines, you can shift the central point for every triangle by dstance d/sin(30) = 2*d
in direction of bisector between two lines
Perhaps also you need to diminish radius (length of triangle side along the line)
newradius = radius - d*sqrt(3) - d * sqrt(3)/3
for (int i = 1; i <= 6; i++) {
cx = x + 2 * d * Math.cos(section * (i + 0.5));
cy = x + 2 * d * Math.sin(section * (i + 0.5));
float x1 = (float) (cx + newradius * Math.cos(section * i)); /
//and similar for other coordinates
trianglesPath.moveTo(cx, cy);
trianglesPath.lineTo(x1, y1);
...
How to draw triangle shape and add it into relative or linear layout android
Code for Custom Linear Layout (I modified your code so its easier for you to understand)
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.LinearLayout;
/**
* @author Atrix1987
*
*/
public class CustomView extends LinearLayout {
/**
* @param context
*/
public CustomView(Context context) {
super(context);
commonConstructor(context);
}
/**
* @param context
* @param attrs
*/
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
commonConstructor(context);
}
/**
*
*/
Paint trianglePaint;
/**
*
*/
Path trianglePath;
/**
* @param context
*/
private void commonConstructor(Context context) {
trianglePaint = new Paint();
trianglePaint.setStyle(Style.FILL);
trianglePaint.setColor(Color.RED);
Point point = new Point();
point.x = 80;
point.y = 80;
trianglePath = getEquilateralTriangle(point, 70, Direction.SOUTH);
}
@Override
protected void onDraw(Canvas canvas) {
Log.i("Sample", "inside ondraw");
//avoid creating objects in onDraw
canvas.drawPath(trianglePath, trianglePaint);
}
private Path getEquilateralTriangle(Point p1, int width, Direction direction) {
Log.i("Sample", "inside getEqui");
Point p2 = null, p3 = null;
if (direction == Direction.NORTH) {
p2 = new Point(p1.x + width, p1.y);
p3 = new Point(p1.x + (width / 2), p1.y - width);
} else if (direction == Direction.SOUTH) {
p2 = new Point(p1.x + width, p1.y);
p3 = new Point(p1.x + (width / 2), p1.y + width);
} else if (direction == Direction.EAST) {
p2 = new Point(p1.x, p1.y + width);
p3 = new Point(p1.x - width, p1.y + (width / 2));
} else if (direction == Direction.WEST) {
p2 = new Point(p1.x, p1.y + width);
p3 = new Point(p1.x + width, p1.y + (width / 2));
}
Path path = new Path();
path.moveTo(p1.x, p1.y);
path.lineTo(p2.x, p2.y);
path.lineTo(p3.x, p3.y);
return path;
}
public enum Direction {
NORTH, SOUTH, EAST, WEST;
}
}
The code for the activity (For simplicity and as a shortcut i did this, you can also specify it in the xml and just setContentView):
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
/**
* @author Atrix1987
*
*/
public class SampleActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CustomView cv = new CustomView(getApplicationContext());
cv.setBackgroundColor(Color.WHITE);
setContentView(cv, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
}
}
Do explore the developer site links for custom views, for more insight.
Hope this helps.
PFB the screenshot
Related Topics
Onactivityresult() Not Called in New Nested Fragment API
How to Send Data from Dialogfragment to a Fragment
How to Catch a Firebase Auth Specific Exceptions
How to Specify the Jdk Version in Android Studio
Failed to Import New Gradle Project: Failed to Find Build Tools Revision *.0.0
How to Sort Alphabetically While Ignoring Case Sensitive
Android Days Between Two Dates
Passing a JavaScript Object Using Addjavascriptinterface() on Android
Android: How to Disable List Items on List Creation
Issue When Using a Custom Font - "Native Typeface Cannot Be Made"
Can't Get Download Url from Firebase Storge in Android
Why Is Accessing Textview of a Fragment Inside Activity Throwing an Error
Dynamic Listview in Android App
How to Get Timezone from Android Mobile
Collections Sort(List<T>,Comparator<? Super T>) Method Example