本文主要是介绍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<Double> yRawData;/*** 横坐标值*/private ArrayList<String> 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);}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh)//尺寸改变事件{super.onSizeChanged(w,h,oldw,oldh);}@Overrideprotected 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;i<allWidth;i++){xPoints[i]=i;yPoints[i]=(float)(Math.random()*400);}canvas.drawText("测试曲线",(float)(allWidth/2.0),20, _mPaint);//画标题for(int i=10;i<allWidth;i++)//画横轴刻度{if(i%50==0){canvas.drawText(String.valueOf(i),i,allHeight-30, _mPaint);}}for(int i=allHeight-10;i>50;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<allWidth-2;i++)//画曲线{canvas.drawLine(xPoints[i]+50,allHeight-210-yPoints[i],xPoints[i+1]+50,allHeight-210-yPoints[i+1], _mPaint);}}}
绘制活动页(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;@Overrideprotected 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() {@Overridepublic void onClick(View v) {graphicView. Redraw("0");//重绘触发}});button2.setOnClickListener(new View.OnClickListener() {@Overridepublic 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();}
};
这篇关于android中在代码中动态布按钮和画板局并绘制曲线的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!