Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] java – Drop shadow for arcs in canvas

Posted on November 11, 2022 By

Solution 1 :

One approach is you could create your entire shape as a vector drawable. That way all you would need to define is the vector drawable and the shadow, this topic is discussed here

Create Vector Drawable with Drop Shadow to Overlay an Image

Problem :

I’m trying to develop a small custom view for my personal project. I’m facing a small trouble as to how to provide elevation effect or drop shadow effect for the view that I created.

It is a rectangle with concave semi circles on the left and right edge. I’m able to provide shadow for the whole rectangle but not for the concave semi circles and this is my problem.

This is the image of what I currently have and if we look at the left and right semi circles, we can see that they do not have elevation.

Below is the code for same –

private void init(AttributeSet set) {

    rect = new RectF();

    this.setLayerType(LAYER_TYPE_SOFTWARE, paint);

    paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setShadowLayer(8f, 4f, 4f, Color.rgb(197, 197, 197));

    eraser = new Paint(Paint.ANTI_ALIAS_FLAG);
    eraser.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

    dashLine = new Paint(Paint.ANTI_ALIAS_FLAG);
    dashLine.setColor(Color.BLACK);
    dashLine.setStyle(Paint.Style.STROKE);
    dashLine.setStrokeWidth(1);
    dashLine.setPathEffect(new DashPathEffect(new float[]{10, 10}, 0));

    path = new Path();
}

@Override
protected void onDraw(Canvas canvas) {

    rect.left = RECT_PADDING;
    rect.top = RECT_PADDING;
    rect.right = getWidth() - RECT_PADDING;
    rect.bottom = getHeight() - RECT_PADDING;

    paint.setColor(Color.WHITE);

    //Canvas draws rectangle
    canvas.drawRoundRect(rect, 20, 20, paint);

    //Canvas draws scallop on left and right edges of rectangle
    canvas.drawCircle(RECT_PADDING, rect.bottom / 2, 20, eraser);
    canvas.drawCircle(rect.right, rect.bottom / 2, 20, eraser);

    //Dashed line between both the scallops
    path.moveTo(RECT_PADDING + 40, rect.bottom / 2);
    path.lineTo(rect.right - 40, rect.bottom / 2);
    canvas.drawPath(path, dashLine);


}

I know this happens because eraser.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); clears everything that comes in its area which includes a part of the rectangle and its shadow.

READ  [FIXED] android - What is the difference between LiveData() and LiveData()
Powered by Inline Related Posts

How to resolve this issue or is there any alternative?

Thanks in advance.

Android Tags:android, android-canvas, android-custom-view, android-view, java

Post navigation

Previous Post: [FIXED] Problem with the implementation of the Android Studio Java design libraries
Next Post: [FIXED] android – Kotlin: Typeconverter for other class type

Related Posts

[FIXED] c# – Transitioning from the Obsolete FragementManager to Android.Support.V4.App Android
[FIXED] Android kotlin passing dynamic arguments/parameters to ViewModel with ViewModelFactory Android
[FIXED] xamarin.android – Adding a custom view to a page not working in PDFTron.Android for Xamarin Android
[FIXED] java – How to change the background color of a view from a different activity in android? Android
[FIXED] android – What’s the difference between Kapt and Implementation in Gradle when declaring dependecies using them? Android
[FIXED] Android Studio: Unable to locate ADB Android

Archives

  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can Vicks humidifier be used without filter?
  • What color is Spanish green?
  • How old is Jamie in The War That Saved My Life?
  • When should I use scalp massager for hair growth?
  • Can I put polyurethane over liming wax?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme