使用 xml android 在雅虎文摘新闻应用程序中绘制倾斜矩形上边缘形状
drawing sloped rectangle upper edge shape as in yahoo digest news app using xml android
我正在尝试绘制倾斜的矩形上边缘形状并将其用作图片中的线性布局背景:
这里是 link 我试图实现的设计:http://postimg.org/image/krjwfcisz/
我试着画了一个矩形并旋转它,但效果不佳。
那么如何创建那个形状。
抱歉语法错误,我不流利:P.
好吧,我认为回答我的问题是一件愚蠢的事情,但我这样做是因为它可能会帮助其他人。
我无法使用 XML 创建我需要的内容。
下面是如何使用 onDraw 方法创建倾斜矩形。
首先你需要创建一个 class 扩展 View
class 然后
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);
}
}
然后您可以在您的布局中使用此视图 class 作为自定义视图,如下所示:
<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
我正在尝试绘制倾斜的矩形上边缘形状并将其用作图片中的线性布局背景:
这里是 link 我试图实现的设计:http://postimg.org/image/krjwfcisz/
我试着画了一个矩形并旋转它,但效果不佳。
那么如何创建那个形状。
抱歉语法错误,我不流利:P.
好吧,我认为回答我的问题是一件愚蠢的事情,但我这样做是因为它可能会帮助其他人。
我无法使用 XML 创建我需要的内容。
下面是如何使用 onDraw 方法创建倾斜矩形。
首先你需要创建一个 class 扩展 View
class 然后
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);
}
}
然后您可以在您的布局中使用此视图 class 作为自定义视图,如下所示:
<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