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" />
編輯 由於梅德,它完美的作品吧!
我明白了,你可以給我一個關於如何使用畫布繪製梯形形狀的想法嗎? –
@AdrianOlar你可以一行一行地繪製它,使用'canvas.skew()'方法或'canvas.drawVertices()' –
感謝德米特里,我使用了逐行方法。我更新了我的答案 –