简体   繁体   English

Android 自定义组件视图,带圆角

[英]Android Custom Component View w/Rounded Corners

I'm trying to create a View with rounded corners (and a background color of choice) that I can reuse with different background colors;我正在尝试创建一个带有圆角(和选择的背景颜色)的视图,我可以在不同的背景中重复使用它 colors; hard to explain, so here's my code:很难解释,所以这是我的代码:

/app/src/com/packagename/whatever/CustomDrawableView.java /app/src/com/packagename/whatever/CustomDrawableView.java


package com.packagename.whatever;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.PaintDrawable;
import android.util.AttributeSet;
import android.view.View;

public class CustomDrawableView extends View {
    private PaintDrawable mDrawable;
    int radius;

    private void init(AttributeSet attrs) {
        TypedArray a = getContext().obtainStyledAttributes(attrs,R.styleable.RoundedRect);
        radius = a.getInteger(R.styleable.RoundedRect_radius, 0);
    }

    public CustomDrawableView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);

        mDrawable = new PaintDrawable();
    }

    protected void onDraw(Canvas canvas) {
        mDrawable.setCornerRadius(radius);
        mDrawable.draw(canvas);
    }
}

Here's the XML to display the custom component: /app/res/layout/test.xml这是用于显示自定义组件的 XML: /app/res/layout/test.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ny="http://schemas.android.com/apk/res/com.packagename.whatever"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    android:padding="10dp">

    <com.packagename.whatever.CustomDrawableView
        android:id="@+id/custom"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#b80010"
        ny:radius="50"
    />

</LinearLayout>

I'm wanting the red box to have 50px rounded corners, but as you'll see, it does not:我希望红色框有 50px 圆角,但正如您所见,它没有:

没有圆角的红框

The idea is that I could easily change the background color in the XML and automatically have a nice View with rounded corners, without having to create multiple drawables.这个想法是我可以轻松地更改 XML 中的背景颜色,并自动获得带有圆角的漂亮视图,而无需创建多个可绘制对象。

Thanks for the help!谢谢您的帮助!

You need to set your corner radius and color into the background drawable.您需要将角半径和颜色设置为背景可绘制对象。

Here is one way that would work.这是一种可行的方法。 Grab the color you set in android:background, then use it to create a new drawable that you set into the background in the constructor.获取您在 android:background 中设置的颜色,然后使用它来创建您在构造函数中设置为背景的新可绘制对象。 This will work as long as you only set android:background to a color value.只要您仅将 android:background 设置为颜色值,这将起作用。

   public CustomDrawableView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);

        // pull out the background color
        int color = attrs.getAttributeIntValue("http://schemas.android.com/apk/res/android", "background", 0xffffffff);

        // create a new background drawable, set the color and radius and set it in place
        mDrawable = new PaintDrawable();
        mDrawable.getPaint().setColor(color);
        mDrawable.setCornerRadius(radius);
        setBackgroundDrawable(mDrawable);
    }

If you override onDraw, make sure you call super.onDraw(canvas) first to get the background drawn.如果您覆盖 onDraw,请确保先调用 super.onDraw(canvas) 以绘制背景。

given a simple shapedrawable like this:给定一个简单的 shapedrawable 像这样:

public ShapeDrawable Sd(int s){

float[] outerR = new float[] { 12, 12, 12, 12, 12, 12, 12, 12 };
ShapeDrawable mDrawable = new ShapeDrawable(new RoundRectShape(outerR, null,null));

            mDrawable.getPaint().setColor(s);
return mDrawable;
}

you can do the following:您可以执行以下操作:

    LinearLayout l=(LinearLayout) findViewById(R.id.testLayout);
l.setBackgroundDrawable(Sd(0xff74AC23));

where the 12's represent the radius.其中 12 代表半径。 you could apply this to any view for a background drawable.您可以将其应用于背景可绘制的任何视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM