我需要使用此类在ImageView
周围绘制一个红色边框:
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ImageView;
public class RoundedImageView extends 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);
}
protected void onDraw(Canvas canvas) {
float radius = 45.0f;
Paint bp= new Paint();
bp.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
bp.setColor(Color.RED);
bp.setStrokeWidth(16);
Path clipPath = new Path();
RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight());
clipPath.addRoundRect(rect, radius, radius, Path.Direction.CW);
canvas.drawRoundRect(rect, radius, radius, bp);
canvas.clipPath(clipPath);
super.onDraw(canvas);
}
}
但它似乎并没有画出边界?我不知道自己做错了什么?
任何人都可以帮忙吗?
由于
答案 0 :(得分:1)
好的,这就是我最终做的事情:
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.ImageView;
public class RoundedImageView extends ImageView {
private static final int PADDING = 8;
private static final float STROKE_WIDTH = 16f;
private Paint mBorderPaint;
public RoundedImageView(Context context) {
this(context, null);
}
public RoundedImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
setPadding(PADDING, PADDING, PADDING, PADDING);
}
public RoundedImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initBorderPaint();
}
private void initBorderPaint() {
mBorderPaint = new Paint();
mBorderPaint.setAntiAlias(true);
mBorderPaint.setStyle(Paint.Style.STROKE);
mBorderPaint.setColor(Color.WHITE);
mBorderPaint.setStrokeWidth(STROKE_WIDTH);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawRect(PADDING, PADDING, getWidth() - PADDING, getHeight() - PADDING, mBorderPaint);
}
}
它没有按照要求给出圆角,但并非真正必要。这只会在图像周围创建一个边框。
答案 1 :(得分:1)
在drawable文件夹中创建一个命名border.xml的xml并添加以下代码:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@android:color/transparent" />
<stroke android:width="0.7dip" android:color="#808080"/>
</shape>
现在将此添加到您的框架布局中,并将您的imageview放在框架布局中,您就完成了。
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/border"
android:padding="3dp" >
<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="110dp"
android:layout_margin="1dp" />
</FrameLayout>
希望它有所帮助。 谢谢:))
答案 2 :(得分:0)
在drawable文件夹中创建一个命名为border.xml的xml,并添加以下代码:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size android:height="100dp" android:width="100dp"/>
<solid android:color="@android:color/transparent" />
<stroke android:width="5dip" android:color="#f2db2f"/>
</shape>
在您的布局中添加以下内容:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:foregroundGravity="center|top"
android:foreground="@drawable/border"
android:layout_weight="4">
<ImageView
android:id="@+id/dog"
android:scaleType="centerCrop"
android:src="@drawable/gears"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>