2013-07-26 163 views
0

我试图达到使用自定义形状(几乎矩形)创建视图的效果。创建自定义视图

这里是我试图做的:

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); 
} 

} 

和XML:

<CustomHeaderview 
      android:layout_width="152dp" 
      android:layout_height="152dp" /> 

编辑 由于梅德,它完美的作品吧!

回答

0

您提供看中坐标为您矩形:

x1: 100 
y1: 100 
x2: 100 
y2: 120 

所以,你要矩形0宽度,这就是为什么它是无形的。

+0

我明白了,你可以给我一个关于如何使用画布绘制梯形形状的想法吗? –

+0

@AdrianOlar你可以一行一行地绘制它,使用'canvas.skew()'方法或'canvas.drawVertices()' –

+0

感谢德米特里,我使用了逐行方法。我更新了我的答案 –