【问题标题】:How to create android drawable consisting of two colors side by side?如何创建由两种颜色并排组成的android drawable?
【发布时间】:2016-11-02 17:37:53
【问题描述】:

使用 XML 是否可以创建一个可绘制对象,其中一半是颜色 1,另一半是颜色 2?当我将该可绘制对象设置为视图的背景时,它应该如下图所示。

【问题讨论】:

    标签: android drawable android-drawable xml-drawable


    【解决方案1】:

    通过 xml 实现:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:right="100dp">
        <shape android:shape="rectangle">
            <size android:height="100dp" android:width="100dp"/>
            <solid android:color="@android:color/black"/>
        </shape>
    </item>
    
    <item android:left="100dp">
        <shape android:shape="rectangle">
            <size android:height="100dp" android:width="100dp"/>
            <solid android:color="@android:color/holo_green_light"/>
        </shape>
    </item>
    </layer-list>
    

    将其放入res/drawable 文件夹中并分配为android:background 给图像

    【讨论】:

    • 谢谢。这个解决方案最接近我想要实现的目标。
    • 如果你将此可绘制对象设置为布局内的背景,它将是静态的(200dp 高度)
    【解决方案2】:

    您可以尝试创建 2 个具有各自颜色的形状,然后使用它们。

    您可以通过为它们编写 xml 并给出尺寸和颜色来制作这些形状。

    【讨论】:

      【解决方案3】:

      这是可能的。你可以:

      1) 创建一个形状

      <shape xmlns:android="http://schemas.android.com/apk/res/android"
      
        android:shape="rectangle" >
      
        <stroke
          android:width="1dp"
          android:color="@color/gray_light" />
      
        <gradient
          android:type="linear"
          android:centerX="0"
          android:centerY="1"
          android:startColor="#bff54a"
          android:endColor="#88c010" />
      
        <corners
          android:bottomLeftRadius="8dp"
          android:bottomRightRadius="8dp"
          android:topLeftRadius="8dp"
          android:topRightRadius="8dp" />
      
      </shape>
      

      2) 扩展可绘制类

      public class ColorBarDrawable extends Drawable {
      
          private int[] themeColors;
      
          public ColorBarDrawable(int[] themeColors) {
              this.themeColors = themeColors;
          }
      
          @Override
          public void draw(Canvas canvas) {
      
              // get drawable dimensions
              Rect bounds = getBounds();
      
              int width = bounds.right - bounds.left;
              int height = bounds.bottom - bounds.top;
      
              // draw background gradient
              Paint backgroundPaint = new Paint();
              int barWidth = width / themeColors.length;
              int barWidthRemainder = width % themeColors.length;
              for (int i = 0; i < themeColors.length; i++) {
                  backgroundPaint.setColor(themeColors[i]);
                  canvas.drawRect(i * barWidth, 0, (i + 1) * barWidth, height, backgroundPaint);
              }
      
              // draw remainder, if exists
              if (barWidthRemainder > 0) {
                  canvas.drawRect(themeColors.length * barWidth, 0, themeColors.length * barWidth + barWidthRemainder, height, backgroundPaint);
              }
      
          }
      
          @Override
          public void setAlpha(int alpha) {
          }
      
          @Override
          public void setColorFilter(ColorFilter cf) {
      
          }
      
          @Override
          public int getOpacity() {
              return PixelFormat.OPAQUE;
          }
      
      }
      

      来源:手性代码 - StackOverflow

      【讨论】:

        【解决方案4】:

        通过xml:

        <rotate xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromDegrees="90">
        <shape>
            <gradient
                android:centerX="-10.0"
                android:endColor="@android:color/holo_blue_dark"
                android:startColor="@android:color/holo_green_light"
                android:type="sweep" />
        </shape>
        
        适合任何视图尺寸。

        您也可以使用 9-Patch 文件:

        https://developer.android.com/studio/write/draw9patch

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-10-24
          • 2018-07-24
          • 2022-10-16
          • 1970-01-01
          • 2012-03-27
          • 2013-07-06
          • 1970-01-01
          相关资源
          最近更新 更多