我希望能够拥有多个RelativeLayouts的屏幕,并且我希望顶部布局和底部布局具有圆角,所以顶部布局将具有顶部2角落圆角,底部布局将有底部2角落圆角。Android围绕布局背景图像,只有顶部或底部的角落
我的问题是,我在网上找到的所有例子都使用shape.xml来创建一个圆角,并给它一个渐变,这是不够好,因为我想给relativeLayout一个背景图像,并有那个形象圆润了,我似乎无法做到这一点。
任何帮助将不胜感激!
编辑 - 赏金开始
好吧,我一直在敲打我的头靠在墙上的这一个年龄段。目前我正在使用名为UITableView的第三方工具,主要是测试一些东西。
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,并使用白色梯度,而不是一个图像变圆。我需要能够膨胀布局,然后弯曲顶部和底部的角落。我已经看过很多裁剪角落的例子,甚至尝试了不同的第三方工具,但还没有找到一个例子,它显示了将一个背景图像应用于一个容器,然后将角落四舍五入。
有没有人有任何想法如何做到这一点?
编辑:
在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));
}
我使用指标设置多少舍入视图,因此它随着不同的屏幕尺寸缩放。
希望能帮助有这个问题的人!
因此,您需要为您的活动中的主布局设置圆角背景,是否正确..或甚至它不在活动中,以容纳其他视图的主容器? – hardartcore 2013-03-13 12:12:38
正确的,我需要能够给该容器一个背景图像,并有该容器的角落2圆角,无论是顶部2角落或底部2角落 – AdamM 2013-03-13 12:15:18
如果你想活动的背景有圆角,那么这活动应首先透明化。然后,将您的圆形图像设置为底部视图的背景。 – Lumis 2013-03-19 23:13:41