William Seemann
William Seemann

Reputation: 3530

Android, creating custom ImageView shape

I need to create a view that is filled with a dynamic color at runtime. I've accomplished this task however the actual view needs to have a "peak" on the right side. Typically ImageView's are only square or rectangular. Does anyone know how I would accomplish adding a triangular peak to the view?

For example:

enter image description here

Upvotes: 0

Views: 866

Answers (1)

muratgu
muratgu

Reputation: 7311

You can override the onDraw event of the ImageView and do whatever you need with the canvas object.

    public class MyImageView extends ImageView {
        ...
        @Override
        public void onDraw(Canvas canvas){
            super.onDraw(canvas);
            Point[] point = ... //create three points here for the triangle
            Paint paint = ... // create your desired color
            drawTriangle(canvas, point, paint);
    }

And for the triagle draw (see this SO answer):

private void drawTriangle(Canvas canvas, Point[] point,  Paint paint) {

    float [] points  = new float[8];             
    points[0] = point[0].x;      
    points[1] = point[0].y;      
    points[2] = point[1].x;      
    points[3] = point[1].y;         
    points[4] = point[2].x;      
    points[5] = point[2].y;              
    points[6] = point[0].x;      
    points[7] = point[0].y;

    canvas.drawVertices(VertexMode.TRIANGLES, 8, points, 0, null, 0, null, 0, null, 0, 0, paint);
    Path path = new Path();
    path.moveTo(point[0].x , point[0].y);
    path.lineTo(point[1].x,point[1].y);
    path.lineTo(point[2].x,point[2].y);
    canvas.drawPath(path,paint);    
}

Upvotes: 1

Related Questions