繪制基礎知識-canvas paint

先來看一下Canvas

Canvas 用來提供draw方法的調用。繪制東西需要4個基本的組建:一個bitmap用來存放像素,一個canvas用來提供draw方法的調用(往bitmap里寫入),原始繪制元素(e.g.Rect, Path, text,Bitmap), 一個paint。
看一下canvas的主要方法:

  • canvas.save()保存canvas的狀態,保存之后,可以調用canvas的平移、縮放、旋轉、錯切、裁剪等操作。
  • canvas.restore();恢復canvas之前保存的狀態。
  • canvas.translate(x,y);移動坐標原點到指定位置。
  • canvas.drawArc();繪制圓弧。
  • canvas.drawText();繪制文本。
  • canvas.drawLine();繪制線段。
  • canvas.drawRect();繪制矩形。

    Paint類

    Paint 類提供繪制幾何圖形,文本,和位圖 所需要的樣式和顏色信息。
    paint的主要方法:

  • paint.setAntiAlias()抗鋸齒。

  • paint.setStrokeWidth() 設置畫筆的寬度。
  • paint.setStyle()設置空心。
  • paint.setDither()設置防抖動。
    ### HappinessView.java
public class HappinessView extends View {private static final float CIRCLE_RADIUS = 100f;private static final float faceRadiusToEyeRadiusRadio = 10f;private static final float faceRadiusToEyeOffsetRadio = 3f;private static final float faceRadiusToEyeSeparationRadio = 1.5f;private static final float faceRadiusToMouthWidthRadio = 1;private static final float faceRadiusToMouthHeightRadio = 3;private static final float faceRadiusToMouthOffsetRatio = 3;private enum Eye{ Left, Right}public HappinessView(Context context) {super(context);}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);Paint paint = new Paint();paint.setStyle(Paint.Style.STROKE);paint.setAntiAlias(true);paint.setColor(Color.BLUE);canvas.drawCircle(100, 100, CIRCLE_RADIUS, paint);circlePathForEye(canvas, paint, Eye.Left);circlePathForEye(canvas, paint, Eye.Right);bezierPathForSmile(canvas, paint);}private void bezierPathForSmile(Canvas canvas, Paint paint) {float fractionOfMaxSmile = -1f;float mouthWidth = CIRCLE_RADIUS / faceRadiusToMouthWidthRadio;float mouthHeight = CIRCLE_RADIUS / faceRadiusToMouthHeightRadio;float mouthVerticalOffset = CIRCLE_RADIUS / faceRadiusToMouthOffsetRatio;float smileHeight = Math.max(Math.min(fractionOfMaxSmile, 1), -1) * mouthHeight;float startPointX = 100 - mouthWidth / 2;float startPointY = 100 + mouthVerticalOffset;float endPointX = startPointX + mouthWidth;float endPointY = startPointY;float point1X = startPointX + mouthWidth / 3;float point1Y = startPointY + smileHeight;float point2X = endPointX - mouthWidth / 3;float point2Y = point1Y;Path path = new Path();path.moveTo(startPointX, startPointY);path.cubicTo( point1X, point1Y, point2X, point2Y, endPointX, endPointY);canvas.drawPath(path, paint);}private void circlePathForEye(Canvas canvas, Paint paint, Eye eye){float eyeRadius = 100 / faceRadiusToEyeRadiusRadio;float eyeVerticalOffset = 100 / faceRadiusToEyeOffsetRadio;float eyeHorizontalSeparation = 100 / faceRadiusToEyeSeparationRadio;float eyeY = 100 - eyeVerticalOffset;float eyeX = 100;switch (eye) {case Left:eyeX = eyeX - eyeHorizontalSeparation / 2;break;case Right:eyeX = eyeX + eyeHorizontalSeparation / 2;break;}canvas.drawCircle(eyeX, eyeY, eyeRadius, paint);}
}

一個簡單的笑臉圖形。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/389136.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/389136.shtml
英文地址,請注明出處:http://en.pswp.cn/news/389136.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Python - 調試Python代碼的方法

調試(debug) 將可疑環節的變量逐步打印出來,從而檢查哪里是否有錯。讓程序一部分一部分地運行起來。從核心功能開始,寫一點,運行一點,再修改一點。利用工具,例如一些IDE中的調試功能,提高調試效率。Python …

設計組合中的10個嚴重錯誤可能會導致您喪命

As an agency co-founder and design lead, I’ve been participating in many recruitment processes. I’ve seen hundreds of portfolios and CVs of aspiring designers. If you’re applying for a UI designer position, it is good to have some things in mind and to …

netflix_Netflix的計算因果推論

netflixJeffrey Wong, Colin McFarland杰弗里黃 , 科林麥克法蘭 Every Netflix data scientist, whether their background is from biology, psychology, physics, economics, math, statistics, or biostatistics, has made meaningful contributions to the way…

算法題庫網站

Google Code Jam(GCJ)Peking University Online Judge(POJ)CodeForces(CF)LeetCode(LC)Aizu Online Judge(AOJ)

org.dom4j.DocumentException: null Nested exception: null解決方法

由于最近在學習使用Spring架構,經常會遇到與xml文檔打交道,今天遇到了此問題,特來分享一下解決方案。 出錯原因: 很明顯是因為找不到文件路徑。這個原因是因為我使用了*.clas.getResourceAsStream(xmlFilePath&#xf…

MySQL命令學習

上面兩篇博客講了MySQL的安裝、登錄,密碼重置,為接下來的MySQL命令學習做好了準備,現在開啟MySQL命令學習之旅吧。 首先打開CMD,輸入命令:mysql -u root -p 登錄MySQL。 注意:MySQL命令終止符為分號 (;) …

實驗心得_大腸桿菌原核表達實驗心得(上篇)

大腸桿菌原核表達實驗心得(上篇)對于大腸桿菌蛋白表達,大部分小伙伴都覺得 so easy! 做大腸桿菌蛋白表達十幾年經歷的老司機還經常陰溝翻船,被大腸桿菌表達蛋白虐千百遍的慘痛經歷,很多小伙伴都有切膚之痛。福因德接下…

scrapy從安裝到爬取煎蛋網圖片

下載地址:https://www.lfd.uci.edu/~gohlke/pythonlibs/pip install wheelpip install lxmlpip install pyopensslpip install Twistedpip install pywin32pip install scrapy scrapy startproject jandan 創建項目cd jandancd jandan items.py 存放數據pipelines.p…

高斯金字塔 拉普拉斯金字塔_金字塔學入門指南

高斯金字塔 拉普拉斯金字塔The topic for today is on data validation and settings management using Python type hinting. We are going to use a Python package called pydantic which enforces type hints at runtime. It provides user-friendly errors, allowing you …

基本排序算法

插入排序 基本思想&#xff1a;把待排序列表分為已排和未排序兩部分&#xff0c;從未排序左邊取值&#xff0c;按順序從已排序的右端開始對比插入到相應的位置。 java代碼實現 private void insertSort(int[] arr){int i, j;int temp;for(i 0; i < arr.length; i){temp …

自定義版本更新彈窗

目錄介紹 1.Animation和Animator區別 2.Animation運行原理和源碼分析 2.1 基本屬性介紹2.2 如何計算動畫數據2.3 什么是動畫更新函數2.4 動畫數據如何存儲2.5 Animation的調用 3.Animator運行原理和源碼分析 3.1 屬性動畫的基本屬性3.2 屬性動畫新的概念3.3 PropertyValuesHold…

《SQL Server 2008從入門到精通》--20180716

1.鎖 當多個用戶同時對同一個數據進行修改時會產生并發問題&#xff0c;使用事務就可以解決這個問題。但是為了防止其他用戶修改另一個還沒完成的事務中的數據&#xff0c;就需要在事務中用到鎖。 SQL Server 2008提供了多種鎖模式&#xff1a;排他鎖&#xff0c;共享鎖&#x…

googleearthpro打開沒有地球_嫦娥五號成功著陸地球!為何嫦娥五號返回時會燃燒,升空卻不會?...

目前&#xff0c;嫦娥五號已經帶著月壤成功降落到地球上&#xff0c;創造了中國航天的又一里程碑。嫦娥五號這一路走來&#xff0c;困難重重&#xff0c;但都被我國航天科技人員逐一克服&#xff0c;最終圓滿地完成了嫦娥五號的月球采樣返回地球任務。嫦娥五號最后這一步走得可…

語言認知偏差_我們的認知偏差正在破壞患者的結果數據

語言認知偏差How do we know if we are providing high-quality care? The answer to this question is sought by a multitude of parties: patients, clinicians, educators, legislators, and insurance companies. Unfortunately, it’s not easy to determine. There is …

android 打包相關問題記錄

Android 中的打包配置在build.gradle文件中&#xff0c;下面對該文件的內容做一下記錄。 buildscript {repositories {jcenter()}dependencies {classpath com.android.tools.build:gradle:2.2.0} } 這里生命了倉庫的位置&#xff0c;依賴gradle的版本。 android{} android {…

本文將引導你使用XNA Game Studio Express一步一步地創建一個簡單的游戲

本文將引導你使用XNA Game Studio Express一步一步地創建一個簡單的游戲 第1步: 安裝軟件 第2步: 創建新項目 第3步: 查看代碼 第4步: 加入一個精靈 第5步: 使精靈可以移動和彈跳 第6步: 繼續嘗試! 完整的實例 第1步: 安裝軟件在動手之前,先確定你已經安裝了所需的軟件,其中包…

C#中實現對象的深拷貝

深度拷貝指的是將一個引用類型&#xff08;包含該類型里的引用類型&#xff09;拷貝一份(在內存中完完全全是兩個對象&#xff0c;沒有任何引用關系)..........  直接上代碼&#xff1a; 1 /// <summary>2 /// 對象的深度拷貝&#xff08;序列化的方式&#xf…

Okhttp 源碼解析

HTTP及okhttp的優勢 http結構 請求頭 列表內容表明本次請求的客戶端本次請求的cookie本次請求希望返回的數據類型本次請求是否采用數據壓縮等等一系列設置 請求體 指定本次請求所使用的方法請求所使用的方法 響應頭 - 服務器標識 - 狀態碼 - 內容編碼 - cookie 返回給客…

python中定義數據結構_Python中的數據結構。

python中定義數據結構I remembered the day when I made up my mind to learn python then the very first things I learned about data types and data structures. So in this article, I would like to discuss different data structures in python.我記得當初下定決心學習…

python實訓英文_GitHub - MiracleYoung/You-are-Pythonista: 匯聚【Python應用】【Python實訓】【Python技術分享】等等...

You-are-Pythonista匯聚【從零單排】【實戰項目】【數據科學】【自然語言處理】【計算機視覺】【面試題系列】【大航海】【Python應用】【錯題集】【技術沙龍】【內推渠道】等等【人人都是Pythonista】由公眾號【Python專欄】推出&#xff0c;請認準唯一標識&#xff1a;請仔細…