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);        }    }


更多相关文章

  1. android 开发过程中对网络状态的判断的工具类
  2. android 在画布上画图片
  3. Android干货福利,常用工具总结
  4. 三分钟学会缓存工具DiskLruCache
  5. android HttpClient get请求与post请求工具类
  6. android调试工具DDMS的使用详解
  7. android程序发布时的常用工具
  8. 转载:为Android加入busybox工具

随机推荐

  1. Android JNI 实例
  2. Android(安卓)M 新的运行时权限开发者需
  3. Android 进入界面禁止EditText弹出系统键
  4. Android的StrictMode
  5. Spring for Android 1.0.0发布
  6. android中禁止横屏和竖屏切换
  7. android之网格布局和线性布局实现注册页
  8. 修改Android(安卓)Media Scanner的扫描路
  9. android点击空白处或者其他控件的时候隐
  10. android api (82) ―― InputConnection