android 畫曲線圖

package com.tiantian.forsight;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;

// next time add auto side back ,auto lenth distance.
public class MyLine extends View {

 float[] x = { 1, 2, 3, 4,5,6,7,8 };
 float[] y = { 2, 1, 2, 2,3,4,2,1 };
 String[] BottomText = { "3月1日", "3月2日", "3月3日", "3月4日","3月1日", "3月2日", "3月3日", "3月4日" };
 // float[]realx,realy;
 int with = 1000, hight = 300;
 int totalWith=0;
 int displayWith=0;
 float dm =0f;
 public void RealDate() {

  float maxX = x[0], maxY = y[0];
  for (int i = 0; i < x.length; i++) {
   if (maxX < x[i]) {
    maxX = x[i];
   }
  }

  for (int i = 0; i < y.length; i++) {
   if (maxY < y[i]) {
    maxY = y[i];
   }
  }

  for (int i = 0; i < x.length; i++) {
   x[i] = x[i] * (with / maxX);
   Log.v("", "ddddd:_change:" + x[i]);
  }
  for (int i = 0; i < y.length; i++) {
   y[i] = y[i] * (hight / maxY);
  }
 }

 public MyLine(Context context, AttributeSet attrs) {
  super(context, attrs);
  
  dm =context.getResources().getDisplayMetrics().density;
  // left for side 25   right for text 15
  displayWith=(int)(context.getResources().getDisplayMetrics().widthPixels-dm*40);
  
 }


 Paint mpaint = new Paint();
 Paint mpointpaint = new Paint();

 //public void init(float[] x,float[] y,String[] bottomtitle)
 public void init()
 {
//  this.x=x;
//  this.y=y;
//  this.BottomText=bottomtitle;
  mpaint.setColor(Color.GRAY);
  mpaint.setTextSize(18);
  mpaint.setAntiAlias(true);
  mpaint.setStyle(Paint.Style.FILL);

  mpointpaint.setColor(Color.RED);
  mpointpaint.setTextSize(24);
  mpointpaint.setAntiAlias(true);
  mpointpaint.setStyle(Paint.Style.FILL);
  RealDate();
  {
   with=displayWith/5;
   totalWith=with*x.length;
   LinearLayout.LayoutParams ll =(LinearLayout.LayoutParams) this.getLayoutParams();
   ll.width=totalWith;
   this.setLayoutParams(ll);
   this.setOnTouchListener(new OnTouchListener() {
    int x_before = 0;

    @Override
    public boolean onTouch(View arg0, MotionEvent arg1) {
     // TODO Auto-generated method stub
     int distance = 0;
     if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
      // next touch continue;
      x_before = 0;
     } else if (arg1.getAction() == MotionEvent.ACTION_MOVE) {
      if (x_before == 0) {
       x_before = (int) arg1.getRawX();
      }
      distance = (int) arg1.getRawX() - x_before;
      if (distance != 0) {
       MyLine.this.layout(MyLine.this.getLeft() + distance,
         MyLine.this.getTop(), MyLine.this.getLeft()
           + distance+totalWith, MyLine.this.getBottom());
      }
      x_before = (int) arg1.getRawX();
      
     } else if (arg1.getAction() == MotionEvent.ACTION_UP) {
      // return back
      
      if (MyLine.this.getRight() > totalWith) {
       MyLine.this.layout(0
         , MyLine.this.getTop(),totalWith,
         MyLine.this.getBottom());
      }
      else if (MyLine.this.getLeft() <displayWith-totalWith) {
       MyLine.this.layout(displayWith-totalWith, MyLine.this.getTop(), displayWith,
         MyLine.this.getBottom());
      }
      Log.v("", "ddddd" + x_before+"dddd"+MyLine.this.getWidth());
     }

     return true;
    }
   });
  }
 }

 // ok evrey thing ok!
 boolean first=true;
 @Override
 public void onDraw(Canvas c) {
  super.onDraw(c);
  Log.v("", "dddd" + x[0] + "::dd::" + y[0]);
  if(first){
   init();
   first=false;
  }
  // c.drawCircle(60, 20, 10, mpointpaint);// 小圓

  //c.drawText("ssdd", 20, 40, mpaint);
  //c.drawLine(0, 0, x[0], y[0], mpaint);
  c.drawText(BottomText[0], x[0]-20, hight+20, mpaint);
  c.drawCircle(x[0], y[0], 3, mpointpaint);
  if (x.length > 1)
   for (int i = 1; i < x.length; i++) {
    Log.v("", "dddd" + x[i] + "::dd::" + y[i]);
    c.drawText("ssdd", x[i] + 10, y[i] - 10, mpaint);
    c.drawCircle(x[i], y[i], 3, mpointpaint);
    c.drawLine(x[i - 1], y[i - 1], x[i], y[i], mpaint);
    
    // draw the bottom text
    c.drawText(BottomText[i], x[i]-20, hight+20, mpaint);
   }
 }
}

 

發佈了100 篇原創文章 · 獲贊 10 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章