我试着画一个倾斜的矩形上边缘形状,并使用它作为线性布局的背景,如图所示:
这里是链接到设计我试图实现:http://postimg.org/image/krjwfcisz/
我试着画一个矩形并旋转它,但是它不能正常工作。
那么如何创建这个形状呢
对不起,语法错误,我不流利:p
我认为回答我的问题是一件愚蠢的事情,但我这样做是因为它可能会帮助其他人。
我无法使用XML创建我需要的东西。
下面是如何使用onDraw方法创建一个倾斜矩形。
首先你需要创建一个类扩展View
类然后
public class SharpRectView extends View {
public SharpRectView(Context context) {
super(context);
}
public SharpRectView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SharpRectView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
float width = getWidth();
float height = getHeight();
Path path = new Path();
Paint paint = new Paint();
paint.setColor(getResources().getColor(R.color.text_white));
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
path.moveTo(0,0);
path.lineTo(width, 0.26f * width);
path.lineTo(width, height);
path.lineTo(0, height);
path.lineTo(0, 0);
path.close();
canvas.drawPath(path,paint);
}
}
,那么你可以在你的布局中使用这个视图类作为自定义视图,如下所示:
<com.example.SharpRectView
android:id="@+id/sharp_rect"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_marginTop="-120dp"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
/>
和给信用的家伙,我用他的项目找到解决方案,请访问:https://github.com/qianlvable/ParallaxEffectDemo他的用户名是:qianlv