android 实现3d曲面效果

实现效果:

 

实现步骤:
 <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>

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值