【发布时间】:2016-06-30 12:10:07
【问题描述】:
【问题讨论】:
标签: android android-layout uiimageview android-camera
【问题讨论】:
标签: android android-layout uiimageview android-camera
使用下面的自定义 ImageView 类。
public class RoundedImageView extends ImageView {
private Path mMaskPath;
private Paint mMaskPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private int mCornerRadius = 10;
public RoundedImageView(Context context) {
super(context);
init(context);
}
public RoundedImageView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
init(context);
}
public RoundedImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context context) {
ViewCompat.setLayerType(this, ViewCompat.LAYER_TYPE_SOFTWARE, null);
mMaskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
mMaskPaint.setColor(context.getResources().getColor(R.color.transparent));
mCornerRadius = (int) context.getResources().getDimension(R.dimen.image_border_curvature);
}
/**
* Set the corner radius to use for the RoundedRectangle.
*/
public void setCornerRadius(int cornerRadius) {
mCornerRadius = cornerRadius;
generateMaskPath(getWidth(), getHeight());
invalidate();
}
@Override
protected void onSizeChanged(int w, int h, int oldW, int oldH) {
super.onSizeChanged(w, h, oldW, oldH);
if (w != oldW || h != oldH) {
generateMaskPath(w, h);
}
}
private void generateMaskPath(int w, int h) {
mMaskPath = new Path();
mMaskPath.addRoundRect(new RectF(0,0,w,h), mCornerRadius, mCornerRadius, Path.Direction.CW);
mMaskPath.setFillType(Path.FillType.INVERSE_WINDING);
}
@Override
protected void onDraw(Canvas canvas) {
if(canvas.isOpaque()) { // If canvas is opaque, make it transparent
canvas.saveLayerAlpha(0, 0, canvas.getWidth(), canvas.getHeight(), 255, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG);
}
super.onDraw(canvas);
if(mMaskPath != null) {
canvas.drawPath(mMaskPath, mMaskPaint);
}
}
}
在你的 xml 中使用这个 ImageView。
【讨论】:
我使用这个库https://github.com/vinc3m1/RoundedImageView 两年多了,我很高兴。你只需要在你的build.gradle中添加这行代码
dependencies {
compile 'com.makeramen:roundedimageview:2.2.1'
}
并像这样定义您的RoundedImageView 小部件
<com.makeramen.roundedimageview.RoundedImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/imageView1"
android:src="@drawable/photo1"
android:scaleType="fitCenter"
app:riv_corner_radius="30dip"
app:riv_border_width="2dip"
app:riv_border_color="#333333"
app:riv_mutate_background="true"
app:riv_tile_mode="repeat"
app:riv_oval="true" />
你准备好了!
希望能提供帮助!
【讨论】:
您可以将 ImageView 包装在 CardView 中。您可以使用 app:cardCornerRadius 设置拐角半径。就这么简单
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardCornerRadius="5dp"
app:cardElevation="0dp">
<ImageView
android:id="@+id/imgUserHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/photo"/>
</android.support.v7.widget.CardView>
【讨论】:
与其自己尝试,不如使用this 之类的库来满足您的目的。它有不同的形状,效果很好。
依赖于应用程序模块的build.gradle 添加此行:
compile 'com.github.siyamed:android-shape-imageview:0.9.+@aar'
同步 gradle,现在您的应用可以访问该库。现在使用以下代码将其添加到您的布局中:
<com.github.siyamed.shapeimageview.BubbleImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/neo"
app:siArrowPosition="right"
app:siSquare="true"/>
关注库页面以获得更多自定义。
【讨论】:
请在gradle中添加如下依赖并在xml中使用如下
编译'de.hdodenhof:circleimageview:1.3.0'
<de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/profile_image"
android:layout_width="@dimen/pad_120dp"
android:layout_height="@dimen/pad_120dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/icon_profile" />
【讨论】: