這篇文章主要介紹如何使用android畫函數(shù)曲線,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
10余年的睢縣網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。網(wǎng)絡(luò)營(yíng)銷推廣的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整睢縣建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)建站從事“睢縣網(wǎng)站設(shè)計(jì)”,“睢縣網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
//布局文件 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.avi.myapplication5.app.MainActivity"> <com.avi.myapplication5.app.DrawActivity android:layout_width="wrap_content" android:layout_height="wrap_content" /> </FrameLayout>//MainActivity類package com.avi.myapplication5.app; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }//DrawActivity類 package com.avi.myapplication5.app; import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; /** * Created by Administrator on 14-4-18. */ public class DrawActivity extends View implements View.OnTouchListener{ float PI=(float)Math.PI;//PI=3.1415那個(gè)什么的每次調(diào)用(float)Math.PI太麻煩,自定義一個(gè)。 float canvasWidth,canvasHeight;//畫布寬、高 float width,height;//自定義長(zhǎng)寬 float left,up;//自定義左上角位置 public DrawActivity(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onDraw(Canvas canvas){ super.onDraw(canvas); //開始一定要先調(diào)用Init()初始化參數(shù)。我自定義的參數(shù)沒有默認(rèn)值。 Init(canvas); Render(canvas); } @Override public boolean onTouch(View view, MotionEvent event) { return false; } //初始化全局參數(shù)。 //建議不要在其它函數(shù)里更改在這里初始化的參數(shù),以免得不出正確結(jié)果 void Init(Canvas canvas){ //獲取畫布寬、高 canvasWidth=(float)canvas.getWidth(); canvasHeight=(float)canvas.getHeight(); //自定義長(zhǎng)寬 width=2*PI;height=2*PI*canvasHeight/canvasWidth; // 自定義左上角位置 left=-width/2;up=height/2; } //渲染畫面。 //畫圖步驟在這里。 void Render(Canvas canvas){ //新建畫筆 Paint paint=new Paint(); //畫網(wǎng)格線。 paint.setARGB(255, 255, 0, 0); DrawGrid(canvas,0.3f,0.3f,paint); //畫坐標(biāo)線。 paint.setARGB(255,0,0,0); DrawCoord(canvas,paint); //顯示橫、縱坐標(biāo)軸名稱和原點(diǎn)名稱。(重要提示:這里就是你想要的新東西) paint.setARGB(255,100,100,100); DrawCoordName(canvas,paint,"t","f(t)","O"); //畫曲線 DrawCurve(canvas, paint); } //畫網(wǎng)格線。 //要先畫網(wǎng)格再畫坐標(biāo),不然網(wǎng)格線會(huì)把坐標(biāo)線覆蓋掉從而看不到坐標(biāo)線 //dx,dy: void DrawGrid(Canvas canvas,float dx,float dy,Paint paint){ //畫縱向網(wǎng)格線 //左側(cè) float x=0; while (x>left){ canvas.drawLine(PX(x),PY(up),PX(x),PY(up-height),paint); x-=dx; } //右側(cè) x=0; while (x<width+left){ canvas.drawLine(PX(x),PY(up),PX(x),PY(up-height),paint); x+=dx; } //畫橫向網(wǎng)格線 //上側(cè) float y=0; while (y<up){ canvas.drawLine(PX(left),PY(y),PX(left+width),PY(y),paint); y+=dy; } //下側(cè) y=0; while (y>up-height){ canvas.drawLine(PX(left),PY(y),PX(left+width),PY(y),paint); y-=dy; } } //畫坐標(biāo)線。 void DrawCoord(Canvas canvas,Paint paint){ //畫x,y坐標(biāo) canvas.drawLine(PX(left),PY(0f),PX(left+width),PY(0f),paint); canvas.drawLine(PX(0f),PY(up),PX(0f),PY(up-height),paint); } //(重要提示:這里就是你想要的新東西) //顯示橫、縱坐標(biāo)軸名稱和原點(diǎn)名稱。 void DrawCoordName(Canvas canvas,Paint paint,String xAxisName,String yAxisName,String originName){ //設(shè)置文字大小 paint.setTextSize(40f); //在適當(dāng)位置顯示x,y,O名稱 canvas.drawText(xAxisName,PX(left+width)-30f,PY(0f)+30f,paint); canvas.drawText(yAxisName,PX(0f),PY(up)+30f,paint); canvas.drawText(originName,PX(0f),PY(0f)+30f,paint); } //畫點(diǎn)函數(shù)。使用自定義坐標(biāo)。 void DrawPoint(Canvas canvas,float x,float y,Paint paint){ canvas.drawPoint(PX(x),PY(y),paint); } //(重要提示:所有你想畫的曲線都在這里進(jìn)行。) //畫曲線函數(shù)。 //使用畫布坐標(biāo)。所以要調(diào)用PX(x),PY(y)把自定義坐標(biāo)里的量(如x,y)轉(zhuǎn)換成畫布坐標(biāo)。 void DrawCurve(Canvas canvas,Paint paint){//繪制曲線 paint.setARGB(255,0,0,255); for(float x=left;x<left+width;x+=0.001f){ float y=(float)Math.sin(x); DrawPoint(canvas,x,y,paint); } //因?yàn)閤,y是自定義坐標(biāo),canvas.drawText()是系統(tǒng)提供的函數(shù), // 所以要調(diào)用PX(),PY()將(PI/2,sin(PI/2))轉(zhuǎn)換成畫布坐標(biāo); canvas.drawText("f(t)=sin(t)",PX(PI/2),PY((float)Math.sin(PI/2)),paint); } //將自定義坐標(biāo)轉(zhuǎn)換成畫布坐標(biāo)的函數(shù), //當(dāng)要用自定義坐標(biāo)在系統(tǒng)提供的函數(shù)上繪圖時(shí),要調(diào)用這兩個(gè)函數(shù)把自定義坐標(biāo)轉(zhuǎn)換成畫布坐標(biāo) float PX(float x){ return (x-left)*canvasWidth/width; } float PY(float y){ return (up-y)*canvasHeight/height; } }
以上是“如何使用android畫函數(shù)曲線”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
本文名稱:如何使用android畫函數(shù)曲線
當(dāng)前路徑:http://muchs.cn/article8/geceip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、網(wǎng)站營(yíng)銷、網(wǎng)頁設(shè)計(jì)公司、標(biāo)簽優(yōu)化、關(guān)鍵詞優(yōu)化、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)