package
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.BitmapDrawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import com.tongniu.liandongfenqikanban.R;
import java.util.ArrayList;
import static android.graphics.Paint.Style;
/**
* on 2017
*/
public class LineView extends View {
private ArrayList yue;
private ArrayList y;
private Paint mPaint;
// private Paint textPaint;
private Path mPath;
private Path erasePath;
private Resources mResources;
private Bitmap mBitmap;
private int height;
private float value;
/**
* 画图paint
*/
private Paint bitmapPaint;
/**
* 画y线
* @param context
*
*/
private Paint yPaint;
/**
* 文字
*/
private Paint textpaint;
private Paint xPaint;
private boolean withzhanbi;
private int width;
private float max;
private float min;
private int distanceX;
private int value30;
public LineView(Context context) {
super(context);
initdata();
}
public LineView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
mResources = getResources();
initdata();
initView(context,attrs);
}
public LineView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initdata();
initView(context,attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
height = getMeasuredHeight()-dp2px(getContext(),25);
width = getMeasuredWidth();
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
// Log.i("aaaaaaaaa"," w"+w+" h"+h+"oldw"+oldw+" oldh"+oldh);
}
public void initdata() {
mPath = new Path();
erasePath = new Path();
int i = dp2px(getContext(), 1);
//画线的笔
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStrokeWidth(i);
mPaint.setDither(true);
mPaint.setStyle(Style.STROKE);
mPaint.setColor(Color.parseColor("#32C8FF"));
//用于画图
bitmapPaint = new Paint();
yPaint = new Paint();
yPaint.setAntiAlias(true);
yPaint.setColor(Color.parseColor("#D0D0D0"));
yPaint.setStyle(Style.FILL_AND_STROKE);
yPaint.setStrokeWidth(3);
//用于画在点上的坐标
textpaint = new Paint();
int texts =dp2px(getContext(),12);
textpaint.setStrokeWidth(30);
textpaint.setTextSize(texts);
textpaint.setAntiAlias(true);
textpaint.setColor(Color.parseColor("#FF9800"));
xPaint = new Paint();
xPaint.setStrokeWidth(11);
xPaint.setTextSize(texts);
xPaint.setAntiAlias(true);
xPaint.setColor(Color.parseColor("#000000"));
mBitmap = ((BitmapDrawable) mResources.getDrawable(R.drawable.yuan_dian2)).getBitmap();
}
private void initView(Context context, AttributeSet attrs) {
TypedArray typeArray = context.getTheme().obtainStyledAttributes(attrs,
R.styleable.LineView, 0, 0);
withzhanbi = typeArray.getBoolean(R.styleable.LineView_withzhanbi, false);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
/* int height = getHeight();
int width = getWidth();
Log.i(“aaaa”,”height”+height+”width”+width);*/
ArrayList xCoordinates = new ArrayList<>();
ArrayList yCoordinates = new ArrayList<>();
xCoordinates.add(“06-06”);
xCoordinates.add(“06-06”);
xCoordinates.add(“06-06”);
xCoordinates.add(“06-06”);
xCoordinates.add(“06-06”);
xCoordinates.add(“06-06”);
xCoordinates.add(“06-06”);
yCoordinates.add((float) 0);
yCoordinates.add((float) 0);
yCoordinates.add((float) 0);
yCoordinates.add((float) 0);
yCoordinates.add((float) 0);
yCoordinates.add((float) 0);
yCoordinates.add((float) 0);
mPath.reset();
if (y==null){
SetY(yCoordinates);
demo(canvas, xCoordinates, yCoordinates,1);
}else {
SetY(y);
demo(canvas,yue,y,1);
}
}
private void SetY(ArrayList<Float> yCoordinates) {
max= yCoordinates.get(0);
min=yCoordinates.get(0);
for (int i = 0; i < yCoordinates.size(); i++) {
float value = yCoordinates.get(i);
if (max<value){
max = value;
}
if (value<min){
min = value;
}
}
float value = max - min;
if (withzhanbi){
if (value<1){
max+=0.2f;
}else if (value>2){
max+=3;
}else {
max+=1f;
}
}else {
if (value<10){
max+=5;
}else if (value<40){
max+=10;
}else if (value>180){
max+=80;
}else {
max+=20;
}
}
/*if (withzhanbi){
if (max-min>7){
max+=10;
}else if (max-min<2){
max+=0.1f;
}else {
if (max>20){
max+= 0.3f;
}else {
max+=0.2f;
}
}
}else {
if (max>180){
max+=80;
}else if (max>40){
max+= 25;
}else {
max+=7;
}
}*/
}
private void demo(Canvas canvas, ArrayList<String> xCoordinates, ArrayList<Float> yCoordinates,int first) {
float value = max - min;
if (value==0){
value=1;
}
distanceX = width / (yCoordinates.size());
// mPath.moveTo(0,height+65);
if (withzhanbi){
value30 = dp2px(getContext(),1);
}else {
value30 = dp2px(getContext(),8);
}
//第一个点
int firstY = height - (int) (((yCoordinates.get(0) - min) / value) * height) + value30;
mPath.moveTo(0, firstY);
mPath.cubicTo(distanceX / 4, firstY , distanceX / 4, firstY, distanceX /2,firstY);
if (first == 1) {
canvas.drawText(xCoordinates.get(0)+"", distanceX / 4 - 6, height + dp2px(getContext(),25)-8, xPaint);
}
boolean flag = true;
//画竖线
for (int i = 0; i < yCoordinates.size(); i++) {
canvas.drawLine(distanceX * i + distanceX / 2, dp2px(getContext(),10), distanceX * i + distanceX / 2, height + dp2px(getContext(),13)-8, yPaint);
}
for (int i = 1; i < yCoordinates.size(); i++) {
int valueY = height - (int) (((yCoordinates.get(i) - min) / value) * height) + value30;
int beforeValue = height - (int) (((yCoordinates.get(i - 1) - min) / value) * height) + value30;
int v = (valueY + beforeValue) / 2;
int vb = Math.abs(valueY - beforeValue);
Log.i("aaaaaaaa", "max" + max + " min" + min + " valueY" + valueY + " height" + height + " 25+distanceX*(i)" + (distanceX * (i)));
int i1 = distanceX * (i);
int i2 = distanceX * i + distanceX / 2;
mPath.cubicTo(i1,beforeValue,i1,valueY,i2,valueY);
flag = !flag;
if (first == 1) {
canvas.drawText(xCoordinates.get(i), distanceX * i + distanceX / 4 - dp2px(getContext(),4), height + dp2px(getContext(),25)-8, xPaint);
}
}
int valueY = height - (int) (((yCoordinates.get(6) - min) / value) * height) + value30;
mPath.cubicTo(distanceX *6+ distanceX /2,valueY, distanceX *6+ distanceX /2,valueY,width,valueY);
canvas.drawPath(mPath, mPaint);
/* LinearGradient shader = new LinearGradient(
0, 50,
0, height+100,
Color.parseColor("#32C8FF"),Color.parseColor("#Ffffff"),
Shader.TileMode.CLAMP);
mPaint.setShader(shader);*/
//画图
for (int i = 0; i < yCoordinates.size(); i++) {
int valueY2 = height - (int) (((yCoordinates.get(i) - min) / value) * height) + value30;
canvas.drawBitmap(mBitmap, distanceX *i+ distanceX /2-dp2px(getContext(),4),valueY2-dp2px(getContext(),4),bitmapPaint);
if (withzhanbi){
canvas.drawText(yCoordinates.get(i)+"%", distanceX *i+ distanceX /2-dp2px(getContext(),10),valueY2-dp2px(getContext(),15),textpaint);
}else {
String s = yCoordinates.get(i) + "";
canvas.drawText((s).substring(0,s.length()-2), distanceX *i+ distanceX /2-dp2px(getContext(),10),valueY2-dp2px(getContext(),15),textpaint);
}
}
}
public void setData1(ArrayList<Float> y){
this.y = y;
}
public void setYuef(ArrayList<String> yue){
this.yue= yue;
}
public static int dp2px(Context context, float dipValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}
}