我希望能够拥有一个具有多个 RelativeLayouts 的屏幕,并且我希望顶部布局和底部布局具有圆角,因此顶部布局将顶部 2 个角圆角,底部布局将底部 2 个角圆角。
我的问题是,我在网上找到的所有示例都使用 shape.xml 创建一个圆角并给它一个渐变,这还不够好,因为我想给 relativeLayout 一个背景图像,并使该图像变圆,我似乎不能同时做这两个。
任何帮助将非常感激!!
编辑 - 赏金开始
好吧,我多年来一直在用头撞墙。我目前正在使用一个名为 UITableView 的 3rd 方工具,主要只是测试一些东西。
https://github.com/thiagolocatelli/android-uitableview
它设置 tableView 类似于 iPhone 表格的方式,我希望能够为每一行提供背景图像,并使顶部和底部行弯曲。在这个 UITableView 类中,在提交下,这段代码被称为
public void commit()
    {
        mIndexController = 0;
        if (mItemList.size() > 1)
        {
            // when the list has more than one item
            for (IListItem obj : mItemList)
            {
                View tempItemView;
                if (mIndexController == 0)
                {
                    //tempItemView = new RoundedView(context_i, this);
                    tempItemView = mInflater.inflate(R.layout.list_item_top,null);
        } 
            else if (mIndexController == mItemList.size() - 1)
            {
                tempItemView = mInflater.inflate(R.layout.list_item_bottom,null);
            } 
            else
            {
                tempItemView = mInflater.inflate(R.layout.list_item_middle,null);
            }
            setupItem(tempItemView, obj, mIndexController);
            tempItemView.setClickable(obj.isClickable());
            mListContainer.addView(tempItemView);
            mIndexController++;
        }
    } 
    else if (mItemList.size() == 1)
    {
        // when the list has only one item
        View tempItemView = mInflater.inflate(R.layout.list_item_single,
                null);
        IListItem obj = mItemList.get(0);
        setupItem(tempItemView, obj, mIndexController);
        tempItemView.setClickable(obj.isClickable());
        mListContainer.addView(tempItemView);
    }
}
他有顶部中间和底部行的布局样式,顶部和底部圆形使用 XML,但问题是,我想给每一行一个图像。所以我添加了这段代码
tempItemView.setBackgroundResource(R.drawable.background);
但问题是,这会删除顶行和底行的弧形角,因为角是使用 XML 和使用白色渐变而不是图像进行圆角处理的。我需要能够使布局膨胀,然后弯曲顶角和底角。我看过这么多裁剪角落的例子,甚至尝试了不同的 3rd 方工具,但还没有找到一个例子来展示将背景图像应用于容器,然后圆角。
有没有人对如何做到这一点有任何想法?
编辑:
在 iPhone 上,你可以做这样的事情
UIColor *color = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"image.png"]];
将图像转换为颜色的位置。Android是否有等价物?
编辑:
感谢ACheese的回答,我修改了他的代码,把它分成了3种方法,一种用于顶部圆角,一种用于全圆角,一种用于底部圆角,并想出了这个
public void setBackgroundRounded(int resID, int w, int h, View v)
    {
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        double dH = (metrics.heightPixels / 100) * 1.5;
        int iHeight = (int)dH;
        Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bmp);
        Shader shader = new BitmapShader(BitmapFactory.decodeResource(
                getResources(), resID), Shader.TileMode.MIRROR,
                Shader.TileMode.MIRROR);
        Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
        paint.setAntiAlias(true);
        paint.setShader(shader);
        RectF rec = new RectF(0, 0, w, h);
        c.drawRoundRect(rec, iHeight, iHeight, paint);
        v.setBackgroundDrawable(new BitmapDrawable(getResources(), bmp));
    }
    public void setTopRounded(int resID, int w, int h, View v)
    {
        Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bmp);
        Shader shader = new BitmapShader(BitmapFactory.decodeResource(
                getResources(), resID), Shader.TileMode.MIRROR,
                Shader.TileMode.MIRROR);
        Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
        paint.setAntiAlias(true);
        paint.setShader(shader);
        RectF rec = new RectF(0, 0, w, h - 20);
        c.drawRect(new RectF(0, 20, w, h), paint);
        c.drawRoundRect(rec, 20, 20, paint);
        v.setBackgroundDrawable(new BitmapDrawable(getResources(), bmp));
    }
    public void setBottomRounded(int id, int w, int h, View v)
    {
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        double dH = (metrics.heightPixels / 100) * 1.5;
        int iHeight = (int)dH;
        Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bmp);
        Shader shader = new BitmapShader(BitmapFactory.decodeResource(
                getResources(), id), Shader.TileMode.MIRROR,
                Shader.TileMode.MIRROR);
        Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
        paint.setAntiAlias(true);
        paint.setShader(shader);
        RectF rec = new RectF(0, 0, w, h);
        c.drawRoundRect(rec, iHeight, iHeight, paint);
        c.drawRect(new RectF(0, 0, w, h-iHeight), paint);
        v.setBackgroundDrawable(new BitmapDrawable(getResources(), bmp));
    }
我使用指标来设置视图的圆角,因此它可以根据不同的屏幕尺寸进行缩放。
希望对遇到此问题的其他人有所帮助!