折线图,左右滚动,纯代码实现


package com.example.linechardemo;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

import java.util.Random;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.activity);
        linearLayout.addView(new MyView3(this),0);
        HorizontalScrollView horizontalScrollView = new HorizontalScrollView(this);
        horizontalScrollView.addView(new MyView(this));
        horizontalScrollView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        horizontalScrollView.setHorizontalScrollBarEnabled(false);
        linearLayout.addView(horizontalScrollView);

    }

    //y轴
    class MyView3 extends View {

        public MyView3(Context context) {
            super(context);
        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            setMeasuredDimension(MeasureSpec.getSize(7), MeasureSpec.getSize(heightMeasureSpec)/2);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            Paint paint = new Paint();
            paint.setColor(Color.RED);
            paint.setStyle(Paint.Style.STROKE);
            paint.setAntiAlias(true);
            paint.setStrokeWidth(10);
            canvas.drawLine(0, 0, 0, getHeight()+10, paint);
        }
    }

    //x轴
    class MyView extends View {
        //横坐标的区间
        private int qujian;

        public MyView(Context context) {
            super(context);
        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            qujian = (MeasureSpec.getSize(widthMeasureSpec) - jianju * 2) / currentSize;
            setMeasuredDimension(qujian * size + jianju * 2, MeasureSpec.getSize(heightMeasureSpec));
        }

        int currentSize = 6;
        int size = 11;
        int jianju = 50;

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            canvas.drawColor(Color.BLACK);
            Paint paint = new Paint();
            paint.setColor(Color.RED);
            paint.setStyle(Paint.Style.STROKE);
            paint.setAntiAlias(true);
            paint.setStrokeWidth(5);
            canvas.drawLine(0, getHeight() / 2, qujian * size + jianju, getHeight() / 2, paint);
            int max = getHeight() / 2;
            int min = 100;
            int[] points = new int[11];
            for (int i = 0; i < 11; i++) {
                int currentX = jianju + qujian * (1 + i);
                canvas.drawLine(currentX, getHeight() / 2, currentX, getHeight() / 2 - 10, paint);
                Random random = new Random();
                int r = random.nextInt(max) % (max - min + 1) + min;
               canvas.drawCircle(currentX, r, 5, paint);
                points[i] = r;
            }
            for (int i = 0; i < points.length; i++) {
                int currentX = jianju + qujian * (1 + i);
                int nextX = jianju + qujian * (2 + i);
                if (i != 10)
                    canvas.drawLine(currentX, points[i], nextX, points[i + 1], paint);
            }


        }
    }
}



Screenshot_1490776472.png
Screenshot_1490776476.png

推荐阅读更多精彩内容