实现效果:
实现步骤:
<1>自定义view画弧形背景
<2>使用FrameLayout布局特性
public class MyView extends View {
private Paint paint;
private Path path;
private int rectfHeight = 100;// 弧线区域高
private int color = Color.parseColor("#4BAE4F");// 蓝色
private Path path2;
/**
* @param context
*/
public MyView(Context context) {
this(context, null);
}
public MyView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
// TODO Auto-generated constructor stub
}
public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// 获取自定义属性值
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.myview_color);
rectfHeight = array.getInteger(R.styleable.myview_color_arc_height, rectfHeight);// 弧线高度
color = array.getColor(R.styleable.myview_color_paint_color, color);// 弧线填充色
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
super.onLayout(changed, l, t, r, b);
initView();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(path, paint);// 绘制弧线
canvas.drawPath(path2, paint);// 绘制弧线
}
/**
* @param 参数
* @return void 返回类型
*/
private void initView() {
// 设置画笔
paint = new Paint();
paint.setAntiAlias(true);// 抗锯齿
paint.setColor(color);// 设置颜色
paint.setStrokeWidth(3);// 线宽
paint.setStyle(Paint.Style.FILL_AND_STROKE); // 填充
// 设置path
path = new Path();
// (1)通过贝塞尔曲线画弧
path.moveTo(0, 0);
path.quadTo(getWidth() / 2, rectfHeight, getWidth(), 0);// 上部弧线区域
path2 = new Path();
path2.moveTo(0, getHeight());
path2.quadTo(getWidth() / 2, getHeight() - rectfHeight, getWidth(), getHeight());// 下部弧线区域
}
attrs.xml自定义属性
<declare-styleable name="myview_color">
<attr name="paint_color" format="color" />
<attr name="arc_height" format="integer" />
</declare-styleable>
使用布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="@color/green" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/image2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="@drawable/abc" />
<ImageView
android:id="@+id/image3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="@drawable/abc" />
<ImageView
android:id="@+id/image4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="@drawable/abc" />
</LinearLayout>
<com.time.time.MyView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
app:arc_height="150"
app:paint_color="@color/green" >
</com.time.time.MyView>
</FrameLayout>