如何利用Thread製作簡單的android動畫

2022-03-10 04:58:07 字數 1228 閱讀 2630

最近按照老闆要求要做乙個簡單動畫,內容可以簡單概括為乙個箭頭在螢幕中心自動旋轉。我知道android api裡面有animation類,它已經提供了rotation的函式。可是,老闆要求不用它的api。所以我的製作如下:

1.先了解一下執行緒(thread)

多執行緒指的是乙個應用程式當中有多個執行緒,執行緒是並列執行的,一起搶占cpu。我們平時寫的主函式就是乙個執行緒,如果我們繼承了thread類,則又開發了乙個新的執行緒。

它有兩種實現方法:

乙個是繼承thread類,並override裡面的run()函式。

另乙個方法是implement runnable(),並override裡面的run()函式。

如下:

class mythread extends

thread

}//啟動執行緒

mythread.start();

另一種方法:

classprimerunimplementsrunnable

}//啟動執行緒

primerunp =newprimerun();

newthread(p).start();

我利用的是thread類中的方法:mythread.sleep();

這個方法可以讓系統每隔一段時間執行一次,我設定的間隔時間是100ms。而每次我畫的指標都會變動它的位置,因此連起來看上去就是它在自己轉動。**如下:

private

final runnable movethread = new

runnable()

catch

(interruptedexception e)

postinvalidate();//每變動完一次之後重新繪圖。}}

};new thread(movethread).start();

接下來寫畫直線的**:需要建立乙個myview來繼承view,並override裡面的ondraw()函式

public

class myview extends

view}}

//vector函式用來畫乙個箭頭,是自己定義的函式如下:

public void vector(float x1,float y1,float x2,float y2,paint paint,canvas canvas)

利用js製作簡單的動態日曆

doctype html en utf 8 title title calendar item item hover style head calendar var year 2020 var month 12 從1900年1月1日到當前年份月份上乙個月的總天數 var totaldays 0 目標...

利用python製作簡單計算器

利用python的定義函式,加上if的條件語句進行程式設計 def add x,y 相加 return x y defsubtract x,y 相減 return x y defmultiply x,y 相乘 return x y defdivide x,y 相除 return x y 使用者輸入 ...

如何利用git製作和提交patch

不過在git中,我們沒有必要直接使用diff和patch來做補丁,這樣做既危險又麻煩。git提供了兩種簡單的patch方案。一是用git diff生成的標準patch,二是git format patch生成的git專用patch 我們可以首先用git diff製作乙個patch。本文示例的工作目錄...