Android自定义控件画小猪佩奇

无特殊属性,直接引用在布局文件里面就行

//控件类
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.util.AttributeSet;
import android.view.View;

public class CustomPig extends View {

private float mLength;
private float mStop = 0;
private PathMeasure mPathMeasure;
private Path mPath;
private float mRadius = 70;
private Paint mPaint;

private Paint mEyePaint;
private Paint mEyeBallPaint;
private Path mShellPath;

public CustomPig(Context context) {
super(context);
}

public CustomPig(Context context, AttributeSet attrs) {
super(context, attrs);
initPaint();
}

private void initPaint() {
mPaint = new Paint();
mPaint.setColor(Color.parseColor("#FF69B4"));
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(6);
mPaint.setAntiAlias(true);

mEyePaint = new Paint();
mEyePaint.setColor(Color.parseColor("#F0F8FF"));
mEyePaint.setAntiAlias(true);

mEyeBallPaint = new Paint();
mEyeBallPaint.setColor(Color.BLACK);
mEyeBallPaint.setAntiAlias(true);
}

@Override
public void draw(Canvas canvas) {
super.draw(canvas);

drawShell(canvas);

drawNose(canvas);

drawEye(canvas);

drawMouth(canvas);

drawEars(canvas);

}

private void drawEars(Canvas canvas) {
Path earPath = new Path();
earPath.moveTo(400, 60);
earPath.cubicTo(410, 5, 460, 5, 470, 70);

earPath.cubicTo(480, 10, 530, 10, 540, 100);

drawAll(canvas, earPath);
}

private void drawAll(Canvas canvas, Path path) {
Path dst = new Path();
mPathMeasure = new PathMeasure(path, false);
mLength = mPathMeasure.getLength();
mPathMeasure.getSegment(0, mStop, dst, true);
canvas.drawPath(dst, mPaint);
if (mStop < mLength) {
if (path == mShellPath) {
mStop += mLength / 200;
} else {
mStop += mLength / 100000;
}
invalidate();
}
}

private void drawMouth(Canvas canvas) {
Path mouthPath = new Path();
mouthPath.moveTo(550, 600);
mouthPath.quadTo(450, 600, 450, 500);

drawAll(canvas, mouthPath);
}

private void drawEye(Canvas canvas) {
Path eyePath = new Path();
eyePath.addCircle(350, 100, 40, Path.Direction.CW);
eyePath.addCircle(420, 120, 40, Path.Direction.CW);
canvas.drawPath(eyePath, mEyePaint);
canvas.drawPath(eyePath, mPaint);

Path eyeBallPath = new Path();
eyeBallPath.addCircle(370, 100, 20, Path.Direction.CW);
eyeBallPath.addCircle(440, 120, 20, Path.Direction.CW);
canvas.drawPath(eyeBallPath, mEyeBallPaint);

}

private void drawShell(Canvas canvas) {
mShellPath = new Path();
mShellPath.moveTo(200, 100 - mRadius);
mShellPath.cubicTo(800, 100 - mRadius, 1000, 600, 600, 700);

mShellPath.cubicTo(300, 700, 300, 280, 350, 250);
mShellPath.quadTo(200, 210, 200, 100 + mRadius);

drawAll(canvas, mShellPath);

}

private void drawNose(Canvas canvas) {
mPath = new Path();
Path nosePath = new Path();
mPath.addCircle(200, 100, mRadius, Path.Direction.CW);
nosePath.addCircle(180, 100, 15, Path.Direction.CW);
nosePath.addCircle(220, 100, 15, Path.Direction.CW);

canvas.drawPath(nosePath, mPaint);

drawAll(canvas, mPath);
}
}

梦雪

专注于AIDE教程分享

留下你的评论

*评论支持代码高亮<pre class="prettyprint linenums">代码</pre>

相关推荐