如何在自定义视图的中心添加一个图像视图?
How to add a imageView in the centre of a customView?
我有一个圆形的自定义视图。这是我的 CircleView 的代码:
public class CircleView extends View {
private static final int START_ANGLE_POINT = 90;
private final Paint paint;
private final RectF rect;
private float angle;
public CircleView(Context context, AttributeSet attrs) {
super(context, attrs);
final int strokeWidth = 40;
paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(strokeWidth);
//Circle color
paint.setColor(Color.RED);
rect = new RectF(strokeWidth, strokeWidth, 1000 + strokeWidth, 1000 + strokeWidth);
//Initial angle is zero
angle = 0;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawArc(rect, START_ANGLE_POINT, angle, false, paint);
}
public float getAngle() {
return angle;
}
public void setAngle(float angle) {
this.angle = angle;
} }
这是我在 activity 的 xml 布局中声明它的方式:
<com.my_package.ui.recording.CircleView
android:id="@+id/circleView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
所有标准的东西。这是我的自定义图片的样子
现在,我想在circleView的中心放置一个imageView?有谁知道我怎样才能做到这一点?
这就是我希望最终得到的理想结果:
提前致谢。
如果您不打算使用 ImageView
并且真的只想在中心绘制位图,那么请查看 canvas' drawBitmap 方法。这将允许你画出你想要的however/wherever。
我有一个圆形的自定义视图。这是我的 CircleView 的代码:
public class CircleView extends View {
private static final int START_ANGLE_POINT = 90;
private final Paint paint;
private final RectF rect;
private float angle;
public CircleView(Context context, AttributeSet attrs) {
super(context, attrs);
final int strokeWidth = 40;
paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(strokeWidth);
//Circle color
paint.setColor(Color.RED);
rect = new RectF(strokeWidth, strokeWidth, 1000 + strokeWidth, 1000 + strokeWidth);
//Initial angle is zero
angle = 0;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawArc(rect, START_ANGLE_POINT, angle, false, paint);
}
public float getAngle() {
return angle;
}
public void setAngle(float angle) {
this.angle = angle;
} }
这是我在 activity 的 xml 布局中声明它的方式:
<com.my_package.ui.recording.CircleView
android:id="@+id/circleView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
所有标准的东西。这是我的自定义图片的样子
现在,我想在circleView的中心放置一个imageView?有谁知道我怎样才能做到这一点?
这就是我希望最终得到的理想结果:
提前致谢。
如果您不打算使用 ImageView
并且真的只想在中心绘制位图,那么请查看 canvas' drawBitmap 方法。这将允许你画出你想要的however/wherever。