http://meteor6789.blog.163.com/blog/static/35040733201111193535153/
Piant
看一段代码:
mPaint = new Paint();
mPaint.setAntiAlias(true);//锯齿
mPaint.setDither(true);//
mPaint.setColor(0xFF385078);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);//文档上的大概意思是设置连接处
mPaint.setStrokeCap(Paint.Cap.ROUND);//文档上的大概意思是设置帽子 帽子应该是:比如画一条直线,那么cap就是指直线的头Or顶端
mPaint.setStrokeWidth(20);//画笔的粗细
样式 :
paint有可以直接画一个矩形、各种圆、三角形等的方法,但是没有设置笔触样式的方法,所以如果想在paint上绑定一个图形,可以参考一下方法:
import android.content.Context;
import android.graphics.*;
import android.graphics.Path.Direction;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
public class FingerPaint extends GraphicsActivity
implements ColorPickerDialog.OnColorChangedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new MyView(this));
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0xFF385078);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(20);
mEmboss = new EmbossMaskFilter(new float[] { 0, 1, 1},
0.2f, 6, 3.5f);
mBlur = new BlurMaskFilter(1, BlurMaskFilter.Blur.NORMAL);
}
private Paint mPaint;
private MaskFilter mEmboss;
private MaskFilter mBlur;
public void colorChanged(int color) {
mPaint.setColor(color);
}
public class MyView extends View {
private static final float MINP = 0.25f;
private static final float MAXP = 0.75f;
private Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
public MyView(Context c) {
super(c);
mBitmap = Bitmap.createBitmap(480, 320, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(0xFFAAAAAA);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaint);
}
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
private void touch_start(float x, float y) {
mPath.reset();
mPath.addCircle(x,y,50,Direction.CW);
mX = x;
mY = y;
}
private void touch_move(float x, float y) {
mPath.addCircle(x,y,50,Direction.CW);
}
private void touch_up() {
mCanvas.drawPath(mPath, mPaint);
mPath.reset();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}
}
粗细:
mPaint.setStrokeWidth(20);//画笔的粗细
硬度:
mPaint.setMaskFilter(XXX);
XXX处有两种可以设置:
mEmboss = new EmbossMaskFilter(new float[] { 0, 1, 1},
0.2f, 6, 3.5f);//指定了光源的方向和环境光强度来添加浮雕效果。三个参数分别是光源的方向(具体的x,y,z代表从哪个方向还未研究 )、环境光的亮度、要应用的反射等级、向mask应用一定级别的模糊
mBlur = new BlurMaskFilter(1, BlurMaskFilter.Blur.NORMAL);//指定了一个模糊的样式和半径来处理Paint的边缘。第一个参数文档上说是半径,大概就是PS软件中硬度的半径大小吧,其实就是硬度的强度大小,第二个参数有三种NORMAL\INNER\OUT
Path
Path路径类在位于android.graphics.Path中,Path的构造方法比较简单。
Java代码:
Path cwj=new Path(); //构造方法
复制代码
下面我们画一个封闭的原型路径,我们使用Path类的addCircle方法。
Java代码:
cwj.addCircle(10,10,50,Direction.CW);
//参数一为x轴水平位置,参数二为y轴垂直位置,第三个参数为圆形的半径,最后是绘制的方向,CW为顺时针方向,而CCW是逆时针方向。
复制代码
我们如何在onDraw方法中如何绘制路径
Java代码:
@Override
protected void onDraw(Canvas canvas)
{
Paint paintPath=new Paint();
Paint paintText=new Paint();
paintPath.setColor(Color.Red); //路径的画刷为红色
paintText.setColor(Color.Blue); //路径上的文字为蓝色
Path pathCWJ=new Path();
pathCWJ.addCircle(10,10,50,Direction.CW); // 半径为50px,绘制的方向CW为顺时针
canvas.drawPath(pathCWJ,paintPath);
canvas.drawTextOnPath("Android123 - CWJ",pathCWJ,0,15,paintText); //在路径上绘制文字
}
void addArc(RectF oval, float startAngle, float sweepAngle) //为路径添加一个多边形
void addCircle(float x, float y, float radius, Path.Direction dir) //给path添加圆圈
void addOval(RectF oval, Path.Direction dir) //添加椭圆形
void addRect(RectF rect, Path.Direction dir) //添加一个区域
void addRoundRect(RectF rect, float[] radii, Path.Direction dir) //添加一个圆角区域
boolean isEmpty() //判断路径是否为空
void transform(Matrix matrix) //应用矩阵变换
void transform(Matrix matrix, Path dst) //应用矩阵变换并将结果放到新的路径中,即第二个参数。
复制代码
旋转
第一种:
import java.io.InputStream;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.View;
/*
* 旋转图像是通过Matrix类得setRotate方法设置要旋转的角度(正值为顺时针旋转,负值为逆时针旋转),
* 然后使用canvas.drawBitmap绘制旋转后的图像。利用invalidate方法不短重绘View,不断调用onDraw方法实现图像不停的旋转的动画
*/
public class Main extends Activity {
class MyView extends View {
private Bitmap bitmap1;
private Bitmap bitmap2;
private int digree1 = 0;// 扳手图像的当前角度
private int digree2 = 360;// 小球的当前角度
public MyView(Context context) {
super(context);
setBackgroundColor(Color.WHITE);
//装载图像资源并获得InputStream对象
InputStream is = getResources().openRawResource(R.drawable.cross);
//使用 BitmapFactory.decodeStream方法将InputStream解码成Bitmap对象
bitmap1 = BitmapFactory.decodeStream(is);
is = getResources().openRawResource(R.drawable.ball);
bitmap2 = BitmapFactory.decodeStream(is);
}
@Override
protected void onDraw(Canvas canvas) {
Matrix matrix = new Matrix();
// 控制旋转角度在0-360之间
if (digree1 > 360)
digree1 = 0;
if (digree2 < 0)
digree2 = 360;
// 设置扳手图像的旋转角度和旋转轴心坐标(后两个参数,注意这个坐标是相对于屏幕的),该轴心也是图像的正中心
matrix.setRotate(digree1++, 160, 250);
canvas.setMatrix(matrix);
//绘制扳手图像
canvas.drawBitmap(bitmap1, 88, 169, null);
// 设置小球的旋转角度和旋转轴心坐标
matrix.setRotate(digree2--, 160, 240);
canvas.setMatrix(matrix);
//绘制小球图像
canvas.drawBitmap(bitmap2, 35, 115, null);
// 不短重绘View,不断调用onDrow方法
invalidate();
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
}
此种的缺点是整个画布都在旋转
第二种
import java.io.InputStream;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
public class PaintActivity extends Activity {
int index = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
class MyView extends View {
private Paint paint;
private Bitmap mBitmap;
private Bitmap resizedBitmap;
private Matrix mMatrix = new Matrix();
public MyView(Context context) {
super(context);
paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
initialize();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
// canvas.drawBitmap(resizedBitmap, 100, 100, paint);
canvas.drawBitmap(mBitmap, mMatrix, paint);
}
private void initialize() {
// mBitmap = BitmapFactory.decodeResource(getResources(),
// R.drawable.icon);
InputStream is = getResources().openRawResource(R.drawable.cross);
mBitmap = BitmapFactory.decodeStream(is);
mMatrix.postRotate(0, 200, 200);// 旋转的角度
//三个参数分别是旋转的角度,和旋转轴x,y
mMatrix.postTranslate(100, 100);
//在两个方向平移x,y长度
// resizedBitmap = Bitmap.createBitmap(mBitmap, 50,50,
//
// mBitmap.getWidth(), mBitmap.getHeight(), mMatrix, true);
}
}
}
此种的缺点是在canvas.drawBitmap(mBitmap, mMatrix, paint);中没有画图的位置的参数 默认是(0,0),如果想画在其他地方,只能通过mMatrix平移