折线图,左右滚动

packagecom.example.linechardemo;

importandroid.app.Activity;

importandroid.content.Context;

importandroid.graphics.Canvas;

importandroid.graphics.Color;

importandroid.graphics.Paint;

importandroid.os.Bundle;

importandroid.view.View;

importandroid.widget.FrameLayout;

importandroid.widget.HorizontalScrollView;

importjava.util.Random;

public classMainActivityextendsActivity {

@Override

protected voidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

FrameLayout frameLayout = (FrameLayout) findViewById(R.id.frame);

frameLayout.addView(newMyView2(this));

HorizontalScrollView scrollView = (HorizontalScrollView) findViewById(R.id.scrollview);

scrollView.addView(newMyView(this));

}

//y轴

classMyView2extendsView {

publicMyView2(Context context) {

super(context);

}

@Override

protected voidonDraw(Canvas canvas) {

super.onDraw(canvas);

Paint paint =newPaint();

paint.setColor(Color.RED);

paint.setStyle(Paint.Style.STROKE);

paint.setAntiAlias(true);

paint.setStrokeWidth(5);

canvas.drawLine(10,0,10, getHeight() /2, paint);

}

}

//x轴

classMyViewextendsView {

private intqujian;

publicMyView(Context context) {

super(context);

}

@Override

protected voidonMeasure(intwidthMeasureSpec,intheightMeasureSpec) {

qujian= (MeasureSpec.getSize(widthMeasureSpec) -jianju*2) /currentSize;

setMeasuredDimension(qujian*size+jianju*2, MeasureSpec.getSize(heightMeasureSpec));

}

intcurrentSize=6;

intsize=11;

intjianju=50;

@Override

protected voidonDraw(Canvas canvas) {

super.onDraw(canvas);

canvas.drawColor(Color.BLACK);

Paint paint =newPaint();

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);

intmax = getHeight() /2;

intmin =100;

int[] points =new int[11];

for(inti =0; i <11; i++) {

intcurrentX =jianju+qujian* (1+ i);

canvas.drawLine(currentX, getHeight() /2, currentX, getHeight() /2-10, paint);

Random random =newRandom();

intr = random.nextInt(max) % (max - min +1) + min;

canvas.drawCircle(currentX, r,5, paint);

points[i] = r;

}

for(inti =0; i < points.length; i++) {

intcurrentX =jianju+qujian* (1+ i);

intnextX =jianju+qujian* (2+ i);

if(i !=10)

canvas.drawLine(currentX, points[i], nextX, points[i +1], paint);

}

}

}

}






<xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/activity"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal"

tools:context="com.example.linechardemo.MainActivity">

android:id="@+id/frame"

android:layout_width="5dp"

android:layout_height="400dp"/>

android:id="@+id/scrollview"

android:layout_width="0dp"

android:layout_height="400dp"

android:scrollbars="none"

android:overScrollMode="never"

android:layout_weight="1"

android:background="#fff"/>





现实原理:

x轴放入水平滚动的HorizontalScrollView,即可。

推荐阅读更多精彩内容