‘壹’ 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啊杂七杂八的属性
/>