導航:首頁 > 操作系統 > android畫圓環

android畫圓環

發布時間:2022-10-01 17:14:29

『壹』 android中這樣的曲線要怎麼繪制

繪制曲線圖首先需要畫好橫豎坐標軸建立坐標系,比如坐標系中的100距離應該在canvas中繪制多長,這個是需要計算的,其實坐標體系的建立是最復雜的,我看過很多第三方庫的建立方法都不一樣,有的要靈活一些,有的比較死板。至於繪制曲線要麼是用Canvas.drawLine方法,要麼是用Path.lineTo方法,看你自己的習慣。

為了做出一個外觀良好的曲線圖,我參考了兩個開源代碼,第一個的曲線圖繪制限制較多,使用范圍太窄,但是有數據變化時的動畫效果。第二個的適用范圍很廣,他能根據數據集合自動計算橫縱坐標的個數,在canvas上單元格的距離,只需輸入坐標點就能自動建立坐標體系繪制曲線,但是沒有動畫效果。
先講第一個LineView。

LineView的demo可以在這里下載,lineview其實只是github項目的一部分,我是將其提取出來了的,個人覺得他的其他部分沒有參考價值。作者好像是個韓國人。

LineView的曲線繪制沒有什麼可取的部分,我想學習的是他實現動畫效果的方法,設計的很好,但具體實現還需要改進,讓動畫更流暢。

Lineview的調用方法:
在xml中添加lineview控制項
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/horizontalScrollView"
android:layout_alignParentRight="true"

android:layout_above="@+id/line_button">
<view
android:layout_width="wrap_content"
android:layout_height="200dp"
class="com.example.widget.LineView"
android:id="@+id/line_view"/>
</HorizontalScrollView>
在activity代碼中獲取lineview對象:
finalLineView lineView = (LineView)findViewById(R.id.line_view);

添加橫坐標:
int randomint = 9;
ArrayList<String>test =newArrayList<String>();
for (int i=0;i<randomint; i++){
test.add(String.valueOf(i+1));
}
lineView.setBottomTextList(test);
允許繪制坐標點:
lineView.setDrawDotLine(true);
lineView.setShowPopup(LineView.SHOW_POPUPS_NONE);

ArrayList<Integer> dataList = newArrayList<Integer>();
intrandom = (int)(Math.random()*9+1);
for (int i=0;i<randomint; i++){
dataList.add((int)(Math.random()*random));
}
添加縱坐標的值:
ArrayList<ArrayList<Integer>>dataLists = newArrayList<ArrayList<Integer>>();
dataLists.add(dataList);
lineView.setDataList(dataLists);
從其用法中可以看出,lineview需要提前設定橫坐標的范圍,而且縱坐標的值必須和lineView.setBottomTextList(test)中添加的值一一對應(讀lineview源碼可以知道),使用起來很不方便,我覺得作者僅僅是做出了一條曲線而已,而不太關注是否有用。和很多曲線圖的開源代碼一樣lineview允許一次繪制幾根顏色不同的曲線。
只需在上面的代碼中為dataLists再添加一個list成員就行。

『貳』 Android 自定義控制項 onDraw()方法里調用的canvas.drawArc()是畫圓或者圓環的,有一點疑問...這個方法

(currentTimeMillis - startTimeMillis ) * 360 / 8000
= (currentTimeMillis - startTimeMillis )/8000(每8秒) * 360(一圈)

『叄』 android view的onDraw從第二次觸發開始,界面變樣了。。。

這個view的大小被限制了,導致draw出來會缺了邊,將view設大一些,或者邊變小一點

『肆』 求教 Android自定義view畫圓弧的問題

簡單的思路是畫兩個圈 取出圓環,然後設置背景色 根據實際進度進行渲染
下面的鏈接是相關的視頻教程 希望可以幫到你
http://www.cniao5.com/course/10049

『伍』 android 怎麼畫2層圓環

Android繪制兩層圓環,可以使用自定義View,繼承View,重寫裡面的Ondraw方法,花兩個同心圓,示例如下:

java">packagecom.cn.myvn;

importandroid.content.Context;
importandroid.graphics.Canvas;
importandroid.graphics.Paint;
importandroid.util.AttributeSet;
importandroid.view.View;

{

privatefinalPaintpaint;
privatefinalContextcontext;

publicRingView(Contextcontext){

//TODOAuto-generatedconstructorstub
this(context,null);
}

publicRingView(Contextcontext,AttributeSetattrs){
super(context,attrs);
//TODOAuto-generatedconstructorstub
this.context=context;
this.paint=newPaint();
this.paint.setAntiAlias(true);//消除鋸齒
this.paint.setStyle(Paint.Style.STROKE);//繪制空心圓
}

@Override
protectedvoidonDraw(Canvascanvas){
//TODOAuto-generatedmethodstub
intcenter=getWidth()/2;
intinnerCircle=dip2px(context,83);//設置內圓半徑
intringWidth=dip2px(context,5);//設置圓環寬度

//繪制內圓
this.paint.setARGB(155,167,190,206);
this.paint.setStrokeWidth(2);
canvas.drawCircle(center,center,innerCircle,this.paint);

//繪制圓環
this.paint.setARGB(255,212,225,233);
this.paint.setStrokeWidth(ringWidth);
canvas.drawCircle(center,center,innerCircle+1+ringWidth/2,this.paint);

//繪制外圓
this.paint.setARGB(155,167,190,206);
this.paint.setStrokeWidth(2);
canvas.drawCircle(center,center,innerCircle+ringWidth,this.paint);

super.onDraw(canvas);
}
}

『陸』 如何在android畫分析圖(例如 柱狀圖、趨勢圖、餅圖)

目前android上圖標引擎並不少見,像aChartEngine就能很好的完成繪圖:

aChartEngine支持:1、linechart(折線圖)2、areachart(面積圖;分區圖,對比圖)3、scatterchart(散點圖)4、timechart(時間圖;進度表)5、barchart(條形圖;柱狀圖)6、piechart(餅圖)7、bubblechart(氣泡圖)8、doughnutchart(圓環圖)9、range(high-low)barchart(范圍條形圖)10、dialchart/gauge(撥號盤/壓力表)11、combined(anycombinationofline,cubicline,scatter,bar,rangebar,bubble)chart(組合圖)12、cubiclinechart(立方折線圖)

上述所有支持的圖表類型,都可以包含多個系列,都支持水平(默認)或垂直方式展示圖表,並且支持許多其他的自定義功能。所有圖表都可以建立為一個view,也可以建立為一個用於啟動activity的intent.

下面是一個餅狀圖的源碼事例:

package org.achartengine.chartdemo.demo.chart;


import org.achartengine.ChartFactory;

import org.achartengine.renderer.DefaultRenderer;


import android.content.Context;

import android.content.Intent;

import android.graphics.Color;


public class BudgetPieChart extends AbstractDemoChart {

public String getName() {

return "Budget chart";

}


public String getDesc() {

return "The budget per project for this year (pie chart)";

}

public Intent execute(Context context) {

double[] values = new double[] { 12, 14, 11, 10, 19 };//餅圖分層5塊,每塊代表的數值

int[] colors = new int[] { Color.BLUE, Color.GREEN, Color.MAGENTA, Color.YELLOW, Color.CYAN };//每塊餅圖的顏色

DefaultRenderer renderer = buildCategoryRenderer(colors);

renderer.setZoomButtonsVisible(true);//設置顯示放大縮小按鈕

renderer.setZoomEnabled(true);//設置允許放大縮小.

renderer.setChartTitleTextSize(20);//設置圖表標題的文字大小

return ChartFactory.getPieChartIntent(context, buildCategoryDataset("Project budget", values),

renderer, "Budget");//構建Intent, buildCategoryDataset是調用AbstraDemoChart的構建方法.

}


}

『柒』 求教 android半圓弧形的進度條問題

package com.example.roundprogressbar;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.RectF;import android.graphics.Typeface;import android.util.AttributeSet;import android.util.Log;import android.view.View;import com.example.circlepregress.R;/** * 仿iphone帶進度的進度條,線程安全的View,可直接在線程中更新進度 * @author xiaanming * */public class RoundProgressBar extends View {/*** 畫筆對象的引用*/private Paint paint;/*** 圓環的顏色*/private int roundColor;/*** 圓環進度的顏色*/private int roundProgressColor;/*** 中間進度百分比的字元串的顏色*/private int textColor;/*** 中間進度百分比的字元串的字體*/private float textSize;/*** 圓環的寬度*/private float roundWidth;/*** 最大進度*/private int max;/*** 當前進度*/private int progress;/*** 是否顯示中間的進度*/private boolean textIsDisplayable;/*** 進度的風格,實心或者空心*/private int style;public static final int STROKE = 0;public static final int FILL = 1;public RoundProgressBar(Context context) {this(context, null);}public RoundProgressBar(Context context, AttributeSet attrs) {this(context, attrs, 0);}public RoundProgressBar(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);paint = new Paint();TypedArray mTypedArray = context.obtainStyledAttributes(attrs,R.styleable.RoundProgressBar);//獲取自定義屬性和默認值roundColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.RED);roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.GREEN);textColor = mTypedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.GREEN);textSize = mTypedArray.getDimension(R.styleable.RoundProgressBar_textSize, 15);roundWidth = mTypedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 5);max = mTypedArray.getInteger(R.styleable.RoundProgressBar_max, 100);textIsDisplayable = mTypedArray.getBoolean(R.styleable.RoundProgressBar_textIsDisplayable, true);style = mTypedArray.getInt(R.styleable.RoundProgressBar_style, 0);mTypedArray.recycle();}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);/*** 畫最外層的大圓環*/int centre = getWidth()/2; //獲取圓心的x坐標int radius = (int) (centre - roundWidth/2); //圓環的半徑paint.setColor(roundColor); //設置圓環的顏色paint.setStyle(Paint.Style.STROKE); //設置空心paint.setStrokeWidth(roundWidth); //設置圓環的寬度paint.setAntiAlias(true); //消除鋸齒 canvas.drawCircle(centre, centre, radius, paint); //畫出圓環Log.e("log", centre + "");/*** 畫進度百分比*/paint.setStrokeWidth(0); paint.setColor(textColor);paint.setTextSize(textSize);paint.setTypeface(Typeface.DEFAULT_BOLD); //設置字體int percent = (int)(((float)progress / (float)max) * 100); //中間的進度百分比,先轉換成float在進行除法運算,不然都為0float textWidth = paint.measureText(percent + "%"); //測量字體寬度,我們需要根據字體的寬度設置在圓環中間if(textIsDisplayable && percent != 0 && style == STROKE){canvas.drawText(percent + "%", centre - textWidth / 2, centre + textSize/2, paint); //畫出進度百分比}/*** 畫圓弧 ,畫圓環的進度*///設置進度是實心還是空心paint.setStrokeWidth(roundWidth); //設置圓環的寬度paint.setColor(roundProgressColor); //設置進度的顏色RectF oval = new RectF(centre - radius, centre - radius, centre+ radius, centre + radius); //用於定義的圓弧的形狀和大小的界限switch (style) {case STROKE:{paint.setStyle(Paint.Style.STROKE);canvas.drawArc(oval, 0, 360 * progress / max, false, paint); //根據進度畫圓弧break;}case FILL:{paint.setStyle(Paint.Style.FILL_AND_STROKE);if(progress !=0)canvas.drawArc(oval, 0, 360 * progress / max, true, paint); //根據進度畫圓弧break;}}}public synchronized int getMax() {return max;}/*** 設置進度的最大值* @param max*/public synchronized void setMax(int max) {if(max max){progress = max;}if(progress <= max){this.progress = progress;postInvalidate();}}public int getCricleColor() {return roundColor;}public void setCricleColor(int cricleColor) {this.roundColor = cricleColor;}public int getCricleProgressColor() {return roundProgressColor;}public void setCricleProgressColor(int cricleProgressColor) {this.roundProgressColor = cricleProgressColor;}public int getTextColor() {return textColor;}public void setTextColor(int textColor) {this.textColor = textColor;}public float getTextSize() {return textSize;}public void setTextSize(float textSize) {this.textSize = textSize;}public float getRoundWidth() {return roundWidth;}public void setRoundWidth(float roundWidth) {this.roundWidth = roundWidth;}}package com.example.roundprogressbar;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import com.example.circlepregress.R;public class MainActivity extends Activity {private RoundProgressBar mRoundProgressBar1, mRoundProgressBar2 ,mRoundProgressBar3, mRoundProgressBar4, mRoundProgressBar5;private int progress = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_cricle_progress);mRoundProgressBar1 = (RoundProgressBar) findViewById(R.id.roundProgressBar1);mRoundProgressBar2 = (RoundProgressBar) findViewById(R.id.roundProgressBar2);mRoundProgressBar3 = (RoundProgressBar) findViewById(R.id.roundProgressBar3);mRoundProgressBar4 = (RoundProgressBar) findViewById(R.id.roundProgressBar4);mRoundProgressBar5 = (RoundProgressBar) findViewById(R.id.roundProgressBar5);((Button)findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {new Thread(new Runnable() {@Overridepublic void run() {while(progress <= 100){progress += 3;System.out.println(progress);mRoundProgressBar1.setProgress(progress);mRoundProgressBar2.setProgress(progress);mRoundProgressBar3.setProgress(progress);mRoundProgressBar4.setProgress(progress);mRoundProgressBar5.setProgress(progress);try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}}}}).start();}});}}activity_cricle_progress.xml剛好做過,通過復寫oncanvas實現的求教 android半圓弧形的進度條問題

『捌』 android如何在布局中吧.9畫成圓弧裝

Android畫布(Canvas)之--- 圓環,利用Path切除一個扇形,形成一段圓弧效果 [問題...然後再在剩餘的畫布上畫圓形,那麼自然截取的圖為我上圖中想要達到的...

『玖』 android內部帶刻度的圓形進度條

78910111213
//circleCenter 整個圓半徑 radius圓環的半徑
RectF oval = new RectF(circleCenter - radius, circleCenter - radius, circleCenter + radius, circleCenter + radius);
//因為-90°才是從12點鍾方向開始 所以從-90開始 progress 為進度
canvas.drawArc(oval, -90, (float) (progress * 3.6), false, paint);

『拾』 android 自定義載入圓環控制項裡面怎麼設置三行字

public class MyView extends View{
//此處省略構造方法

private void onDraw(Canvas canvas){
//重寫view的onDraw方法,繪制控制項的樣式
//這里你使用canvas來繪制,你布局中使用這個控制項就是你繪制的樣子
}

//然後你可以定義很多自己的一些方法,用來修改控制項的樣式
//假如你自定義的一個進度條的話,就要修改進度條值,你就可以自定義方法,讓實現對象來改變進度值,記得修改後調用validate方法更新顯示。(具體函數記不太清了)
}

大概就是這樣實現的自定義控制項,自定義控制項的話優化是很重要的哦,不然性能會很差。
然後你要使用這個控制項的話,在布局中就需要這樣定義,假如這個自定義控制項類是這樣的:
xxx.xxx.MyView。
則使用時:
<xxx.xxx.MyView
這些地方一樣的設置寬高,id啊雜七雜八的屬性

/>

閱讀全文

與android畫圓環相關的資料

熱點內容
看免費大片網站 瀏覽:849
h5游戲源碼論壇 瀏覽:692
視覺表現pdf 瀏覽:555
htlm源碼 瀏覽:939
文明景洪app怎麼下載 瀏覽:232
郵件電子伺服器是什麼 瀏覽:910
電腦軟體加密保護軟體 瀏覽:196
java迴文素數 瀏覽:369
愛愛影視倫理片 瀏覽:849
電影更新最快 瀏覽:612
熱門推薦中文字幕1 瀏覽:674
成龍五行拳電影叫什麼 瀏覽:718
加密110報警點位 瀏覽:707
同花順指標編程 瀏覽:572
小米應用雙開如何加密 瀏覽:180
rsa的加密優勢 瀏覽:243
佛教電影在線觀看 瀏覽:754
韓劇電影免費觀看 瀏覽:685
日本劇情劇電影 瀏覽:969
2017最火編程語言 瀏覽:406