XML android中底部为圆形的背景图像
我想在线性布局的背景中添加一个图像,我知道该属性将是android:background=“@drawable/login_bg”,但现在我必须创建一个可绘制的资源文件,在该文件中,我希望左下方和右下方为圆形,左上方和右上方为矩形。 记住:我需要一个圆角背景内的图像 我试过这个XML android中底部为圆形的背景图像,android,android-layout,android-xml,Android,Android Layout,Android Xml,我想在线性布局的背景中添加一个图像,我知道该属性将是android:background=“@drawable/login_bg”,但现在我必须创建一个可绘制的资源文件,在该文件中,我希望左下方和右下方为圆形,左上方和右上方为矩形。 记住:我需要一个圆角背景内的图像 我试过这个 尝试使用此自定义视图。但您必须从android:background更改为android:src class RadiusImageView: AppCompatImageView { private val clip
尝试使用此自定义视图。但您必须从android:background更改为android:src
class RadiusImageView: AppCompatImageView {
private val clipPath = Path()
constructor(context: Context) : super(context) {}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {}
constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) {}
override fun onDraw(canvas: Canvas) {
//float radius = 36.0f;
val rect = RectF(0f, 0f, width.toFloat(), height.toFloat())
// 4 Pair of radius : top-left, top -right, bottom-right, bottom left, each pair is radius
// for rx and ry for each corner
clipPath.addRoundRect(rect, floatArrayOf(0f,0f,0f,0f,40f,40f,40f,40f) Path.Direction.CW)
canvas.clipPath(clipPath)
super.onDraw(canvas)
}
}我找到了一个解决方案,我用以下方法解决了这个问题: 我正在编写onCreate方法的一部分
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
topbackground=(ImageView) findViewById(R.id.topbackground);
Bitmap image1 = BitmapFactory.decodeResource(getResources(),R.drawable.login_bg)
topbackground.setImageBitmap(roundedImage.getRoundedCornerBitmap(this,
image1,200,image1.getWidth(),image1.getHeight(),true,true,false,false ));
}
RoundedImage roundedImage = new RoundedImage();
有一个名为“login_bg”的图像,“topbackground”是图像的视图,我正在调用一个名为“RoundeImage”的单独类,并通过传递其参数来执行此操作,我的类RoundeImage如下所示:
public class RoundedImage {
public static Bitmap getRoundedCornerBitmap(Context context, Bitmap input, int
pixels , int w , int h , boolean squareTL, boolean squareTR, boolean squareBL,
boolean squareBR ) {
Bitmap output = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final float densityMultiplier =
context.getResources().getDisplayMetrics().density;
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, w, h);
final RectF rectF = new RectF(rect);
//make sure that our rounded corner is scaled appropriately
final float roundPx = pixels*densityMultiplier;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
//draw rectangles over the corners we want to be square
if (squareTL ){
canvas.drawRect(0, 0, w/2, h/2, paint);
}
if (squareTR ){
canvas.drawRect(w/2, 0, w, h/2, paint);
}
if (squareBL ){
canvas.drawRect(0, h/2, w/2, h, paint);
}
if (squareBR ){
canvas.drawRect(w/2, h/2, w, h, paint);
}
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(input, 0,0, paint);
return output;
}
}
这就是它的运行方式,我能够得到所需要的。希望这个答案对@ViduraPrasangana有帮助@ViduraPrasangana我已经阅读了这篇文章,我在我的项目中创建了这个类,现在如何将它与XML文件集成。你可以尝试更改
项
s的顺序吗?签出这个@paritoshurohit你能发布一张图片吗你想实现什么?没有这么复杂!你可以简单地使用xml@HelloWorld这在我的情况下不起作用,我不会记下你的答案,无论如何,谢谢!
public class RoundedImage {
public static Bitmap getRoundedCornerBitmap(Context context, Bitmap input, int
pixels , int w , int h , boolean squareTL, boolean squareTR, boolean squareBL,
boolean squareBR ) {
Bitmap output = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final float densityMultiplier =
context.getResources().getDisplayMetrics().density;
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, w, h);
final RectF rectF = new RectF(rect);
//make sure that our rounded corner is scaled appropriately
final float roundPx = pixels*densityMultiplier;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
//draw rectangles over the corners we want to be square
if (squareTL ){
canvas.drawRect(0, 0, w/2, h/2, paint);
}
if (squareTR ){
canvas.drawRect(w/2, 0, w, h/2, paint);
}
if (squareBL ){
canvas.drawRect(0, h/2, w/2, h, paint);
}
if (squareBR ){
canvas.drawRect(w/2, h/2, w, h, paint);
}
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(input, 0,0, paint);
return output;
}
}