【问题标题】:Rectangle shape with two solid colors具有两种纯色的矩形
【发布时间】:2013-07-13 13:00:23
【问题描述】:

我想用两种纯色(水平)创建一个矩形来实现这样的效果:

我听说过layer-list,虽然我可以用它来包含两个颜色不同的矩形,但它似乎只能垂直放置形状。

有没有办法使用 lalyer-list 来实现这一点,还是我应该使用完全不同的东西?我想保持简单,能够在运行时更改形状颜色。

谢谢。

【问题讨论】:

  • 查看九个补丁绘图
  • @nathan 看看我的回答!

标签: android android-xml shape


【解决方案1】:

这肯定会根据您的要求绘制 形状

根据需要调整<item> 的大小!

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

    <item
        android:left="50dip">
        <shape
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle" >
            <solid android:color="#0000FF" />
        </shape>
    </item>
    <item android:right="50dip">
        <shape
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle" >
            <solid android:color="#ff0000" />
        </shape>
    </item>

</layer-list>

【讨论】:

  • 不错的一个.. 是否可以仅在特定边上添加圆形边框?
  • 我们可以让左边的match_parent吗?
  • 有什么方法可以使这些左/右值成为一个比率,而不必对它们进行硬编码。希望重新使用不同大小的drawable。
【解决方案2】:

您可以为此创建自定义可绘制对象。只需扩展 Drawable 类。

这是一个示例代码,可以根据需要绘制一个矩形,您可以提供任意数量的颜色。

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;
    }

}

【讨论】:

    【解决方案3】:

    这将给你两种颜色一半和一半垂直。将此代码放入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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-14
      • 2017-11-13
      • 2023-03-18
      • 1970-01-01
      • 2012-05-29
      • 1970-01-01
      相关资源
      最近更新 更多