您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇“Android如何使用贝塞尔曲线画心形”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Android如何使用贝塞尔曲线画心形”文章吧。
1. 创建一个Activity
RelativeLayout container = findViewById(R.id.download_container); DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); deviceWidth = metrics.widthPixels; deviceHeight = metrics.heightPixels; Circle circle = new Circle(this, deviceWidth / 2, deviceHeight / 2, deviceWidth / 8); Line line = new Line(this, deviceWidth / 2, deviceHeight / 2, deviceWidth / 8); container.addView(line);
2. 创建一个自定义的View
import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.graphics.PointF; import android.view.View; public class Line extends View { private Paint mPaint; private PointF startPointF; private PointF endPointF; private PointF controlPointF1, controlPointF2; private PointF startPointF2; private PointF endPointF2; private PointF controlPointF3, controlPointF4; public Line(Context context, float x, float y, float radius) { super(context); double d = (2 * Math.sqrt(2) - 1); this.startPointF = new PointF(x, y - radius); this.endPointF = new PointF(x, y + radius / 10); this.controlPointF1 = new PointF(x, (float) (y - d * radius)); this.controlPointF2 = new PointF((float) (x + d * radius), (float) (y - d * radius)); this.startPointF2 = new PointF(x, y - radius); this.endPointF2 = new PointF(x, y + radius / 10); this.controlPointF3 = new PointF(x, (float) (y - d * radius)); this.controlPointF4 = new PointF((float) (x - d * radius), (float) (y - d * radius)); this.mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); this.mPaint.setColor(Color.WHITE); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //绘制贝塞尔曲线 Path path = new Path(); path.moveTo(startPointF.x, startPointF.y); path.cubicTo(controlPointF1.x, controlPointF1.y, controlPointF2.x, controlPointF2.y, endPointF.x, endPointF.y); canvas.drawPath(path, mPaint); path.moveTo(startPointF2.x, startPointF2.y); path.cubicTo(controlPointF3.x, controlPointF3.y, controlPointF4.x, controlPointF4.y, endPointF2.x, endPointF2.y); canvas.drawPath(path, mPaint); } }
运行效果
以上就是关于“Android如何使用贝塞尔曲线画心形”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。