【问题标题】:Draw android xml shape diagonal with triangles用三角形绘制android xml形状对角线
【发布时间】:2015-10-01 09:02:10
【问题描述】:

你好,我想画这样的东西

有形状可以吗?

【问题讨论】:

  • 你尝试了什么?
  • 我尝试了两个矩形并旋转它们,但我没有绘制 xml 形状的经验..
  • 为什么要尝试使用图片
  • 我需要然后添加圆角半径 5 dp 像这样:<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <corners android:radius="5dp"/> <solid android:color="@color/button_buy_pre" /> </shape>

标签: android xml shape diagonal


【解决方案1】:

我知道现在回答这个问题为时已晚,但仍然在这里发布我的答案,以便其他人可以参考:

<?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">

<path
    android:name="dark_triangle"
    android:fillColor="#FFFFFF"
    android:pathData="M 100,0 L 0,100 100,100 z" />

<path
    android:name="light_triangle"
    android:fillColor="#d0021b"
    android:pathData="M 0,0 L 100,0 0,100 z" />

</vector>

【讨论】:

  • 谢谢。这是最好的答案。
  • 有史以来最简洁的答案!我需要参加关于在 Android 中创建矢量图形的速成课程!
【解决方案2】:

@Pauli。你可以使用Image来轻松练习,可以试试这种方式。使用这个逻辑

     <?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:drawable="@color/Your_color">
</item>

<item>
    <rotate
        android:fromDegrees="50"
        android:pivotX="10%"
        android:pivotY="85%"
         >
        <shape

            android:shape="rectangle">
            <solid android:color="#000000" />

        </shape>
    </rotate>
</item>

请见谅

Diagonally spilliting background of a Relative Layout

Making a triangle shape using xml definitions?

【讨论】:

  • 我以前看过这个链接并尝试过..但这不是我想要的
【解决方案3】:
  1. 在colors.xml文件中创建颜色资源

    <color name="primaryColor">#3F51B5</color>
    <color name="primaryColorDark">#303F9F</color>
    
  2. 创建Java文件TriangleBackgroundView.Java:

    public class TriangleBackgroundView extends View {
    Paint paint;
    Paint bgPaint;
    
    public TriangleBackgroundView(Context context) {
        super(context);
        init();
    }
    
    public TriangleBackgroundView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    
    public TriangleBackgroundView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    
    private void init() {
        paint = new Paint();
        paint.setStrokeWidth(1);
        paint.setAntiAlias(true);
        paint.setStrokeCap(Paint.Cap.SQUARE);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(getResources().getColor(R.color.primaryColor));
    
        bgPaint= new Paint();
        bgPaint.setStyle(Paint.Style.FILL);
        bgPaint.setColor(getResources().getColor(R.color.primaryColorDark));
    }
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    
        int h = getMeasuredHeight();
        int w = getMeasuredWidth();
    
        canvas.drawRect(0,0,w,h,bgPaint);
    
        Path path = new Path();
        path.moveTo(0, h);
        path.lineTo(w, h);
        path.lineTo(0, 0);
        path.lineTo(0, h);
        path.close();
        canvas.drawPath(path,paint);
    }
    }
    
  3. 现在在您的布局中添加自定义视图:

<com.yourpackage.TriangleBackgroundView
    android:layout_height="match_parent"
    android:layout_width="match_parent"/>

<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textStyle="bold"
    android:textSize="20dp"
    android:textColor="@android:color/white"
    android:layout_centerInParent="true"/>

您可以从本教程中获得帮助,用三角形创建形状对角线:

来源:http://marcouberti.net/2015/06/23/create-a-custom-triangle-background-view-in-android/

【讨论】:

  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
  • 编辑帖子@AbhinavSinghMaurya
【解决方案4】:

你可以这样使用:

<?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="@color/yellow_color"/>
        </shape>
    </item>
    <item>
        <rotate
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:fromDegrees="45"
            android:pivotX="85%"
            android:pivotY="135%">
            <shape>
                <solid android:color="@color/brown_color" />

            </shape>
        </rotate>
    </item>
    <item>
        <rotate
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:fromDegrees="45"
            android:pivotX="-35%"
            android:pivotY="85%">
            <shape android:shape="rectangle">
                <solid android:color="@color/brown_color" />

            </shape>
        </rotate>
    </item>
</layer-list>

两个旋转的矩形会在您的图像中完成填充棕色边的工作。

【讨论】:

    【解决方案5】:

    half_diagonal.xml

    <?xml version="1.0" encoding="utf-8"?>
    <vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="200dp"
        android:height="200dp"
        android:viewportHeight="100"
        android:viewportWidth="100">
    
        <path
            android:name="dark_triangle"
            android:fillColor="#673AB7"
            android:pathData="M 0,0 L 100,0 0,100 z" />
    
    </vector>
    

    【讨论】:

      【解决方案6】:

      你可以使用渐变工具...

      http://angrytools.com/gradient/

      【讨论】:

      • 虽然理论上这可以回答这个问题,it would be preferable 在这里包含答案的基本部分,并提供链接以供参考。
      • 背景:-moz-linear-gradient(45deg, rgba(192,192,192,1) 0%, rgba(192,192,192,1) 49%, rgba(0,0,0,1) 50%, RGBA(0,0,0,1) 100%); /* ff3.6+ / background: -webkit-gradient(linear, left bottom, right top, color-stop(0%, rgba(192,192,192,1)), color-stop(49%, rgba(192,192,192) ,1)), 颜色停止(50%, rgba(0,0,0,1)), 颜色停止(100%, rgba(0,0,0,1))); / safari4+,chrome / 背景:-webkit-linear-gradient(45deg, rgba(192,192,192,1) 0%, rgba(192,192,192,1) 49%, rgba(0,0,0,1 ) 50%, RGBA(0,0,0,1) 100%); / safari5.1+,chrome10+ */
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-26
      • 1970-01-01
      相关资源
      最近更新 更多