今天~~ 做了个用canvas
用point划了个抛物线~~效果图~好难看
图片为不同精确度的效果~~~~其实没啥差别
先看代码
import android.os.Bundle;
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.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
class CP extends View {
public CP(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public CP(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);//画笔颜色白色
canvas.rotate(-90);//逆时针旋转画布90度
canvas.translate(-getHeight() + 50, getWidth() / 2);//设置画布圆心在屏幕中线并且据底边50
Paint paint = new Paint();//新建画笔
//设置画笔属性
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.BLUE);
paint.setAntiAlias(true);
canvas.drawCircle(0, 0, 5, paint);
Path path = new Path();//新建路径
path.moveTo(0, 0);//初始位置移动到(0,0)
float x = 0, y = 0;
for (; y < 350; x = (float) (x + 0.1), y = x * x / 4) {
Log.i("TAG", "x=" + x + "y=" + y);
path.lineTo(y, x);//划直线,精确度不是很重要。
}
path.moveTo(0, 0);
for (x = 0, y = 0; y < 350; x = (float) (x - 1), y = x * x / 4) {
float x1 = (float) (x - 1), y1 = x1 * x1 / 4;
Log.i("TAG", "x=" + x + "y=" + y);
path.quadTo(y, x, y1, x1);//划曲线,对比直线
}
canvas.drawPath(path, paint);
//划坐标系
Paint paint1 = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.BLACK);
canvas.drawLine(0, -300, 0, 300, paint1);
canvas.drawLine(0, 0, 500, 0, paint1);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new CP(this));//设置这个画布到activity
}
}