自定义View2 ,自定义路径,bitmap图像处理(缩放,翻转之类,还有Matrix和Mode)
2015-09-17 22:25
387 查看
自定义时钟,四种进度条,自定义路径,bitmap图像处理(缩放,翻转之类,还有Matrix和Mode),仿联系人A-Z选择效果
1,画轨迹
/**
* 画轨迹
*/
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,覆盖问题
/article/1875776.html
/** * 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); } }
相关文章推荐
- VMware Workstation与Hyper-V不兼容。请先从系统中移除Hyper-V角色,然后再运行VMware Workstation
- 类加载器的知识(要补充)
- Java 内存区域和GC机制
- poj1321
- hdu5128The E-pang Palace dfs暴力
- Implement strStr()
- 友盟分享
- 【Java】Java中多态的实现
- STL set的总结
- STL set的总结
- 服务项 开机启动设置
- iOS激情详解之json解析
- 修改应用程序图标和app名称
- Ossimplanet编译笔记(VS2008)
- 关于编码的小知识
- 根据父节点,返回其子节点
- 如何根据父级元素进行相对定位
- html5常见的表单元素
- 如何搭建ssh项目(基于注解)
- [Android Pro] sqlite数据库的char,varchar,text,nchar,nvarchar,ntext的区别