[英]How to make a drawable rectangle with two colors in xml in android?
I want to make something like this by using xml in android. 我想通过在android中使用xml来制作这样的东西。 I achieved something like it using gradient with angle 45 degree but I don't want gradient but plain color like this.
我用渐变角度45度实现了类似的东西,但我不想要渐变而是像这样的纯色。 Any suggestion is greatly appreciated.
任何建议都非常感谢。 Thanks in Advance.
提前致谢。
This is what I want to make using xml. 这是我想用xml制作的。
I need many like this so I can not load bitmaps in drawable folder.] 1 我需要很多这样的,所以我无法在drawable文件夹中加载位图。] 1
您可以使用Android PathShape类绘制所需颜色的两个三角形
The below answer is shamelessly copied from here How to make gradient background in android 以下答案是从这里无耻地复制如何在android中制作渐变背景
Try with this : 试试这个:
<gradient
android:angle="90" //you need to change angle as per your needs (270 might work in your case)
android:centerColor="#555994" //try playing with colors of start, center and End colors to get desired result. also try removing some.
android:endColor="#b5b6d2"
android:startColor="#555994"
android:type="linear" />
<corners
android:radius="0dp"/>
To use above code you need to make a drawable .xml file, copy & paste the above code into this file. 要使用上面的代码,您需要制作一个可绘制的.xml文件,将上述代码复制并粘贴到此文件中。
Thanks. 谢谢。
I do not know if it is possible in XML. 我不知道XML是否可行。 In Java, one possible way is to create a Shape and build a ShapeDrawable with it.
在Java中,一种可能的方法是创建一个Shape并使用它构建一个ShapeDrawable。
TwoTrianglesDrawable.java TwoTrianglesDrawable.java
public class TwoTrianglesDrawable extends ShapeDrawable {
public TwoTrianglesDrawable(){
super();
setShape(new TwoTrianglesShape());
}
private class TwoTrianglesShape extends Shape {
@Override
public void draw(Canvas canvas, Paint paint) {
Path path = new Path();
path.setFillType(Path.FillType.INVERSE_EVEN_ODD);
Path path1 = new Path();
path1.setFillType(Path.FillType.INVERSE_EVEN_ODD);
paint.setStrokeWidth(0);
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
paint.setColor(Color.YELLOW);
Point a = new Point(0, 0);
Point b = new Point(0, (int) getHeight());
Point c = new Point((int)getWidth(), (int)getHeight());
path.moveTo(a.x, a.y);
path.lineTo(b.x, b.y);
path.lineTo(c.x, c.y);
path.close();
canvas.drawPath(path, paint);
paint.setColor(Color.BLUE);
Point a1 = new Point(0, 0);
Point b1 = new Point((int)getWidth(),0);
Point c1 = new Point((int)getWidth(), (int)getHeight());
path1.moveTo(a1.x, a1.y);
path1.lineTo(b1.x, b1.y);
path1.lineTo(c1.x, c1.y);
path1.close();
canvas.drawPath(path1, paint);
}
}
}
Using, for example, as a RelativeLayout background: 例如,使用RelativeLayout背景:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout);
ShapeDrawable background = new TwoTrianglesDrawable();
layout.setBackground(background);//Requires API 16 or higher.
This will give you two colors half and half vertically. 这将为您提供两种颜色垂直的一半和一半。 Put this code in a
drawable
resource. 将此代码放在
drawable
资源中。
<item
android:top="320dip">
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="@color/red" />
</shape>
</item>
<item android:bottom="320dip">
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="@color/yellow" />
</shape>
</item>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.