——先畫圓,再畫刻度…
已知風向值,確定N、NE、E、SE、S、SW、W、NW中的一個:
String direction = "Unknown";
if (degrees >= 337.5 || degrees < 22.5) {
direction = "N";
}
else if (degrees >= 22.5 && degrees < 67.5) {
direction = "NE";
}
else if (degrees >= 67.5 && degrees < 112.5) {
direction = "E";
}
else if (degrees >= 112.5 && degrees < 157.5) {
direction = "SE";
}
else if (degrees >= 157.5 && degrees < 202.5) {
direction = "S";
}
else if (degrees >= 202.5 && degrees < 247.5) {
direction = "SW";
}
else if (degrees >= 247.5 && degrees < 292.5) {
direction = "W";
}
else if (degrees >= 292.5 && degrees < 337.5) {
direction = "NW";
}
已知degree,畫風向標(盤)
過程不難,注意細節
1. 自定義View —— MyView
首先是一些構造方法,然後就是重寫onDraw()和onMeasure()方法了。
public class MyView extends View {
//Constructor 1
public MyView(Context context)
{
super(context);
}
//Constructor 2
public MyView(Context context, AttributeSet attrs)
{
super(context , attrs);
}
//Constructor 3
public MyView(Context context , AttributeSet attrs , int defaultStyle){
super(context , attrs , defaultStyle);
}
//onDraw()
@Override
public void onDraw(Canvas canvas)
{
super(canvas);
//Do Some Draw.
}
//onMeasure
@Override
public void onMeasure(int widthMeasureSpec ,int heightMeasureSpec ){
//getSize(29 ... 0) & Mode(31 30)
}
}
2. 在佈局文件中加上
<com.xxxxx.MyView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
3. init()
在對應的Activity中,init
private void init(){
final MyView myView = new MyView(this);
myView.invalidate();//重畫
}
4. 在對應的CreateView中添加MyView,更新數據
由於需要更新,所以在CreateView時,需要有myView。所以,在佈局文件中,給MyView加一個id
android:id = "@+id/direction"
然後,獲取該view
public View onCreateView(LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.fragment_xxx , container , false);
······
private MyView myView;
myView = (MyView) rootView.findViewById(R.id.direction);
return rootView;
}
更新風向時,調用MyView中的update()方法:
private float direction;
public void update(float dir)
{
direction = (float) Math.toRadians(dir); //度數變爲弧度數,用於三角函數計算
invalidate();
}
具體用了一些Math中的
方法:
Math.sin/cos/tan/asin/atan/acos/atan2
Math.abs()
Math.toRadians();
Math.toDegrees();
特殊值:
Math.PI
Math.E
tips
判斷浮點數等於0
直接用 == 嗎?
在一個判斷sin函數值的時候,sinπ的值並不爲0,而是一個很小很小的數,小到還是大於0的浮點數。
所以,在比較是否爲0,用 == 顯然是不成立的,但是我需要它成立。關於數學方面的代碼,這樣的情況會有很多。
所以判斷一個浮點數是否等於0,是需要根據精度來確定的,要求精度高,也可以用 new BigDecimal();一般精度要求 ,可以自己設置。
對於判斷sinπ這樣的,sinπ < 0.000001 等條件就可以了。
判斷浮點數是否相等呢,做減法啦,再判斷0
Java 浮點數賦值:
這裏要注意一點,Java中對浮點數的賦值一般需要強制類型轉換,就算是用小數賦值,也需要在末尾加上字母f
direction = (float) Math.toRadians(dir); float pi = 3.1415926f;
git命令
git log:查看git日誌
git reset –hard/soft commitID:
回退到某一個commitID的狀態(hard指代碼也一併回到那個狀態,soft指代碼不用回退)。
git remote -v :查看遠程master的地址