Android 仪表盘
生活随笔
收集整理的這篇文章主要介紹了
Android 仪表盘
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、依賴
compile 'org.xclcharts:lib:2.4'自定義儀表盤view
/** * @ClassName meixi * @Description 儀表盤例子 * @author <br/>(1085220040@qq.com.com) */ public class GaugeChart01View extends GraphicalView {private String TAG = "GaugeChart01View"; private GaugeChart chart = new GaugeChart(); private List<String> mLabels = new ArrayList<String>(); private List<Pair> mPartitionSet = new ArrayList<Pair>(); private float mAngle = 0.0f; public GaugeChart01View(Context context) {super(context); // TODO Auto-generated constructor stub initView(); }public GaugeChart01View(Context context, AttributeSet attrs){super(context, attrs); initView(); }public GaugeChart01View(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle); initView(); }private void initView(){chartLabels(); chartDataSet(); chartRender(); }@Override protected void onSizeChanged(int w, int h, int oldw, int oldh) {super.onSizeChanged(w, h, oldw, oldh); //圖所占范圍大小 //xml中的設置: android:layout_width="300dip" // android:layout_height="300dip" chart.setChartRange(w ,h ); //繪圖區范圍 //左右各縮進10% //int offsetX = DensityUtil.dip2px(getContext(), (float) (300 * 0.1)); //偏移高度的25%下來 //int offsetY = DensityUtil.dip2px(getContext(), (float) (300 * 0.25)); // chart.setPadding(offsetY, 0, 0, 0); }//從seekbar傳入的值 public void setAngle(float currentAngle){mAngle = currentAngle; }public void chartRender(){try {//設置標題 chart.setTitle("刻度盤 "); //刻度步長 chart.setTickSteps(10d);//密度 //標簽(標簽和步長分開,步長即刻度可以密點,標簽可以松點) chart.setCategories(mLabels); //分區 chart.setPartition(mPartitionSet); //設置當前指向角度(0-180). //chart.setCurrentAngle(90f); chart.setCurrentAngle(mAngle); //繪制邊框 chart.showRoundBorder(); chart.getPinterCirclePaint().setColor(getResources().getColor(R.color.t_blue));//指針圓點畫筆 chart.getPointerLinePaint().setColor(getResources().getColor(R.color.anlv));//指針畫筆 chart.getTickPaint().setColor(getResources().getColor(R.color.anlv));//向內刻度畫筆 chart.getDountPaint().setColor(getResources().getColor(R.color.anlv));//外刻度線畫筆 chart.getLabelPaint().setColor(getResources().getColor(R.color.anlv));//外標簽畫筆 } catch (Exception e) {// TODO Auto-generated catch block Log.e(TAG, e.toString()); }}//分區[角度(0-mStartAngle),顏色] private void chartDataSet(){int Angle = 180/3; mPartitionSet.add(new Pair<Float,Integer>((float)Angle, Color.rgb(73, 172, 72))); mPartitionSet.add(new Pair<Float,Integer>((float)Angle, Color.rgb(247, 156, 27))); mPartitionSet.add(new Pair<Float,Integer>((float)Angle, Color.rgb(224, 62, 54))); }private void chartLabels(){//標簽 mLabels.add("起始"); mLabels.add("安全"); mLabels.add("警惕"); mLabels.add("危險"); mLabels.add("終止"); }@Override public void render(Canvas canvas) {try{chart.render(canvas); } catch (Exception e){Log.e(TAG, e.toString()); }} } <clan.yuanxin.com.mydkaifa.pictures.GaugeChart01View android:id="@+id/chart_view" android:layout_width="400dip" android:layout_height="200dip" android:layout_centerInParent="true" />Java代碼
GaugeChart01View chart = null; chart = (GaugeChart01View)findViewById(R.id.chart_view); chart.setAngle(progress);//30 chart.chartRender(); chart.invalidate();總結
以上是生活随笔為你收集整理的Android 仪表盘的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 高等数理统计(part7)--无偏估计
- 下一篇: Android 柱状图