Android图形图画学习(10)——AlphaBitmap
16lz
2021-01-23
private static class SampleView extends View { private Bitmap mBitmap; private Bitmap mBitmap2; private Bitmap mBitmap3; private Shader mShader; private static void drawIntoBitmap(Bitmap bm) { //正如在图形图画学习(1)中提到的,画图有两种,一种是在空白画图上作画,一种是在已知的一张Bitmap上作画。这里展示了如何在一张Bitmap上作画 float x = bm.getWidth(); float y = bm.getHeight(); Canvas c = new Canvas(bm); Paint p = new Paint(); //组成图画的基本单元由画笔决定,例如,透明度,色彩,抖动等等。 p.setAntiAlias(true); p.setAlpha(0x80); c.drawCircle(x/2, y/2, x/2, p); //Canvas类是画图工具类,这是一个画圆工具 p.setAlpha(0x30); p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC)); p.setTextSize(60); p.setTextAlign(Paint.Align.CENTER); Paint.FontMetrics fm = p.getFontMetrics(); c.drawText("Alpha", x/2, (y-fm.ascent)/2, p); //这是一个绘制文字的工具 } public SampleView(Context context) { super(context); setFocusable(true); InputStream is = context.getResources().openRawResource(R.drawable.app_sample_code); mBitmap = BitmapFactory.decodeStream(is); mBitmap2 = mBitmap.extractAlpha(); mBitmap3 = Bitmap.createBitmap(200, 200, Bitmap.Config.ALPHA_8); drawIntoBitmap(mBitmap3); mShader = new LinearGradient(0, 0, 100, 70, new int[] { //着色器实现了渐变色彩,仍然交给画笔处理 Color.RED, Color.GREEN, Color.BLUE }, null, Shader.TileMode.MIRROR); } @Override protected void onDraw(Canvas canvas) { canvas.drawColor(Color.WHITE); Paint p = new Paint(); float y = 10; p.setColor(Color.RED); canvas.drawBitmap(mBitmap, 10, y, p);//最终位图依然依托于View的onDraw()回调交由系统画布绘制出来 y += mBitmap.getHeight() + 10; canvas.drawBitmap(mBitmap2, 10, y, p); y += mBitmap2.getHeight() + 10; p.setShader(mShader); canvas.drawBitmap(mBitmap3, 10, y, p); } }
更多相关文章
- android 开发过程中对网络状态的判断的工具类
- android 在画布上画图片
- Android干货福利,常用工具总结
- 三分钟学会缓存工具DiskLruCache
- android HttpClient get请求与post请求工具类
- android调试工具DDMS的使用详解
- android程序发布时的常用工具
- 转载:为Android加入busybox工具