最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android自定義View實(shí)現(xiàn)時(shí)鐘效果

 更新時(shí)間:2022年01月12日 08:00:54   作者:仙氣兒飄飄w  
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)時(shí)鐘效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android自定義View實(shí)現(xiàn)時(shí)鐘效果的具體代碼,供大家參考,具體內(nèi)容如下

自定義時(shí)鐘

初學(xué)自定義View,先畫一個(gè)不太成熟的時(shí)鐘(甚至只有秒針)

時(shí)鐘效果

@SuppressLint("DrawAllocation")
public class ClockView extends View {

? ? private final Context mContext;
? ? private Canvas mCanvas;// 畫布
? ? private Paint clockPaint;// 表盤畫筆
? ? private Paint textPaint;// 文字畫筆
? ? private Paint secondPaint;// 秒針畫筆
? ? private int x,y;// 表中心坐標(biāo)
? ? private Thread refreshThread;
? ? private boolean isStop = false;
? ? // 用于獲取當(dāng)前秒數(shù)
? ? private ?Date currentDate;
? ? private SimpleDateFormat sp = new SimpleDateFormat("ss");

? ? @SuppressLint("HandlerLeak")
? ? private final Handler mHandler = new Handler(){
? ? ? ? @Override
? ? ? ? public void handleMessage(@NonNull Message msg) {
? ? ? ? ? ? super.handleMessage(msg);
? ? ? ? ? ? if(msg.what == 0){
? ? ? ? ? ? ? ? invalidate();//每隔一秒刷新一次
? ? ? ? ? ? }
? ? ? ? }
? ? };

? ? public ClockView(Context context) {
? ? ? ? this(context,null);
? ? }

? ? public ClockView(Context context, @Nullable AttributeSet attrs) {
? ? ? ? this(context,null,0);
? ? }

? ? public ClockView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
? ? ? ? this(context,null,0,0);
? ? }

? ? public ClockView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
? ? ? ? super(context, attrs, defStyleAttr, defStyleRes);
? ? ? ? this.mContext = context;
? ? }


? ? @Override
? ? protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
? ? ? ? int widthMeasure;
? ? ? ? // 設(shè)置寬高一致
? ? ? ? if(widthMeasureSpec > heightMeasureSpec){
? ? ? ? ? ? super.onMeasure(heightMeasureSpec, heightMeasureSpec);
? ? ? ? ? ? widthMeasure ?= getMeasuredHeight();
? ? ? ? }else{
? ? ? ? ? ? super.onMeasure(widthMeasureSpec, widthMeasureSpec);
? ? ? ? ? ? widthMeasure = getMeasuredWidth();
? ? ? ? }
? ? ? ? initPaint(widthMeasure);
? ? }

? ? private void initPaint(int width){
? ? ? ? // 表盤畫筆
? ? ? ? clockPaint = new Paint();
? ? ? ? clockPaint.setColor(mContext.getColor(R.color.black));// 顏色
? ? ? ? clockPaint.setAntiAlias(true);// 抗鋸齒
? ? ? ? clockPaint.setStyle(Paint.Style.STROKE);// 樣式
? ? ? ? clockPaint.setStrokeWidth(width/80f);// 寬度

? ? ? ? //字體畫筆
? ? ? ? textPaint = new Paint();
? ? ? ? textPaint.setColor(mContext.getColor(R.color.black));
? ? ? ? textPaint.setAntiAlias(true);
? ? ? ? textPaint.setStyle(Paint.Style.FILL);
? ? ? ? textPaint.setTextSize(width/16f);
? ? ? ? textPaint.setTextAlign(Paint.Align.CENTER);

? ? ? ? // 秒針畫筆
? ? ? ? secondPaint = new Paint();
? ? ? ? secondPaint.setColor(mContext.getColor(R.color.red));
? ? ? ? secondPaint.setAntiAlias(true);
? ? ? ? secondPaint.setStyle(Paint.Style.FILL);
? ? ? ? secondPaint.setStrokeWidth(5f);
? ? }

? ? @Override
? ? protected void onDraw(Canvas canvas) {
? ? ? ? super.onDraw(canvas);

? ? ? ? mCanvas = canvas;

? ? ? ? // 獲取畫布中心坐標(biāo)
? ? ? ? x = getWidth() / 2;
? ? ? ? y = getHeight() / 2;

? ? ? ? // 繪制表盤
? ? ? ? mCanvas.drawCircle(x, y,getWidth()/40f, clockPaint);// 表盤中心
? ? ? ? mCanvas.drawCircle(x, y, x -getWidth()/40f, clockPaint);// 表盤邊框

? ? ? ? // 繪制刻度
? ? ? ? for(int i = 1;i <= 60;i++){
? ? ? ? ? ? mCanvas.rotate(6, x, y);
? ? ? ? ? ? if(i%5 == 0){
? ? ? ? ? ? ? ? // 繪制大刻度
? ? ? ? ? ? ? ? mCanvas.drawLine(x, getWidth()*3/80f, x, getWidth()*5/80f, clockPaint);
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? // 繪制小刻度
? ? ? ? ? ? ? ? mCanvas.drawLine(x, getWidth()*3/80f, x, getWidth()/20f, clockPaint);
? ? ? ? ? ? }
? ? ? ? }

? ? ? ? // 繪制 1-12小時(shí) 字體
? ? ? ? for(int i = 1;i <= 60;i++){
? ? ? ? ? ? if(i%5 == 0){
? ? ? ? ? ? ? ? float x1 = (float) Math.sin(Math.toRadians(6 * i)) * (y * 3 / 4f) + x;
? ? ? ? ? ? ? ? float y1 = y - (float) Math.cos(Math.toRadians(6 * i)) * (y * 3 / 4f) + getWidth()/40f;
? ? ? ? ? ? ? ? mCanvas.drawText(String.valueOf(i/5), x1, y1, textPaint);
? ? ? ? ? ? }
? ? ? ? }

? ? ? ? // 繪制秒針
? ? ? ? currentDate = new Date(System.currentTimeMillis());
? ? ? ? int ss = Integer.parseInt(sp.format(currentDate));// 獲取當(dāng)前秒數(shù)
? ? ? ? // 根據(jù)當(dāng)前秒數(shù) 計(jì)算出秒針的 start 及 end 坐標(biāo)
? ? ? ? float sin = (float) Math.sin(Math.toRadians(6 * ss));
? ? ? ? float cos = (float) Math.cos(Math.toRadians(6 * ss));
? ? ? ? float x0 = x - sin * (y / 10f);
? ? ? ? float y0 = y + cos * (y / 10f);
? ? ? ? float x1 = x + sin * (y * 13 / 20f);
? ? ? ? float y1 = y - cos * (y * 13 / 20f);
? ? ? ? mCanvas.drawLine(x0, y0, x1, y1, secondPaint);
? ? ? ? mCanvas.drawCircle(x, y,getWidth()/80f, secondPaint);
? ? }

? ? @Override
? ? protected void onAttachedToWindow() {
? ? ? ? super.onAttachedToWindow();
? ? ? ? refreshThread = new Thread(){
? ? ? ? ? ? @Override
? ? ? ? ? ? public void run() {
? ? ? ? ? ? ? ? super.run();
? ? ? ? ? ? ? ? while (!isStop){
? ? ? ? ? ? ? ? ? ? SystemClock.sleep(1000);
? ? ? ? ? ? ? ? ? ? mHandler.sendEmptyMessage(0);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? };
? ? ? ? refreshThread.start();
? ? }

? ? @Override
? ? protected void onDetachedFromWindow() {
? ? ? ? super.onDetachedFromWindow();
? ? ? ? mHandler.removeCallbacksAndMessages(null);
? ? ? ? isStop = true;
? ? }

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android實(shí)現(xiàn)Android?APP自動(dòng)更新功能

    Android實(shí)現(xiàn)Android?APP自動(dòng)更新功能

    在移動(dòng)應(yīng)用的全生命周期中,版本迭代和用戶更新體驗(yàn)至關(guān)重要,傳統(tǒng)的做法是依賴?Google?Play?商店強(qiáng)制推送更新,但在某些場景下,我們需要更即時(shí)地控制更新流程,所以本文給大家介紹了Android實(shí)現(xiàn)Android?APP自動(dòng)更新功能,需要的朋友可以參考下
    2025-04-04
  • Android編程實(shí)現(xiàn)ImageView圖片拋物線動(dòng)畫效果的方法

    Android編程實(shí)現(xiàn)ImageView圖片拋物線動(dòng)畫效果的方法

    這篇文章主要介紹了Android編程實(shí)現(xiàn)ImageView圖片拋物線動(dòng)畫效果的方法,實(shí)例分析了Android實(shí)現(xiàn)拋物線運(yùn)動(dòng)的算法原理與相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • android項(xiàng)目手機(jī)衛(wèi)士來電顯示號碼歸屬地

    android項(xiàng)目手機(jī)衛(wèi)士來電顯示號碼歸屬地

    由于詐騙電話越來越猖狂,號碼歸屬地顯示越來越重要,本篇文章主要介紹了android手機(jī)衛(wèi)士來電顯示號碼歸屬地,有要的朋友可以了解一下。
    2016-10-10
  • Android短信備份及數(shù)據(jù)插入實(shí)現(xiàn)代碼解析

    Android短信備份及數(shù)據(jù)插入實(shí)現(xiàn)代碼解析

    這篇文章主要介紹了Android短信備份及數(shù)據(jù)插入實(shí)現(xiàn)代碼解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • Android LocationManager獲取經(jīng)度與緯度等地理信息

    Android LocationManager獲取經(jīng)度與緯度等地理信息

    這篇文章主要介紹了Android LocationManager獲取經(jīng)度與緯度等地理信息的相關(guān)資料,希望通過本站大家能掌握這樣的知識,需要的朋友可以參考下
    2017-09-09
  • Android開啟ADB網(wǎng)絡(luò)調(diào)試方法

    Android開啟ADB網(wǎng)絡(luò)調(diào)試方法

    今天小編就為大家分享一篇Android開啟ADB網(wǎng)絡(luò)調(diào)試方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • Mac Android Studio 3.0 Terminal 中文亂碼問題處理

    Mac Android Studio 3.0 Terminal 中文亂碼問題處理

    本文給大家分享的是在更新Android Studio 3.0之后,使用Terminal時(shí),發(fā)現(xiàn) git log 命令查看歷史 log會(huì)亂碼,以及最后的解決方法,推薦給小伙伴們
    2017-11-11
  • Android Studio綁定下拉框數(shù)據(jù)詳解

    Android Studio綁定下拉框數(shù)據(jù)詳解

    這篇文章主要為大家詳細(xì)介紹了Android Studio綁定下拉框數(shù)據(jù),Android Studio綁定網(wǎng)絡(luò)JSON數(shù)據(jù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • 談?wù)凙ndroid的三種網(wǎng)絡(luò)通信方式

    談?wù)凙ndroid的三種網(wǎng)絡(luò)通信方式

    Android平臺有三種網(wǎng)絡(luò)接口可以使用,他們分別是:java.net.*(標(biāo)準(zhǔn)Java接口)、Org.apache接口和Android.net.*(Android網(wǎng)絡(luò)接口)。本文詳細(xì)的介紹,有興趣的可以了解一下。
    2017-01-01
  • Android進(jìn)階事件分發(fā)機(jī)制解決事件沖突

    Android進(jìn)階事件分發(fā)機(jī)制解決事件沖突

    這篇文章主要為大家介紹了Android進(jìn)階事件分發(fā)機(jī)制解決事件沖突過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01

最新評論

浦县| 西峡县| 酉阳| 射阳县| 新津县| 开平市| 蒙自县| 航空| 山东| 镇原县| 禄劝| 尉犁县| 洛川县| 论坛| 曲麻莱县| 镇沅| 乌兰县| 平果县| 上虞市| 常熟市| 凉山| 北流市| 宜阳县| 铅山县| 南昌市| 怀宁县| 和顺县| 青海省| 海兴县| 桂东县| 满城县| 林州市| 两当县| 诸暨市| 宣威市| 呼玛县| 古浪县| 普兰店市| 白水县| 建阳市| 台北市|