I have a png image that contains a person, the area of the image that is not a person is transparent. I want to draw on top of that image, but only where it is not transparent. So, only on the person. For example let's say I have a blue rectangle and I place that rectangle on this legs then it should look like he has pants on since the only thing that would show up blue is his legs.
If in my fragment I have a MyImageView and set the source to a png image I have of a person. Then the code below will show that person image.
public class MyImageView extends ImageView {
private HashMap<String, InjuryOverlay> overlays = new HashMap<>();
public MyImageView(Context context) {
super(context);
}
public MyImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void addOverlay(String overlayName, MyOverlay overlay) {
overlays.put(overlayName, overlay);
}
public void removeAllOverlays() {
overlays.clear();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (MyOverlay overlay : overlays.values()){
overlay.onDraw(canvas);
}
}
}
If I add a MyOverlay with some points in the path it shows up, RED in my case, and it overlays nicely where I can see the human under it, but I don't want any overflow to be displayed... only the parts directly touching the person.
public class MyOverlay
{
private String name;
private ArrayList<Point> path;
private int color;
private Paint paint = new Paint();
private Path drawPath = new Path();
public MyOverlay() {
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
}
public MyOverlay(String name, ArrayList<Point> path, int color) {
this();
setName(name);
setPath(path);
setColor(color);
}
public void setName(String name) {
this.name = name;
}
public void setPath(ArrayList<Point> path) {
this.path = path;
}
public void setColor(int color) {
this.color = color;
paint.setColor(this.color);
}
public void onDraw(Canvas canvas) {
if (path != null && path.size() > 1)
{
drawPath.reset();
drawPath.moveTo(path.get(0).x, path.get(0).y);
for (int i = 1; i < path.size(); i++)
{
drawPath.lineTo(path.get(i).x, path.get(i).y);
}
// If the path doesn't end on the same point that we stated then force the path to end on the starting
// point. This is required since we are filling the path as a solid shape.
if (path.get(0).x != path.get(path.size()-1).x || path.get(0).y != path.get(path.size()-1).y)
{
drawPath.lineTo(path.get(0).x, path.get(0).y);
}
canvas.drawPath(drawPath, paint);
}
}
}
Any help would be appreciated.
Thanks