Android 最简单圆形图片,带边框

public class XCRoundRectImageView extends ImageView{
    
    public XCRoundRectImageView(Context context) {
        this(context,null);  
    }  
  
    public XCRoundRectImageView(Context context, AttributeSet attrs) {  
        this(context, attrs,0);  
    }  
  
    public XCRoundRectImageView(Context context, AttributeSet attrs, int defStyle) {  
        super(context, attrs, defStyle);
    }
    /**
     * 绘制圆形图片
     * @author caizhiming
     */
    @Override  
    protected void onDraw(Canvas canvas) {
        Path mPath = new Path();
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        mPath.addCircle(width/2, height/2, height/2, Path.Direction.CW);
        canvas.clipPath(mPath);//将画布裁剪成圆形
        super.onDraw(canvas);

        //画上圆形边框
        Paint paint=new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(10);
        paint.setColor(Color.RED);
        canvas.drawPath(mPath,paint);
    }

}

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要在Android Studio中的圆形边框中添加图,您可以按照以下步骤进行操作: 1. 在res/drawable文件夹中创建一个XML文件(例如circlular_border.xml),并添加以下代码: ``` <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="50dp"/> <stroke android:width="2dp" android:color="@color/black"/> <solid android:color="@color/white"/> </shape> ``` 2. 在布局文件中添加ImageView控件,并为其设置背景为刚刚创建的XML文件: ``` <ImageView android:id="@+id/imageView" android:layout_width="100dp" android:layout_height="100dp" android:background="@drawable/circular_border" /> ``` 3. 在Java代码中加载您想要添加的图,并将其设置为ImageView的源(src): ``` ImageView imageView = findViewById(R.id.imageView); imageView.setImageResource(R.drawable.your_image_file); ``` 这样,您就可以在圆形边框中添加图了。请注意,如果您想要实现圆形ImageView,可以使用CircleImageView库。 ### 回答2: 在Android Studio中添加圆形边框并在其中添加图的步骤如下: 1. 首先,在drawable文件夹下创建一个圆形边框的XML文件。例如,创建一个名为circle_border.xml的文件。 2. 在circle_border.xml文件中,使用shape标签定义一个圆形的形状,并设置边框的颜色和宽度。例如,可以使用solid标签设置边框的颜色,stroke标签设置边框的宽度。同时,设置一个size属性来限定圆形的大小。以下是circle_border.xml文件的示例代码: ```xml <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="#FF0000" /> <stroke android:color="#000000" android:width="2dp" /> <size android:width="100dp" android:height="100dp" /> </shape> ``` 3. 在布局文件中,添加一个ImageView元素,并为它设置drawable属性为前面创建的circle_border.xml文件。例如,可以将drawable属性设置为@drawable/circle_border。以下是一个示例代码: ```xml <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/circle_border" /> ``` 4. 最后,在Java代码中,使用Glide或者Picasso等图加载库来加载要显示的图,并将其设置到ImageView元素中。以下是一个使用Glide库加载图的示例代码: ```java ImageView imageView = findViewById(R.id.imageView); Glide.with(this) .load(R.drawable.your_image) .into(imageView); ``` 以上就是在Android Studio中添加圆形边框并添加图的步骤。通过创建圆形边框的XML文件,并将该边框作为ImageView的drawable属性,再加载图到ImageView中,即可实现圆形边框中添加图的效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值