Android围绕一个Layouts背景图片,只有顶角或底角

Ada*_*amM 14 java android

我希望能够拥有一个具有多个RelativeLayouts的屏幕,并且我希望顶部布局和底部布局具有圆角,因此顶部布局将具有圆角的前2个角,并且底部布局将具有圆形的底部2个角.

我的问题是,我在网上找到的所有例子都使用shape.xml来创建一个圆角并给它一个渐变,这还不够好,因为我想给relativeLayout一个背景图像,并将该图像舍入,我似乎无法做到这两点.

任何帮助将非常感激!!

编辑 - 赏金开始

好吧,在这个问题上,我多年来一直在撞墙.我目前正在使用名为UITableView的第三方工具,主要是测试一些东西.

https://github.com/thiagolocatelli/android-uitableview

它设置tableView类似于iPhone表格的方式,我希望能够为每一行提供背景图像,并使顶部和底部行弯曲.在这个UITableView类中,在commit下,调用此代码

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

他有一个顶部中间和底部行的布局样式,顶部和底部圆形使用XML,但问题是,我想给每一行一个图像.所以我添加了这段代码

tempItemView.setBackgroundResource(R.drawable.background);
Run Code Online (Sandbox Code Playgroud)

但问题是,这会消除顶部和底部行的弯曲角落,因为角落使用XML并使用白色渐变而不是图像.我需要能够给布局充气,然后弯曲顶角和底角.我看了很多裁剪角落的例子,甚至尝试了不同的第三方工具,但还没有找到一个例子,它显示了将背景图像应用到容器,然后将角落四舍五入.

有没有人对如何做到这一点有任何想法?

编辑:

在iPhone上,你可以做这样的事情

UIColor *color = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"image.png"]];
Run Code Online (Sandbox Code Playgroud)

将图像转换为颜色的位置.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));
    }
Run Code Online (Sandbox Code Playgroud)

我使用指标来设置视图的四舍五入,因此它可以根据不同的屏幕尺寸进行缩放.

希望能帮助其他人解决这个问题!!

Ach*_*ese 2

检查我的解决方案是否适合您的情况:通过扩展RelativeLayout 定义您自己的布局。只需要添加以下代码即可

@SuppressWarnings("deprecation")
public void setBackground(int id){
    Bitmap bmp = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(bmp);
    Shader shader = new BitmapShader(BitmapFactory.decodeResource(getResources(), id), Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);

    Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
    paint.setAntiAlias(true);
    paint.setShader(shader);
    RectF rec = new RectF(0, 0, getMeasuredWidth(), getMeasuredHeight());
//  you may need this for only top round corner
//  RectF rec = new RectF(0, 0, getMeasuredWidth(), getMeasuredHeight()-20);
//  c.drawRect(new RectF(0, 20, getMeasuredWidth(), getMeasuredHeight()),      paint);
    c.drawRoundRect(rec, 20, 20, paint);
    this.setBackgroundDrawable(new BitmapDrawable(getResources(), bmp));

}
Run Code Online (Sandbox Code Playgroud)

从您的活动中调用此方法。您无法从 onCreate() 调用,因为 getMeasuredWidth() 和 getMeasuredHeight() 尚未准备好。通过设置您自己的可绘制 ID 来覆盖并从 onWindowFocusChanged(boolean hasFocused) 调用。这将重复将您的图像设置为圆角背景。