这应该是他自己定义的控件把。
重写了onDraw方法
构造方法是两个list为参数(高低温数据)
大概就是这种思路,通过数据画出整个view

解决方案 »

  1.   

    MainActivitypackage com.example.zdemo;import java.util.ArrayList;
    import java.util.List;import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.widget.RelativeLayout;
    import android.widget.RelativeLayout.LayoutParams;public class MainActivity extends Activity {    private RelativeLayout rl ;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            List<Integer> high = new ArrayList<Integer>();
            high.add(40);
            high.add(35);
            high.add(50);
            high.add(45);
            List<Integer> low = new ArrayList<Integer>();
            low.add(-10);
            low.add(5);
            low.add(0);
            low.add(-20);
            MyChartView chart = new MyChartView(MainActivity.this , high , low);
            rl = (RelativeLayout)findViewById(R.id.rl);
            LayoutParams lp = new RelativeLayout.LayoutParams(400,200);
            chart.setLayoutParams(lp);
            rl.addView(chart);
            
            
        }    @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
    }MyChartViewpackage com.example.zdemo;import java.util.ArrayList;
    import java.util.List;import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Rect;
    import android.util.AttributeSet;
    import android.view.View;public class MyChartView extends View {    private List< Integer> highTemprature = new ArrayList<Integer>();    private List< Integer> lowTemprature = new ArrayList<Integer>();    private boolean isDataEnough = false;
        
        public MyChartView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            
        }    public MyChartView(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }    public MyChartView(Context context) {
            this(context, null);
        }    public MyChartView(Context context, List<Integer> a,
                List< Integer> b) {
            this(context);
            highTemprature = a;
            lowTemprature = b;
            init();
        }    private void init() {
            if(highTemprature.size() < 4 || lowTemprature.size() < 4){
                isDataEnough = false;
            }
            else{
                isDataEnough = true;
                if (highTemprature.size() > 4) {
                    highTemprature = highTemprature.subList(0, 4);
                }
                if (lowTemprature.size() > 4) {
                    lowTemprature = lowTemprature.subList(0, 4);
                }
            }
        }    @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            Paint paintGRAY = new Paint();
            Paint paintWHITE = new Paint();
            Paint paintBLUE = new Paint();
            Paint paintRED = new Paint();
            paintGRAY.setColor(Color.GRAY);
            paintWHITE.setColor(Color.WHITE);
            paintBLUE.setColor(Color.BLUE);
            paintRED.setColor(Color.RED);
            canvas.drawRect(new Rect(0, 0, getWidth(), getHeight()), paintGRAY);
            if(isDataEnough){
                float singleWidth = getWidth()/4;
                float lastX = 0;
                float lastY = 0;
                float curX = 0;
                float curY = 0;
                //画高温线
                for(int i = 0 ; i < 4 ; i++){
                    //画点
                    canvas.drawCircle(singleWidth/2+i*singleWidth, getHeight()/2-highTemprature.get(i), 5, paintWHITE);
                    if(i == 0){
                        lastX = singleWidth/2+i*singleWidth;
                        lastY = getHeight()/2-highTemprature.get(i);
                    }
                    //画线
                    else{
                        curX = singleWidth/2+i*singleWidth;
                        curY = getHeight()/2-highTemprature.get(i);
                        canvas.drawLine(lastX, lastY, curX, curY, paintRED);
                        lastX = curX;
                        lastY = curY;
                    }
                }
                //低温线
                for(int i = 0 ; i < 4 ; i++){
                    //画点
                    canvas.drawCircle(singleWidth/2+i*singleWidth, getHeight()/2-lowTemprature.get(i), 5, paintWHITE);
                    if(i == 0){
                        lastX = singleWidth/2+i*singleWidth;
                        lastY = getHeight()/2-lowTemprature.get(i);
                    }
                    //画线
                    else{
                        curX = singleWidth/2+i*singleWidth;
                        curY = getHeight()/2-lowTemprature.get(i);
                        canvas.drawLine(lastX, lastY, curX, curY, paintBLUE);
                        lastX = curX;
                        lastY = curY;
                    }
                }
            }
        }
    }activity_main.xml<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/rl"
        android:layout_width="match_parent"
        android:layout_height="match_parent" ></RelativeLayout>给分吧。