Java中使用三角函数
来源:互联网 发布:淘宝店全包 编辑:程序博客网 时间:2024/05/17 00:58
通过一个简单的demo,学习三角函数的简单使用
注:使用三角函数的时候,不要直接Math.sin(x),这样是不对的,或者说,你传的30度,进去就不是30度了。要经过转换。详见代码
画一个播放用的view(一个圆,中间一个三角形)
1、效果图:
2、计算三角形坐标(拍的照片,有点模糊)
3、代码:
PlayIconView
package com.chen.demo;import android.content.Context;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.Path;import android.util.AttributeSet;import android.util.Log;import android.view.View;public class PlayIconView extends View { //圆的画笔 private Paint circlePaint; //圆的边 private Paint strokePaint; private int view_width; private int view_height; //三角形左上角点坐标 private double point_1_x; private double point_1_y; //三角形右下角点坐标 private double point_2_x; private double point_2_y; //三角形右侧点坐标 private double point_3_x; private double point_3_y; private Paint trianglePaint; private Path trianglePath; public PlayIconView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } public PlayIconView(Context context) { super(context); init(); } public PlayIconView(Context context, AttributeSet attrs) { super(context, attrs); init(); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); Log.e("chen", "onSizeChanged"); view_width = w; view_height = h; calculateCoordinate(); } /** * 计算坐标 */ private void calculateCoordinate() { point_1_x = (1 / 2d - 1 / (4 * Math.tan(60 * Math.PI / 180))) * view_width; point_1_y = view_width / 4d; point_2_x = point_1_x; point_2_y = view_width * 3 / 4d; point_3_x = (1 / 2d + 1 / (4 * Math.sin(60 * Math.PI / 180))) * view_width; point_3_y = view_height / 2d; trianglePath.moveTo((float) point_1_x, (float) point_1_y); trianglePath.lineTo((float) point_2_x, (float) point_2_y); trianglePath.lineTo((float) point_3_x, (float) point_3_y); trianglePath.close(); } /** * 初始化 */ private void init() { //背景圆画笔 circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG); circlePaint.setStyle(Paint.Style.FILL); circlePaint.setColor(0x33000000); //圆边画笔 strokePaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG); strokePaint.setStyle(Paint.Style.STROKE); strokePaint.setStrokeWidth(1); strokePaint.setColor(0xccffffff); //三角形画笔 trianglePaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG); trianglePaint.setStyle(Paint.Style.FILL); trianglePaint.setColor(0xffffffff); //闭合区间路径 trianglePath = new Path(); } @Override protected void onDraw(Canvas canvas) { canvas.drawCircle(view_width / 2f, view_height / 2f, view_width / 2f, circlePaint); canvas.drawCircle(view_width / 2f, view_height / 2f, view_width / 2f - 1, strokePaint); canvas.drawPath(trianglePath, trianglePaint); }}
4、布局文件中使用:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@mipmap/ic_launcher" > <com.chen.demo.PlayIconView android:id="@+id/play_icon" android:layout_width="40dp" android:layout_height="40dp" android:layout_centerInParent="true" /></RelativeLayout>
5、Activity:
package com.chen.demo;import android.app.Activity;import android.os.Bundle;import android.view.Window;public class MainActivity extends Activity { private PlayIconView play_icon; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); play_icon = (PlayIconView) findViewById(R.id.play_icon); }}
0 0
- Java中使用三角函数
- Java中使用反三角函数
- PB6.5中使用反三角函数
- C++中三角函数
- J2ME中查表法使用三角函数
- J2ME中查表法使用三角函数
- C#三角函数使用
- J2ME中查表法使用三角函数
- logo 注意使用三角函数
- javascript三角函数的使用
- Java 中的三角函数
- 三角函数
- 三角函数
- 三角函数
- 三角函数
- 三角函数
- 三角函数
- 三角函数
- Linux下启动java程序的通用脚本
- React native 打包发布
- java类加载器
- hdu5890 bitset优化0/1背包
- Java内存区域和GC机制
- Java中使用三角函数
- c++简单的ATL COM开发和调用实例
- 欢迎使用CSDN-markdown编辑器
- AllenZhao Java项目中读取properties文件,以及六种获取路径的方法
- WSGI 简介
- Java数组-选择排序-排序的性能问题
- 【单片机】DS1302
- 解决 网页本地调试通过,上传到服务器上后运行不加载JavaScript的问题
- rest与soap