Android(安卓)Canvas
16lz
2022-05-29
今天趁有空熟悉一下Canvas基本绘图方法:
public class MyView extends View{ private Bitmap bm; private Paint paint; private Rect srcRect; private Rect dstRect; public MyView(Context context) { super(context); // TODO Auto-generated constructor stub } public MyView(Context context,AttributeSet set) { super(context,set); bm = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); paint = new Paint(); srcRect = new Rect(0,0,50,50); dstRect = new Rect(200,200,250,250); } /* (non-Javadoc) * @see android.view.View#onDraw(android.graphics.Canvas) */ @SuppressLint("DrawAllocation") @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); paint.setAlpha(125); paint.setColor(Color.BLACK); paint.setAntiAlias(true);//低分辨率放到高分辨的屏幕的时候,图片会产生锯齿,设置抗锯齿可以柔化边缘 canvas.drawColor(Color.WHITE);//绘制背景 canvas.drawBitmap(bm, 0, 0, paint);//左上角完全显示+透明度 canvas.drawBitmap(bm,50,50, paint);//左上角偏离左边框50px,上边框50px完全显示+透明度 canvas.drawBitmap(bm,100,100,null);//左上角偏离左边框100px,上边框100px完全显示+透明度 canvas.drawBitmap(bm,srcRect,dstRect,null);//从bmp中挖去50*50中放到屏幕Piont(200,200)处 /** * DrawCircle */ canvas.drawCircle(400,50,50,paint); paint.setColor(Color.RED); paint.setAntiAlias(false); canvas.drawCircle(500,50 ,50, paint); /** * drawLine */ paint.setColor(Color.BLACK); paint.setAntiAlias(true); canvas.drawLine(350, 150, 700, 150, paint); paint.setColor(Color.RED); paint.setAntiAlias(false); canvas.drawLine(350, 200, 700, 200, paint); /** * drawRect */ paint.setColor(Color.BLACK); paint.setAntiAlias(true); paint.setStyle(Style.STROKE);//填充风格->点填充 canvas.drawRect(new Rect(350, 250, 500, 300), paint); paint.setStyle(Style.FILL);//填充风格->全部填充 canvas.drawRect(new Rect(500, 250, 650, 300), paint); //RoundRect //RectF->float型 /** * 第一个10是在X轴的半径 * 第二个10是在Y轴的半径 */ canvas.drawRoundRect(new RectF(350, 300, 500, 350), 10, 10, paint); //椭圆 canvas.drawOval(new RectF(350, 350, 500, 400) , paint); /** * drawText */ paint.setTextSize(24); canvas.drawText("HELLO", 0,300, paint); /** * drawTextOnPath */ Path path = new Path(); path.moveTo(26,360); path.lineTo(54, 360); path.lineTo(70, 392); path.lineTo(40, 420); path.lineTo(10, 392); path.close(); canvas.drawPath(path,paint); /** * 第一个0是修改起始字符距离Path第一个点的距离 * 第二个0是->负数的话,远离Path;正数的话会看到字符串嵌在Path中 * 因为Android坐标系以左上角为起点,-10就是往上移动 */ canvas.drawTextOnPath("abcdefgh", path, 0,-10, paint); }}
实际效果如下:
更多相关文章
- Android(安卓)AlertDialog去除白色边框
- Android用开发者模式进行调试时出现的问题及解决方案
- android editText 自定义边框
- Android(安卓)studio3 报错module not specified解决方案
- android刮刮乐
- NestedScrollView中添加一个android:fillViewport="true"
- Android(安卓)登录界面Demo源码
- imageButton被边框包围的原因、解决办法
- Android快速SDK(15)二维码扫描Scanner【傻瓜模式】