android - 在圆形 ImageView 外创建边框

标签 android border android-imageview

我创建了一个圆形的 ImageView,但我需要在图像外添加一个边框。

代码如下:

Bitmap circleBitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);   
BitmapShader shader = new BitmapShader (bitmap,  TileMode.CLAMP, TileMode.CLAMP);

Paint paint = new Paint();
paint.setShader(shader);
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);      
paint.setColor(Color.parseColor("#BAB399"));

Canvas c = new Canvas(circleBitmap);        
c.drawARGB(0, 0, 0, 0);
c.drawCircle(50, 40,40, paint);

谁能帮我在圆形图像外创建一个边框?

最佳答案

首先你创建一个像这样的圆形,

    <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
    <gradient android:startColor="#333440" android:endColor="#333440"
        android:angle="270"/>
</shape>

然后添加一个相对布局,并为其添加一个imageview。将其排列到相对布局的中心。并将此圆形设置为Imageview的背景。然后将您的圆形imageview放在先前添加的imageview上方。也将其排列到中心。通过更改您的圆形 imageview 边距,您将获得所需的边框效果。

最终代码,

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="30dp">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/imageView14"
            android:background="@drawable/circ"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView15"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:src="@drawable/linked"
            android:layout_margin="5dp" />
    </RelativeLayout>
</LinearLayout>

enter image description here

关于android - 在圆形 ImageView 外创建边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16770693/

相关文章:

webkit - 带有 border-radius 的 img 覆盖其边界

Android 最大观看次数?

android - PNG 未显示在 ImageView 中

java - 为什么java从属对象不能访问父域?

java - 绘制新 Canvas 是否会删除旧 Canvas 上的内容?

java - 使用 xmlpullparser 解析 xml 时在列表中获取相同的值

Python/QT - 我该怎么做才能在一系列小部件之间获得 4 个像素边框?

css - 单击时删除输入突出显示的框

android - 如何允许用户在android中设置图像

android - 即使下载了安装工具,也出现未安装错误