Android 如何创建包含Imageview和textview的圆形布局?

Android 如何创建包含Imageview和textview的圆形布局?,android,android-layout,Android,Android Layout,我必须创建一个包含ImageView和TextView的framelayout。如何创建圆形布局,使其如图所示。我尝试在布局的背景中添加圆形,但不起作用 我有一个类做同样的工作,请检查此类以供参考在包中添加此类并使用此类 公共类RoundImageView扩展了ImageView{ public RoundedImageView(Context context) { super(context); } public RoundedImageView(Context context, A

我必须创建一个包含ImageView和TextView的framelayout。如何创建圆形布局,使其如图所示。我尝试在布局的背景中添加圆形,但不起作用


我有一个类做同样的工作,请检查此类以供参考在包中添加此类并使用此类 公共类RoundImageView扩展了ImageView{

public RoundedImageView(Context context) {
    super(context);
}

public RoundedImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public RoundedImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onDraw(Canvas canvas) {

    Drawable drawable = getDrawable();

    if (drawable == null) {
        return;
    }

    if (getWidth() == 0 || getHeight() == 0) {
        return;
    }

    Bitmap b = ((BitmapDrawable) drawable).getBitmap();
    if (b == null) {
        return;
    }
    Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);


    if(bitmap ==null)
    {
        return;
    }
    int w = getWidth(), h = getHeight();

    Bitmap roundBitmap = getCroppedBitmap(bitmap, w);
    canvas.drawBitmap(roundBitmap, 0, 0, null);

}

public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
    Bitmap sbmp;

    if (bmp.getWidth() != radius || bmp.getHeight() != radius) {
        float smallest = Math.min(bmp.getWidth(), bmp.getHeight());
        float factor = smallest / radius;
        sbmp = Bitmap.createScaledBitmap(bmp,
                (int) (bmp.getWidth() / factor),
                (int) (bmp.getHeight() / factor), false);
    } else {
        sbmp = bmp;
    }

    Bitmap output = Bitmap.createBitmap(radius, radius, Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xffa19774;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, radius, radius);

    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    paint.setDither(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(Color.parseColor("#BAB399"));
    canvas.drawCircle(radius / 2 + 0.7f, radius / 2 + 0.7f,
            radius / 2 + 0.1f, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(sbmp, rect, rect, paint);

    return output;
}
}

并使用这种类型的XML进行设计

<com.packagename.RoundedImageView
    android:id="@+id/odd_bubble"
    android:layout_width="50dip"
    android:layout_height="50dip"
    android:layout_alignParentLeft="true"
    android:layout_margin="5dip"
    android:src="@drawable/index"
    />

创建自定义视图,并在自定义视图的
onDraw
中使用
canvas.drawText()
放置文本。现在创建一个扩展
FrameLayout
RelativeLayout
的新类,并将上述自定义视图类和RoundedImageView类用作内部类。现在展开父类内的2个视图。现在,您可以获得圆形图像及其下方的文本

仅供参考:您必须将适当的参数传递给
drawText()
方法,以便将其对齐到所需的位置

您可以使用来实现以下功能:



这是您当前拥有的图像,还是您想要实现的图像?这是我想要实现的图像。要获得图像的反射,请尝试此操作。这只是对ImageView进行取整。Textview也应该包含在imageview中,并带有圆角。非常有用,这对我帮助很大,谢谢
   <com.stelladk.arclib.ArcLayout
        android:layout_width="200dp"
        android:layout_height="200dp"
        app:ArcType="inner"
        app:ArcRadius="100dp"
        android:background="@drawable/scenery">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:text="Change"
            android:background="@color/semiwhite"
            android:gravity="center"
            android:layout_gravity="bottom"/>
    </com.stelladk.arclib.ArcLayout>