我想让任何图像从我的ImageView是圆形的边界。
我搜索了一下,但没有找到任何有用的信息(我尝试的任何方法都不管用)。
如何通过XML实现这一点: 创建一个ImageView与某些src,并使它与边界圆形?
我想让任何图像从我的ImageView是圆形的边界。
我搜索了一下,但没有找到任何有用的信息(我尝试的任何方法都不管用)。
如何通过XML实现这一点: 创建一个ImageView与某些src,并使它与边界圆形?
当前回答
创建一个CustomImageview,然后简单地覆盖它的onDraw()方法如下:
@Override
protected void onDraw(Canvas canvas) {
float radius = this.getHeight()/2;
Path path = new Path();
RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight());
path.addRoundRect(rect, radius, radius, Path.Direction.CW);
canvas.clipPath(path);
super.onDraw(canvas);
}
如果你想要的代码自定义小部件以及:-
CircularImageView.java
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
import androidx.annotation.Nullable;
public class CircularImageView extends ImageView {
private Drawable image;
public CircularImageView(Context context) {
super(context);
init(null, 0);
}
public CircularImageView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(attrs, 0);
}
public CircularImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
float radius = this.getHeight()/2;
Path path = new Path();
RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight());
path.addRoundRect(rect, radius, radius, Path.Direction.CW);
canvas.clipPath(path);
super.onDraw(canvas);
}
private void init(AttributeSet attrs, int defStyle) {
TypedArray a = Utils.CONTEXT.getTheme().obtainStyledAttributes(attrs, R.styleable.CircularImageView, 0, 0);
try {
image = a.getDrawable(R.styleable.CircularImageView_src);
} finally {
a.recycle();
}
this.setImageDrawable(image);
}
}
此外,在res/attrs.xml中添加以下代码来创建所需的属性
<declare-styleable name="CircularImageView">
<attr name="src" format="reference" />
</declare-styleable>
其他回答
您不需要任何第三方库。
你可以在材质中使用ShapeableImageView。
implementation 'com.google.android.material:material:1.2.0'
style.xml
<style name="ShapeAppearanceOverlay.App.CornerSize">
<item name="cornerSize">50%</item>
</style>
在布局
<com.google.android.material.imageview.ShapeableImageView
android:layout_width="100dp"
android:layout_height="100dp"
app:srcCompat="@drawable/ic_profile"
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.App.CornerSize"
/>
你们可以看到
https://developer.android.com/reference/com/google/android/material/imageview/ShapeableImageView
或者这个
https://medium.com/android-beginners/shapeableimageview-material-components-for-android-cac6edac2c0d
如果你在应用中使用材质设计,那么就使用这个
<com.google.android.material.card.MaterialCardView
android:layout_width="75dp"
android:layout_height="75dp"
app:cardCornerRadius="50dp"
app:strokeWidth="1dp"
app:strokeColor="@color/black">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/circular_image"
android:scaleType="fitCenter"
android:src="@drawable/your_img" />
</com.google.android.material.card.MaterialCardView>
在glide库和RoundedBitmapDrawableFactory类的帮助下,很容易实现。您可能需要创建圆形占位符映像。
滑翔V4:
Glide.with(context).load(url).apply(RequestOptions.circleCropTransform()).into(imageView);
滑翔V3:
Glide.with(context)
.load(imgUrl)
.asBitmap()
.placeholder(R.drawable.placeholder)
.error(R.drawable.placeholder)
.into(new BitmapImageViewTarget(imgProfilePicture) {
@Override
protected void setResource(Bitmap resource) {
RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(context.getResources(),
Bitmap.createScaledBitmap(resource, 50, 50, false));
drawable.setCircular(true);
imgProfilePicture.setImageDrawable(drawable);
}
});
对于Picasso RoundedTransformation,这是一个非常好的解决方案,提供了一个额外的选择,在顶部或底部边缘的图像。
另一个想法是使用ImageView的clipToOutline属性。
这是一个布局示例:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Simple view to draw borders for an image,
borders will be rounded because of the oval-shaped background. -->
<View
android:id="@+id/v_border"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/shape_border"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- Image itself: fits the border view,
a margin serves as a border width;
the key point here - is a background shape which will clip the view to its forms. -->
<ImageView
android:id="@+id/iv_image"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="4dp"
android:background="@drawable/shape_oval"
android:src="@mipmap/ic_launcher"
app:layout_constraintBottom_toBottomOf="@+id/v_border"
app:layout_constraintEnd_toEndOf="@+id/v_border"
app:layout_constraintStart_toStartOf="@+id/v_border"
app:layout_constraintTop_toTopOf="@+id/v_border" />
</androidx.constraintlayout.widget.ConstraintLayout>
这是我们的shape_border可绘制对象:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<solid android:color="#FF00FF" />
</shape>
和shape_oval drawable:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" />
你在代码中唯一要做的就是启用clipToOutline属性:
binding.ivImage.clipToOutline = true
当然,您甚至可以使用一些BindingAdapter来避免这一行代码。
如果使用src属性,上述方法似乎不起作用。我所做的是把两个图像视图放在一个框架布局中,一个在另一个上面,就像这样:
<FrameLayout android:id="@+id/frame"
android:layout_width="40dp"
android:layout_height="40dp">
<ImageView android:id="@+id/pic"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/my_picture" />
<ImageView android:id="@+id/circle_crop"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/circle_crop" />
</FrameLayout>
简单地把一个circular_crop.png放在你的可绘制文件夹中,它是你的图像尺寸的形状(在我的例子中是一个正方形),白色背景,中间是一个透明的圆。如果你想要一个正方形imageview,你可以使用这个图像。
下载上面的图片。