有没有办法在xml文件中指定三角形?
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="triangle">
<stroke android:width="1dip" android:color="#FFF" />
<solid android:color="#FFF" />
</shape>
Run Code Online (Sandbox Code Playgroud)
我们可以用路径形状做什么吗?我只需要一个等边三角形.
谢谢
Jac*_*ski 165
在这篇文章中,我描述了如何做到这一点.这是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="@color/transparent" android:width="10dp"/>
<solid
android:color="@color/your_color_here" />
</shape>
</rotate>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
如果有什么不清楚或者您需要解释它是如何构建的,请参阅我的帖子.它旋转了一个切口矩形:)这是非常聪明和良好的工作解决方案.
编辑:创建一个指向的箭头 - >使用:
...
android:fromDegrees="45"
android:toDegrees="45"
android:pivotX="13%"
android:pivotY="-40%" >
...
Run Code Online (Sandbox Code Playgroud)
并创建一个指向< - use的箭头:
android:fromDegrees="45"
android:toDegrees="45"
android:pivotX="87%"
android:pivotY="140%" >
Run Code Online (Sandbox Code Playgroud)
Elh*_*aky 80
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="?"/>
Run Code Online (Sandbox Code Playgroud)
你可以在这里获得更多选择.
Pha*_*inh 55
你可以vector像这样制作三角形
ic_triangle_right.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:pathData="M0,12l0,12 11.5,-5.7c6.3,-3.2 11.5,-6 11.5,-6.3 0,-0.3 -5.2,-3.1 -11.5,-6.3l-11.5,-5.7 0,12z"
android:strokeColor="#00000000"
android:fillColor="#000000"/>
</vector>
Run Code Online (Sandbox Code Playgroud)
然后就像使用它一样
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_triangle_right"
/>
Run Code Online (Sandbox Code Playgroud)
要改变颜色和方向,请使用android:tint和android:rotation
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_triangle_right"
android:rotation="180" // change direction
android:tint="#00f" // change color
/>
Run Code Online (Sandbox Code Playgroud)
结果
要更改矢量的形状,可以更改矢量的宽度/高度.示例将宽度更改为10dp
<vector
android:width="10dp"
android:height="24dp"
>
...
</vector>
Run Code Online (Sandbox Code Playgroud)
Oli*_*etz 43
您可以使用矢量绘图.
如果您的最低API低于21,则Android Studio会在构建时自动为这些较低版本创建PNG位图(请参阅Vector Asset Studio).如果您使用支持库,Android甚至可以管理"真正的向量"到API 7(更多内容在此帖子的更新底部).
红色向上指向的三角形将是:
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="100dp"
android:width="100dp"
android:viewportHeight="100"
android:viewportWidth="100" >
<group
android:name="triableGroup">
<path
android:name="triangle"
android:fillColor="#FF0000"
android:pathData="m 50,0 l 50,100 -100,0 z" />
</group>
</vector>
Run Code Online (Sandbox Code Playgroud)
将它添加到您的布局中并记住如果旋转三角形则设置clipChildren ="false".
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false">
<ImageView
android:layout_width="130dp"
android:layout_height="100dp"
android:rotation="0"
android:layout_centerInParent="true"
android:background="@drawable/triangle"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
通过设置Views layout_width/layout_height属性来更改三角形的大小(宽度/高度).这样,如果你的数学运算正确,你也可以得到一个eqilateral triagle.
更新25.11.2017
如果使用支持库,您可以使用真实的向量(而不是创建位图),这可以追溯到API 7.只需添加:
vectorDrawables.useSupportLibrary = true
Run Code Online (Sandbox Code Playgroud)
defaultConfig在你的模块的build.gradle中做你的.
然后像这样设置(vector xml)drawable:
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:srcCompat="@drawable/triangle" />
Run Code Online (Sandbox Code Playgroud)
Vector Asset Studio页面上的所有内容都记录得非常好.
自从这个功能以来,我一直在完全没有图标方面的位图.这也大大降低了APK的大小.
Pet*_*ter 39
在这种情况下,我会定义实现一个View:
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 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;
Path path = new Path();
path.moveTo( w, 0);
path.lineTo( 2 * w , 0);
path.lineTo( 2 * w , w);
path.lineTo( w , 0);
path.close();
Paint p = new Paint();
p.setColor( Color.RED );
canvas.drawPath(path, p);
}
}
Run Code Online (Sandbox Code Playgroud)
在布局中使用它,如下所示:
<TriangleShapeView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff487fff">
</TriangleShapeView>
Run Code Online (Sandbox Code Playgroud)
使用此实现将为您提供以下结果:

sen*_*aux 38
Jacek Milewski的解决方案适用于我,根据他的解决方案,如果你需要和反向三角形,你可以使用它:
<?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="135%"
android:pivotY="15%">
<shape android:shape="rectangle">
<solid android:color="@color/aquamarine" />
</shape>
</rotate>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
zed*_*zed 19
请参阅此处的答案: 没有图片的自定义箭头:Android
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="32dp"
android:height="24dp"
android:viewportWidth="32.0"
android:viewportHeight="24.0">
<path android:fillColor="#e4e4e8"
android:pathData="M0 0 h32 l-16 24 Z"/>
</vector>
Run Code Online (Sandbox Code Playgroud)
Ahm*_*eim 11
我可以不使用XML来帮助你吗?
只是,
自定义布局(切片):
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.View;
public class Slice extends View {
Paint mPaint;
Path mPath;
public enum Direction {
NORTH, SOUTH, EAST, WEST
}
public Slice(Context context) {
super(context);
create();
}
public Slice(Context context, AttributeSet attrs) {
super(context, attrs);
create();
}
public void setColor(int color) {
mPaint.setColor(color);
invalidate();
}
private void create() {
mPaint = new Paint();
mPaint.setStyle(Style.FILL);
mPaint.setColor(Color.RED);
}
@Override
protected void onDraw(Canvas canvas) {
mPath = calculate(Direction.SOUTH);
canvas.drawPath(mPath, mPaint);
}
private Path calculate(Direction direction) {
Point p1 = new Point();
p1.x = 0;
p1.y = 0;
Point p2 = null, p3 = null;
int width = getWidth();
if (direction == Direction.NORTH) {
p2 = new Point(p1.x + width, p1.y);
p3 = new Point(p1.x + (width / 2), p1.y - width);
} else if (direction == Direction.SOUTH) {
p2 = new Point(p1.x + width, p1.y);
p3 = new Point(p1.x + (width / 2), p1.y + width);
} else if (direction == Direction.EAST) {
p2 = new Point(p1.x, p1.y + width);
p3 = new Point(p1.x - width, p1.y + (width / 2));
} else if (direction == Direction.WEST) {
p2 = new Point(p1.x, p1.y + width);
p3 = new Point(p1.x + width, p1.y + (width / 2));
}
Path path = new Path();
path.moveTo(p1.x, p1.y);
path.lineTo(p2.x, p2.y);
path.lineTo(p3.x, p3.y);
return path;
}
}
Run Code Online (Sandbox Code Playgroud)
你的活动(例子):
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
public class Layout extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Slice mySlice = new Slice(getApplicationContext());
mySlice.setBackgroundColor(Color.WHITE);
setContentView(mySlice, new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
}
}
Run Code Online (Sandbox Code Playgroud)
工作实例:

Calculate您可能感兴趣的另一个绝对简单的功能..
private Path Calculate(Point A, Point B, Point C) {
Path Pencil = new Path();
Pencil.moveTo(A.x, A.y);
Pencil.lineTo(B.x, B.y);
Pencil.lineTo(C.x, C.y);
return Pencil;
}
Run Code Online (Sandbox Code Playgroud)
对于那些想要一个正确的三角形箭头的人,你可以去:
步骤1:创建可绘制的XML文件,将以下XML内容复制并粘贴到可绘制的XML中.(请注意,您可以使用任何名称作为可绘制的XML文件.对于我的情况,我将其命名为"v_right_arrow")
<?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="15%"
android:pivotY="-36%" >
<shape
android:shape="rectangle" >
<stroke android:color="@android:color/transparent" android:width="1dp"/>
<solid
android:color="#000000" />
</shape>
</rotate>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
第2步:在布局的XML中,创建一个View并将其背景绑定到刚刚在STEP 1中创建的可绘制XML .对于我的情况,我将v_right_arrow绑定到我的View的background属性.
<View
android:layout_width="200dp"
android:layout_height="200dp"
android:background="@drawable/v_right_arrow">
</View>
Run Code Online (Sandbox Code Playgroud)
样本输出:
希望这有帮助,祝你好运!
使用矢量可绘制:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:pathData="M0,0 L24,0 L0,24 z"
android:strokeColor="@color/color"
android:fillColor="@color/color"/>
</vector>
Run Code Online (Sandbox Code Playgroud)
您可以使用以下xml在背景中添加以下三角形

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="100dp"
android:width="100dp"
android:viewportHeight="100"
android:viewportWidth="100" >
<group
android:name="triableGroup">
<path
android:name="triangle"
android:fillColor="#848af8"
android:pathData="M 0,20 L 0,0 L 100,0 L 100,20 L 54,55 l -1,0.6 l -1,0.4 l -1,0.2 l -1,0 l -1,-0 l -1,-0.2 l -1,-0.4 l -1,-0.6 L 46,55 L 0,20 -100,-100 Z" />
</group>
</vector>
Run Code Online (Sandbox Code Playgroud)
定制xml设计的整个逻辑是在路径数据中。考虑左上角为(0,0)并根据需要设计布局。检查此答案。
我从来没有这样做过,但据我了解,您可以使用 PathShape 类: http://developer.android.com/reference/android/graphics/drawable/shapes/PathShape.html
| 归档时间: |
|
| 查看次数: |
141824 次 |
| 最近记录: |