我想在线性布局的背景中添加一个图像,并且我知道该属性将为android:background =“ @ drawable / login_bg”,但是现在我必须创建一个可绘制的资源文件,在该文件中,我想要左下和右下角是圆角的,左上和右上角是矩形。 记住:我需要在背景内带有圆角的图像。
我已经尝试过link
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle" android:padding="10dp">
<corners
android:bottomRightRadius="15dp"
android:bottomLeftRadius="15dp"
android:topLeftRadius="0dp"
android:topRightRadius="0dp"/>
</shape>
</item>
<item android:drawable="@drawable/login_bg" />
</layer-list>
答案 0 :(得分:0)
尝试使用此customview。但是您必须从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)
}
}
答案 1 :(得分:0)
我得到了一个解决方案,我通过以下方式做到了这一点: 我正在写我的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”是Image的视图,我正在调用一个名为“ RoundedImage”的单独的类,并通过传递其参数来执行此操作,而我的RoundedImage类如下所示:>
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;
}
}
事情就是这样,我能够得到需要的东西。