this is how am getting circularImageView on api 23
public class CustomImageViewCircularShape extends ImageView {
public static float radius = 100.0f;
public CustomImageViewCircularShape(Context context) {
super(context);
}
public CustomImageViewCircularShape(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomImageViewCircularShape(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onDraw(Canvas canvas) {
Path clipPath = new Path();
RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight());
clipPath.addRoundRect(rect, radius, radius, Path.Direction.CW);
canvas.clipPath(clipPath);
super.onDraw(canvas);
}
}
but its not working on api 16 also android Studio is not giving any warning related to Backward Support ? any idea why its not working or how i can make it work on the older Apis ?
One alternative way would be using Fresco library from Facebook. Among all its features, you can use circular images.
Here you have more information.
http://frescolib.org/docs/rounded-corners-and-circles.html
For an approach like yours, try this one:
https://gist.github.com/melanke/7158342
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.
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.
I am trying to draw a Rectangle but with INVERSE_WINDING. This works as expected in the case of LAYER_TYPE_SOFTWARE, but weirdly in case of Hardware Acceleration. The below are the results:
CODE:
public class PathView extends View {
private final Paint mPaint;
public PathView(Context context) {
this(context, null, 0);
}
public PathView(Context context, #Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public PathView(Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// setLayerType(LAYER_TYPE_SOFTWARE, null); // Toggle this!!!
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(Color.RED);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
final Path path = new Path();
path.addRect(50,50,400,400, Path.Direction.CCW); // CCW or CW, same result
path.close(); // same result even if I didn't close the path
path.setFillType(Path.FillType.INVERSE_WINDING);
canvas.drawPath(path, mPaint);
}
}
Why is Android behaving this way? I read the documentation on What can't be done in Hardware Acceleration, but it never mentioned anything on Path.FillType.
How do I get the same behavior in Hardware Acceleration as well?
I am working with android.I had created an android app with a custom image view which clip corners of the image. here is my custom image view
public class CustomImage extends ImageView {
public static float radius = 18.0f;
public CustomImage(Context context) {
super(context);
}
public CustomImage(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomImage(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onDraw(Canvas canvas) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB) {
CustomImage.this.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
Path clipPath = new Path();
RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight());
clipPath.addRoundRect(rect, radius, radius, Path.Direction.CW);
canvas.clipPath(clipPath);
super.onDraw(canvas);
}
}
it works perfect.But I need to add a border around the clipped Image view .How can I do this?
If you want rounded conners you can use this library RoundedImageView. You can see the code of library for you can implementation.
Or This tutorial Round Corners
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.