自定义View2 ,自定义路径,bitmap图像处理(缩放,翻转之类,还有Matrix和Mode)

自定义时钟,四种进度条,自定义路径,bitmap图像处理(缩放,翻转之类,还有Matrix和Mode),仿联系人A-Z选择效果
1,画轨迹
/**
画轨迹
*/
public class DrawPath extends View {
private int width;
private int height;
private Paint mPaintBackGround;
private Paint mPaintLine;
private Paint mPaintText;
private float textSize = 70; //textSize默认值等于70
private Path pathTriangle;
private Path pathBEZIER;
private Path pathWave;
private int count;
private Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(count<120){
count+=5;
}else {
count=0;
}
invalidate();
mHandler.sendEmptyMessageDelayed(0x11,50);
}
};

public void setTextSize(float textSize) {
this.textSize = textSize;
}


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

public DrawPath(Context context, AttributeSet attrs) {
super(context, attrs);
mPaintBackGround = new Paint();
mPaintBackGround.setColor(Color.BLUE);
mPaintBackGround.setStrokeWidth(20);
mPaintBackGround.setStyle(Paint.Style.STROKE);

mPaintLine = new Paint();
mPaintLine.setColor(Color.BLACK);
mPaintLine.setStrokeWidth(10);
mPaintLine.setStyle(Paint.Style.STROKE);

mPaintText =new Paint();
mPaintText.setTextSize(10);

pathTriangle = new Path();
pathBEZIER = new Path();
pathWave = new Path();

mHandler.sendEmptyMessage(0x11);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
mPaintText.setTextSize(textSize);
}


@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

pathTriangle.moveTo(200, 100);
pathTriangle.lineTo(0, 400);
pathTriangle.lineTo(400, 400);
pathTriangle.close();
canvas.drawPath(pathTriangle, mPaintBackGround);
// canvas.drawTextOnPath();
pathBEZIER.moveTo(500, 60);
pathBEZIER.quadTo(500, 600, 1000, 400); //绘制贝塞尔曲线
canvas.drawPath(pathBEZIER, mPaintLine);
canvas.drawPoint(500, 60, mPaintBackGround);
canvas.drawPoint(500, 600, mPaintBackGround);
canvas.drawPoint(1000, 400, mPaintBackGround);
canvas.drawTextOnPath("贝塞尔曲线", pathBEZIER, 0, -10, mPaintText);

/**
* 绘制波浪线
*/
pathWave.reset();
pathWave.moveTo(count, 1200);
for(int i=0;i<11;i++) {
pathWave.rQuadTo(30, 30, 60, 0);
pathWave.rQuadTo(30, -30, 60, 0);
}
canvas.drawPath(pathWave,mPaintLine);


/**
* 绘制圆形背景
*/
canvas.drawCircle(width/2,1200,200,mPaintBackGround);
}
}
2,bitmap图像处理
matrix:详解
http://blog.csdn.net/flash129/article/details/8234599
/**
* bitmap图像的操作
*/
public class MyBitmapView extends View{
private Bitmap mBitmap;
private Paint mPaint;
private Matrix matrix;
private float mBitmapWidth;
private float mBitmapHeight;
public MyBitmapView(Context context) {
super(context);
}

public MyBitmapView(Context context, AttributeSet attrs) {
super(context, attrs);
mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.gece);
mBitmapWidth = mBitmap.getWidth();
mBitmapHeight =mBitmap.getHeight();
mPaint = new Paint();
matrix = new Matrix();
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
matrix.reset();
matrix.postScale(0.5f, 0.5f); //放大缩小
canvas.drawBitmap(mBitmap, matrix, mPaint);

matrix.postTranslate(0, mBitmapHeight * 0.5f); //平移
canvas.drawBitmap(mBitmap, matrix, mPaint);

matrix.reset();
matrix.postScale(0.5f, 0.5f); //放大缩小
matrix.postRotate(180); //旋转
matrix.postTranslate(mBitmapWidth, mBitmapHeight);
canvas.drawBitmap(mBitmap, matrix, mPaint);

// matrix.reset();
// matrix.postScale(0.5f, 0.5f); //放大缩小
// // 9. 对称 - 垂直
// float matrix_values[] = {-1f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 1f};
// matrix.setValues(matrix_values);
// canvas.drawBitmap(mBitmap, matrix, mPaint);
}
}
3,覆盖问题
http://blog.csdn.net/t12x3456/article/details/10432935

/**
* Created by Administrator on 2015/9/17.
*/
public class MyBitmapView2 extends View{
private Bitmap mBitmap;
private Paint mPaintRect;
private Paint mPaintCircle;
private int width;
private int height;
private Canvas mCanvasBitmap;
public MyBitmapView2(Context context) {
super(context);
}

public MyBitmapView2(Context context, AttributeSet attrs) {
super(context, attrs);
mPaintRect = new Paint();
mPaintRect.setColor(Color.GREEN);
mPaintCircle = new Paint();
mPaintCircle.setColor(Color.YELLOW);
PorterDuffXfermode xfermode = new PorterDuffXfermode(PorterDuff.Mode.XOR);
mPaintRect.setXfermode(xfermode);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
mBitmap = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
mCanvasBitmap = new Canvas(mBitmap);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.GRAY);
mCanvasBitmap.drawCircle(width/2,height/2,300,mPaintCircle);
mCanvasBitmap.drawRect(0,0,width/2,height/2,mPaintRect);
canvas.drawBitmap(mBitmap,0,0,null);
}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值