Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animated Dashed Border in Android

How do you create an animated dashed or dotted border of an arbitrary shape in Android? In XML (preferred) or programmatically.

See picture below for an example.

alt text

like image 473
hpique Avatar asked Aug 25 '10 07:08

hpique


People also ask

How do you make a dotted border on Android?

There's no “native” way to give a layout a dotted border on Android. To do that, you have to create a drawable representing your border and attach it to your layout.

How do I make a dotted dashed line in Android?

This example demonstrates how do I make a dotted/dashed line in Android. Step 1 - Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 - Add the following code to res/layout/activity_main. xml.


1 Answers

Have you seen the PathEffects API demo? http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/PathEffects.html

It produces precisely an animated line and you can just adjust the path to the edge of your view to create a border. For example:

Define a path by your view parameters / arbitrary shape:

Path path = new Path();
path.addRect(view.getLeft(), view.getTop(), view.getRight(), view.getBottom(), Path.Direction.CW);

Then create a dashed PathEffect with:

PathEffect pe = new DashPathEffect(new float[] {10, 5, 5, 5}, phase);

Then set the associate it with a Paint object and draw:

mPaint.setPathEffect(pe);
canvas.drawPath(path, mPaint);

EDIT: The animated effect comes from continuously changing the phase and redrawing. In the API demo it calls invalidate() in the onDraw() method (which triggers onDraw()...)

like image 111
antonyt Avatar answered Sep 22 '22 13:09

antonyt