How can we create ballon drawable shape as below. where we can change the color of it dynamically.
如何创建如下所示的气球拉延形状。我们可以动态改变颜色。
7 个解决方案
#1
55
Here it is XML
for triangle
and rectangle
. save it inside drawable folder.
这里是三角形和矩形的XML。将其保存在可伸缩的文件夹中。
triangle.xml
triangle.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item >
<rotate
android:fromDegrees="45"
android:toDegrees="45"
android:pivotX="-40%"
android:pivotY="87%" >
<shape
android:shape="rectangle" >
<stroke android:color="@android:color/transparent" android:width="10dp"/>
<solid
android:color="#000000" />
</shape>
</rotate>
</item>
</layer-list>
rectangle.xml
rectangle.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle">
<solid android:color="#B2E3FA" />
</shape>
</item>
</layer-list>
and layout for shape you require.
以及你需要的形状的布局。
<RelativeLayout
android:id="@+id/rlv1"
android:layout_width="150dp"
android:layout_height="50dp"
android:background="@drawable/rectangle" />
<RelativeLayout
android:id="@+id/rlv2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="@+id/rlv1"
android:background="@drawable/triangle"
android:rotation="180" />
set margin according you required.
根据你的要求设定保证金。
源
#2
13
If you want a border for your layout
如果你想为你的布局设置一个边框
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear_root"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="@+id/text_message"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="@drawable/bg_rectangle"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:padding="8dp"
android:text="Abc"
/>
<ImageView
android:id="@+id/image_arrow"
android:layout_marginTop="-1.5dp"
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_gravity="center_horizontal"
android:background="@drawable/icon_arrow_down"
/>
</LinearLayout>
bg_rectangle
bg_rectangle
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#eaeaea" />
<stroke
android:width="1dp"
android:color="#f00" />
<corners android:radius="8dp" />
</shape>
icon_arrow_down, or you can create triangle by vector like here
icon_arrow_down,或者你可以像这里一样创建一个三角形
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<rotate
android:fromDegrees="45"
android:pivotX="135%"
android:pivotY="15%"
android:toDegrees="45"
>
<shape android:shape="rectangle">
<solid android:color="#eaeaea"/>
<stroke
android:width="1dp"
android:color="#f00" />
</shape>
</rotate>
</item>
</layer-list>
#3
4
The clean and right way to do this whilst keeping it dynamic is to extend the View class.
在保持动态的同时,干净而正确的方法是扩展View类。
Then in the onDraw you would do something like this:
然后在onDraw你会做这样的事情:
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawBackground(canvas);
}
private void drawBackground(Canvas canvas) {
int width = (int) mWidth;
int height = (int) mHeight;
Point a = new Point(0, 0);
Point b = new Point(width, 0);
Point c = new Point(width, height - mPointHeight);//mPointedHeight is the length of the triangle... in this case we have it dynamic and can be changed.
Point d = new Point((width/2)+(mPointedHeight/2), height - mPointHeight);
Point e = new Point((width/2), height);// this is the sharp point of the triangle
Point f = new Point((width/2)-(mPointedHeight/2), height - mPointHeight);
Point g = new Point(0, height - mPointHeight);
Path path = new Path();
path.moveTo(a.x, a.y);
path.lineTo(b.x, b.y);
path.lineTo(c.x, c.y);
path.lineTo(d.x, d.y);
path.lineTo(e.x, e.y);
path.lineTo(f.x, f.y);
path.lineTo(g.x, g.y);
canvas.drawPath(path, mPointedBackgroundPaint);// mPointedBackgroundPaint is whatever color you want as the fill.
}
There you go, no unnecessary layering or code that isn't dynamic or clean. You could also add the text in the box too.
在这里,没有不必要的分层或代码不是动态的或干净的。您也可以在框中添加文本。
#4
1
Use a triangle image and a rectangular image and mathematically align them in the above mentioned format. Use color filtering to dynamically change its color.
使用一个三角形图像和一个矩形图像,并在数学上按照上面提到的格式对齐它们。使用颜色过滤来动态改变它的颜色。
You can even draw them on a custom view, using vector graphics, using custom colors, and that would be another way of solving this problem.
你甚至可以在一个自定义视图上画出来,使用矢量图形,使用自定义颜色,这是解决这个问题的另一种方法。
#5
#6
0
First you can create one xml
inside drawable
folder
首先,您可以在可绘制文件夹中创建一个xml
That xml
will be responsible for the border color of rectangle shape
该xml将负责矩形形状的边框颜色。
You can create such border shape with below code
您可以用下面的代码创建这样的边框形状。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#B2E3FA" />
</shape>
</item>
<item android:left="5dp" android:bottom="5dp" android:top="5dp" >
<shape android:shape="rectangle">
<solid android:color="#D8D8D8" />
</shape>
</item>
</layer-list>
well this will create a required border to rectangle shape, you need to assign background of that rectangle shape with this drawable like this
这将创建一个需要的边框到矩形的形状,你需要把矩形的背景设定为这样的drawable。
android:background="@drawable/bg"
where bg
is xml file name which has been saved on drawable folder
在哪里bg是xml文件名已被保存在可绘制文件夹
After that you need to put that triangle exactly below to rectangle object.
之后,你需要把这个三角形正好放在矩形对象下面。
I hope you understood my logic
我希望你能理解我的逻辑
#7
0
Create custom view and draw traingle with canvas
创建自定义视图并使用画布绘制遍历
package com.example.dickbutt;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;
public class TriangleShapeView extends View {
public int colorCode = Color.MAGENTA;
public int getColorCode() {
return colorCode;
}
public void setColorCode(int colorCode) {
this.colorCode = colorCode;
}
public TriangleShapeView(Context context) {
super(context);
}
public TriangleShapeView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public TriangleShapeView(Context context, AttributeSet attrs) {
super(context, attrs);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int w = getWidth() / 2;
int h = getHeight() / 2;
Path path = new Path();
path.moveTo(0, 0);
path.lineTo(w, 2 * h);
path.lineTo(2 * w, 0);
path.lineTo(0, 0);
path.close();
Paint p = new Paint();
p.setColor(colorCode);
p.setAntiAlias(true);
canvas.drawPath(path, p);
}
}
Result
结果
Usage
使用
<TextView
android:id="@+id/progress_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@android:color/holo_purple"
android:gravity="center_horizontal"
android:text="200,0000000"
android:textColor="#fff" />
<com.example.dickbutt.TriangleShapeView
android:id="@+id/textView1"
android:layout_width="10dp"
android:layout_height="20dp"
android:layout_below="@+id/progress_value"
android:layout_centerHorizontal="true"
android:background="@drawable/rectangle"
android:gravity="center_horizontal"
android:textSize="10sp" />
Advantages
优势
- Change shape according to width and height of view .
- 根据视图的宽度和高度改变形状。
- Highly customization possible.
- 高度定制成为可能。
- Look cleaner
- 看起来更清晰
#1
55
Here it is XML
for triangle
and rectangle
. save it inside drawable folder.
这里是三角形和矩形的XML。将其保存在可伸缩的文件夹中。
triangle.xml
triangle.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item >
<rotate
android:fromDegrees="45"
android:toDegrees="45"
android:pivotX="-40%"
android:pivotY="87%" >
<shape
android:shape="rectangle" >
<stroke android:color="@android:color/transparent" android:width="10dp"/>
<solid
android:color="#000000" />
</shape>
</rotate>
</item>
</layer-list>
rectangle.xml
rectangle.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle">
<solid android:color="#B2E3FA" />
</shape>
</item>
</layer-list>
and layout for shape you require.
以及你需要的形状的布局。
<RelativeLayout
android:id="@+id/rlv1"
android:layout_width="150dp"
android:layout_height="50dp"
android:background="@drawable/rectangle" />
<RelativeLayout
android:id="@+id/rlv2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="@+id/rlv1"
android:background="@drawable/triangle"
android:rotation="180" />
set margin according you required.
根据你的要求设定保证金。
源
#2
13
If you want a border for your layout
如果你想为你的布局设置一个边框
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear_root"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="@+id/text_message"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="@drawable/bg_rectangle"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:padding="8dp"
android:text="Abc"
/>
<ImageView
android:id="@+id/image_arrow"
android:layout_marginTop="-1.5dp"
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_gravity="center_horizontal"
android:background="@drawable/icon_arrow_down"
/>
</LinearLayout>
bg_rectangle
bg_rectangle
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#eaeaea" />
<stroke
android:width="1dp"
android:color="#f00" />
<corners android:radius="8dp" />
</shape>
icon_arrow_down, or you can create triangle by vector like here
icon_arrow_down,或者你可以像这里一样创建一个三角形
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<rotate
android:fromDegrees="45"
android:pivotX="135%"
android:pivotY="15%"
android:toDegrees="45"
>
<shape android:shape="rectangle">
<solid android:color="#eaeaea"/>
<stroke
android:width="1dp"
android:color="#f00" />
</shape>
</rotate>
</item>
</layer-list>
#3
4
The clean and right way to do this whilst keeping it dynamic is to extend the View class.
在保持动态的同时,干净而正确的方法是扩展View类。
Then in the onDraw you would do something like this:
然后在onDraw你会做这样的事情:
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawBackground(canvas);
}
private void drawBackground(Canvas canvas) {
int width = (int) mWidth;
int height = (int) mHeight;
Point a = new Point(0, 0);
Point b = new Point(width, 0);
Point c = new Point(width, height - mPointHeight);//mPointedHeight is the length of the triangle... in this case we have it dynamic and can be changed.
Point d = new Point((width/2)+(mPointedHeight/2), height - mPointHeight);
Point e = new Point((width/2), height);// this is the sharp point of the triangle
Point f = new Point((width/2)-(mPointedHeight/2), height - mPointHeight);
Point g = new Point(0, height - mPointHeight);
Path path = new Path();
path.moveTo(a.x, a.y);
path.lineTo(b.x, b.y);
path.lineTo(c.x, c.y);
path.lineTo(d.x, d.y);
path.lineTo(e.x, e.y);
path.lineTo(f.x, f.y);
path.lineTo(g.x, g.y);
canvas.drawPath(path, mPointedBackgroundPaint);// mPointedBackgroundPaint is whatever color you want as the fill.
}
There you go, no unnecessary layering or code that isn't dynamic or clean. You could also add the text in the box too.
在这里,没有不必要的分层或代码不是动态的或干净的。您也可以在框中添加文本。
#4
1
Use a triangle image and a rectangular image and mathematically align them in the above mentioned format. Use color filtering to dynamically change its color.
使用一个三角形图像和一个矩形图像,并在数学上按照上面提到的格式对齐它们。使用颜色过滤来动态改变它的颜色。
You can even draw them on a custom view, using vector graphics, using custom colors, and that would be another way of solving this problem.
你甚至可以在一个自定义视图上画出来,使用矢量图形,使用自定义颜色,这是解决这个问题的另一种方法。
#5
0
Use Canvas in onDraw method inside custom View
class.
在自定义视图类内的onDraw方法中使用Canvas。
Other way is to use Path class.
另一种方法是使用Path类。
#6
0
First you can create one xml
inside drawable
folder
首先,您可以在可绘制文件夹中创建一个xml
That xml
will be responsible for the border color of rectangle shape
该xml将负责矩形形状的边框颜色。
You can create such border shape with below code
您可以用下面的代码创建这样的边框形状。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#B2E3FA" />
</shape>
</item>
<item android:left="5dp" android:bottom="5dp" android:top="5dp" >
<shape android:shape="rectangle">
<solid android:color="#D8D8D8" />
</shape>
</item>
</layer-list>
well this will create a required border to rectangle shape, you need to assign background of that rectangle shape with this drawable like this
这将创建一个需要的边框到矩形的形状,你需要把矩形的背景设定为这样的drawable。
android:background="@drawable/bg"
where bg
is xml file name which has been saved on drawable folder
在哪里bg是xml文件名已被保存在可绘制文件夹
After that you need to put that triangle exactly below to rectangle object.
之后,你需要把这个三角形正好放在矩形对象下面。
I hope you understood my logic
我希望你能理解我的逻辑
#7
0
Create custom view and draw traingle with canvas
创建自定义视图并使用画布绘制遍历
package com.example.dickbutt;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;
public class TriangleShapeView extends View {
public int colorCode = Color.MAGENTA;
public int getColorCode() {
return colorCode;
}
public void setColorCode(int colorCode) {
this.colorCode = colorCode;
}
public TriangleShapeView(Context context) {
super(context);
}
public TriangleShapeView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public TriangleShapeView(Context context, AttributeSet attrs) {
super(context, attrs);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int w = getWidth() / 2;
int h = getHeight() / 2;
Path path = new Path();
path.moveTo(0, 0);
path.lineTo(w, 2 * h);
path.lineTo(2 * w, 0);
path.lineTo(0, 0);
path.close();
Paint p = new Paint();
p.setColor(colorCode);
p.setAntiAlias(true);
canvas.drawPath(path, p);
}
}
Result
结果
Usage
使用
<TextView
android:id="@+id/progress_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@android:color/holo_purple"
android:gravity="center_horizontal"
android:text="200,0000000"
android:textColor="#fff" />
<com.example.dickbutt.TriangleShapeView
android:id="@+id/textView1"
android:layout_width="10dp"
android:layout_height="20dp"
android:layout_below="@+id/progress_value"
android:layout_centerHorizontal="true"
android:background="@drawable/rectangle"
android:gravity="center_horizontal"
android:textSize="10sp" />
Advantages
优势
- Change shape according to width and height of view .
- 根据视图的宽度和高度改变形状。
- Highly customization possible.
- 高度定制成为可能。
- Look cleaner
- 看起来更清晰