【问题标题】:Background image with rounded bottom in XML androidXML android中圆底的背景图像
【发布时间】:2019-09-30 05:38:27
【问题描述】:

我想在我的线性布局的背景中添加一个图像,我知道属性将是 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>

【问题讨论】:

  • 希望这个答案会有所帮助stackoverflow.com/a/3292810/10393174>
  • @ViduraPrasangana 我已经阅读了这篇文章,并且我已经在我的项目中制作了这个类,现在如何将它与 XML 文件集成。
  • 可以试试改一下items的顺序吗?
  • @Paritoshpurohit 你能发一张你想要实现的图片吗?

标签: android android-layout android-xml


【解决方案1】:

尝试使用此自定义视图。但是你必须从 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)
}

}

【讨论】:

  • 没那么复杂!你可以简单地使用 xml 来做到这一点
  • @HelloWorld 在我的情况下它不起作用,我不会记下你的答案,无论如何谢谢!
【解决方案2】:

我得到了一个解决方案,我通过以下方式做到了: 我正在编写我的 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”是图像的视图,我正在调用一个名为“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;
    }
  }

事情就是这样,我能够得到需要的东西。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-11
    • 1970-01-01
    • 2012-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-22
    相关资源
    最近更新 更多