android中在代码中动态布按钮和画板局并绘制曲线

绘图面板基类继承View
package com.mytest.myapplication.Utility;

import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import android.widget.Button;

import java.util.ArrayList;

//自定义简单曲线图
public class MyGraphicView extends View{

    public Context _Context;
    private Paint _mPaint;
    private Paint _mPaint2;
    public String _type="0";


   // private Resources _res;
   // private DisplayMetrics _dm;
    /**
     * 曲线上总点数
     */
    private float[] mPoints={0,0,10,10,20,20,30,30,};
    /**
     * 纵坐标值
     */
    private ArrayList yRawData;
    /**
     * 横坐标值
     */
    private ArrayList xRawDatas;
    public MyGraphicView(Context context)
    {
        this(context, null);
    }
    public MyGraphicView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        this._Context = context;

        initView();
    }
    private void initView()//初始化
    {
        //this._res = _Context.getResources();
        this._mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        this._mPaint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
       // _dm = new DisplayMetrics();
      //  WindowManager wm = (WindowManager) _Context.getSystemService(Context.WINDOW_SERVICE);
       // wm.getDefaultDisplay().getMetrics(_dm);
    }
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh)//尺寸改变事件
    {
          super.onSizeChanged(w,h,oldw,oldh);
    }
    @Override
    protected void onDraw(Canvas canvas)
    {
        _mPaint.setColor(Color.BLUE);//颜色赋值
        _mPaint2.setColor(Color.RED);//颜色赋值

        if(_type=="0") {
            canvas.drawLine(0, 100, getWidth(), getHeight(), _mPaint);

            // super.onDraw(canvas);
        }
        else
        {
            DrawXYAxis( canvas);//画坐标和曲线
           // canvas.drawLines(mPoints, _mPaint);
        }

    }
    public void Redraw(String type)//重绘触发
    {
        _type=type;
        invalidate();//重绘触发事件
    }
    public void DrawXYAxis(Canvas canvas)//画坐标和曲线
    {
        int allWidth=getWidth();//界面宽度
        int allHeight=getHeight();//界面高度
        canvas.drawLine(50,50,(float)(50),(float)(allHeight-50),_mPaint2);//画y轴
        canvas.drawLine((float)(50),(float)(allHeight-50),(float)(allWidth-50),(float)(allHeight-50),_mPaint2);//画x轴


        float[] xPoints=new float[allWidth];
        float[] yPoints=new float[allWidth];
        for(int i=0;i50;i-=50)//画纵轴刻度
        {
          try {
              canvas.drawText(String.valueOf(allHeight-10-i),20,i, _mPaint);
          }
          catch (Exception e)
          {
            Log.i("异常:",e.toString());
          }


        }
        for(int i=0;i

绘制活动页(TableLayout(纵向)好用,LinerLayOut横向)

 

package com.mytest.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TableLayout;

import com.mytest.myapplication.Utility.MyGraphicView;

public class MyDrawActivity extends AppCompatActivity {
    MyGraphicView graphicView;
    public Button button1;
    public Button button2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        int width = metric.widthPixels;     // 屏幕宽度(像素)
        int height = metric.heightPixels;   // 屏幕高度(像素)
        float density = metric.density;      // 屏幕密度(0.75 / 1.0 / 1.5)
        int densityDpi = metric.densityDpi;  // 屏幕密度DPI(120 / 160 / 240)
        //final LinearLayout layout = new LinearLayout(this);
        final TableLayout layout=new TableLayout(this);
        graphicView=new MyGraphicView(this,null);
        button1=new Button(this);
        button2=new Button(this);
        button1.setText("按钮1");
        button2.setText("按钮2");
        button1.setWidth(width);
        button2.setWidth(width);
        layout.addView(button1);
        layout.addView(button2);
        layout.addView(graphicView);
        setContentView(layout);
        button1.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {

                graphicView. Redraw("0");//重绘触发
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                graphicView. Redraw("1");//重绘触发
            }
        });
    }
}

调用(主活动页的按钮注册侦听事件调用)

 

Button.OnClickListener listener5 = new Button.OnClickListener() {
    public void onClick(View v) {
        Intent intent = new Intent(MainActivity.this,MyDrawActivity.class);
        startActivity(intent);
        MainActivity.this.finish();


    }
};

 

你可能感兴趣的:(Java,安卓前端,安卓)