我的android绘图学习笔记(一)
2010-11-04
偶打算绘制一个坐标轴,画一个折线图,一开始用直线画出了坐标轴,但是不太好看
先贴上代码
/** testPaint.java**/
package com.test; 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; public class testPaint extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); MyView myview = new MyView(this); setContentView(myview); } private class MyView extends View { @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(Color.WHITE); Paint paint =new Paint(); paint.setAntiAlias(true); paint.setColor(Color.BLACK); Paint paint1=new Paint(); paint1.setColor(Color.RED); canvas.drawLine(30, 160, 300, 160, paint); canvas.drawLine(30, 160, 30, 30, paint); canvas.drawLine(30, 30, 24, 36, paint); canvas.drawLine(30, 30, 36, 36, paint); canvas.drawLine(300, 160, 294, 154, paint); canvas.drawLine(300, 160,294, 166, paint); paint.setTextSize(12); canvas.drawText(getResources().getString(R.string.date), 280, 180, paint); canvas.drawText(getResources().getString(R.string.product), 1, 40, paint); } public MyView(Context context) { super(context); } } }
/**strings.xml*/
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, testPaint!</string> <string name="app_name">testPaint</string> <string name="date">天</string> <string name="product">产量</string> </resources>
运行效果
后来找了一种填充多边形的方法画箭头
/**testPaint.java*/
package com.test; import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.os.Bundle; import android.view.View; public class testPaint extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); MyView myview = new MyView(this); setContentView(myview); } private class MyView extends View { @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(Color.WHITE); Paint paint =new Paint(); paint.setAntiAlias(true); paint.setColor(Color.BLACK); Paint paint1=new Paint(); paint1.setColor(Color.RED); canvas.drawLine(30, 160, 300, 160, paint); canvas.drawLine(30, 160, 30, 30, paint); Path path=new Path(); path.moveTo(30, 30); path.lineTo(25,35); path.lineTo(30, 25); path.lineTo(35, 35); canvas.drawPath(path, paint); Path path1=new Path(); path1.moveTo(300, 160); path1.lineTo(295,155); path1.lineTo(305, 160); path1.lineTo(295, 165); canvas.drawPath(path1, paint); paint.setTextSize(12); canvas.drawText(getResources().getString(R.string.date), 280, 180, paint); canvas.drawText(getResources().getString(R.string.product), 1, 40, paint); } public MyView(Context context) { super(context); } } }
运行效果
这样看起来好看多了,哈哈