ApiDemo学习之CreateBitmap以及 Paint类介绍

 
private static final int WIDTH = 50;
    private static final int HEIGHT = 50;
    private static final int STRIDE = 64;   // must be >= WIDTH
    
    /**
     * 创建颜色数组,ARGB8888,每一个颜色值都介于(0,255)之间
     * @return
     */
    private static int[] createColors() {
        int[] colors = new int[STRIDE * HEIGHT];
        for (int y = 0; y < HEIGHT; y++) {
            for (int x = 0; x < WIDTH; x++) {
                int r = x * 255 / (WIDTH - 1);
                int g = y * 255 / (HEIGHT - 1);
                int b = 255 - Math.min(r, g);
                int a = Math.max(r, g);
                colors[y * STRIDE + x] = (a << 24) | (r << 16) | (g << 8) | b;
            }
        }
        return colors;
    }
        
    private static class SampleView extends View {
        private Bitmap[] mBitmaps;
        private Bitmap[] mJPEG;
        private Bitmap[] mPNG;
        private int[]    mColors;
        private Paint    mPaint;
        
        /**
         * 将bitmap转话为PNG,或者JPG格式
         * @param src 原始bitmap
         * @param format bitmap的压缩格式
         * @param quality 质量介于(0,100之间),png的时候,基本不考虑这个值,为0
         * @return
         */
        private static Bitmap codec(Bitmap src, Bitmap.CompressFormat format,
                                    int quality) {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            src.compress(format, quality, os);            

            byte[] array = os.toByteArray();//将bitmap转化为2进制流数组
            return BitmapFactory.decodeByteArray(array, 0, array.length);
        }

        public SampleView(Context context) {
            super(context);
            setFocusable(true);
            
            mColors = createColors();
            int[] colors = mColors;

            mBitmaps = new Bitmap[6];
            // these three are initialized with colors[]
            mBitmaps[0] = Bitmap.createBitmap(colors, 0, STRIDE, WIDTH, HEIGHT,
                                              Bitmap.Config.ARGB_8888);
            mBitmaps[1] = Bitmap.createBitmap(colors, 0, STRIDE, WIDTH, HEIGHT,
                                              Bitmap.Config.RGB_565);
            mBitmaps[2] = Bitmap.createBitmap(colors, 0, STRIDE, WIDTH, HEIGHT,
                                              Bitmap.Config.ARGB_4444);
            
            // these three will have their colors set later
            mBitmaps[3] = Bitmap.createBitmap(WIDTH, HEIGHT,
                                              Bitmap.Config.ARGB_8888);
            mBitmaps[4] = Bitmap.createBitmap(WIDTH, HEIGHT,
                                              Bitmap.Config.RGB_565);
            mBitmaps[5] = Bitmap.createBitmap(WIDTH, HEIGHT,
                                              Bitmap.Config.ARGB_4444);
            
            for (int i = 3; i <= 5; i++) {
                mBitmaps[i].setPixels(colors, 0, STRIDE, 0, 0, WIDTH, HEIGHT);
            }
            
            mPaint = new Paint();
            //设定是否使用图像抖动处理,会使绘制出来的图片颜色更加平滑和饱满,
            //图像更加清晰  
            mPaint.setDither(true);  

            // now encode/decode using JPEG and PNG
            mJPEG = new Bitmap[mBitmaps.length];
            mPNG = new Bitmap[mBitmaps.length];
            for (int i = 0; i < mBitmaps.length; i++) {
                mJPEG[i] = codec(mBitmaps[i], Bitmap.CompressFormat.JPEG, 80);
                mPNG[i] = codec(mBitmaps[i], Bitmap.CompressFormat.PNG, 0);
            }
        }
        
        @Override protected void onDraw(Canvas canvas) {
            canvas.drawColor(Color.WHITE);//绘制白色背景

            for (int i = 0; i < mBitmaps.length; i++) {
                canvas.drawBitmap(mBitmaps[i], 0, 0, null);
                canvas.drawBitmap(mJPEG[i], 80, 0, null);
                canvas.drawBitmap(mPNG[i], 160, 0, null);
                canvas.translate(0, mBitmaps[i].getHeight());//移动原点座标
            }
            
            // draw the color array directly, w/o craeting a bitmap object
            canvas.drawBitmap(mColors, 0, STRIDE, 0, 0, WIDTH, HEIGHT,
                              true, null);
            canvas.translate(0, HEIGHT);
            canvas.drawBitmap(mColors, 0, STRIDE, 0, 0, WIDTH, HEIGHT,
                              false, mPaint);
        }
    }

/**  

         *   

     * Paint即画笔,在绘图过程中起到了极其重要的作用,画笔主要保存了颜色,  

     * 样式等绘制信息,指定了如何绘制文本和图形,画笔对象有很多设置方法,  

     * 大体上可以分为两类,一类与图形绘制相关,一类与文本绘制相关。         

     *   

     * 1.图形绘制  

     * setARGB(int a,int r,int g,int b);  

     * 设置绘制的颜色,a代表透明度,r,g,b代表颜色值。  

     *   

     * setAlpha(int a);  

     * 设置绘制图形的透明度。  

     *   

     * setColor(int color);  

     * 设置绘制的颜色,使用颜色值来表示,该颜色值包括透明度和RGB颜色。  

     *   

    * setAntiAlias(boolean aa);  

     * 设置是否使用抗锯齿功能,会消耗较大资源,绘制图形速度会变慢。  

     *   

     * setDither(boolean dither);  

     * 设定是否使用图像抖动处理,会使绘制出来的图片颜色更加平滑和饱满,图像更加清晰  

     *   

     * setFilterBitmap(boolean filter);  

     * 如果该项设置为true,则图像在动画进行中会滤掉对Bitmap图像的优化操作,加快显示  

     * 速度,本设置项依赖于dither和xfermode的设置  

     *   

     * setMaskFilter(MaskFilter maskfilter);  

     * 设置MaskFilter,可以用不同的MaskFilter实现滤镜的效果,如滤化,立体等       *   

     * setColorFilter(ColorFilter colorfilter);  

     * 设置颜色过滤器,可以在绘制颜色时实现不用颜色的变换效果  

     *   

     * setPathEffect(PathEffect effect);  

     * 设置绘制路径的效果,如点画线等  

     *   

     * setShader(Shader shader);  

     * 设置图像效果,使用Shader可以绘制出各种渐变效果  

     *  

     * setShadowLayer(float radius ,float dx,float dy,int color);  

     * 在图形下面设置阴影层,产生阴影效果,radius为阴影的角度,dx和dy为阴影在x轴和y轴上的距离,color为阴影的颜色  

     *   

     * setStyle(Paint.Style style);  

     * 设置画笔的样式,为FILL,FILL_OR_STROKE,或STROKE  

     *   

     * setStrokeCap(Paint.Cap cap);  

     * 当画笔样式为STROKE或FILL_OR_STROKE时,设置笔刷的图形样式,如圆形样式  

     * Cap.ROUND,或方形样式Cap.SQUARE  

     *   

     * setSrokeJoin(Paint.Join join);  

     * 设置绘制时各图形的结合方式,如平滑效果等  

     *   

     * setStrokeWidth(float width);  

     * 当画笔样式为STROKE或FILL_OR_STROKE时,设置笔刷的粗细度  

     *   

     * setXfermode(Xfermode xfermode);  

     * 设置图形重叠时的处理方式,如合并,取交集或并集,经常用来制作橡皮的擦除效果  

     *   

     * 2.文本绘制  

     * setFakeBoldText(boolean fakeBoldText);  

     * 模拟实现粗体文字,设置在小字体上效果会非常差  

     *   

     * setSubpixelText(boolean subpixelText);  

     * 设置该项为true,将有助于文本在LCD屏幕上的显示效果  

     *   

     * setTextAlign(Paint.Align align);  

     * 设置绘制文字的对齐方向  

     *   

   * setTextScaleX(float scaleX);  

    * 设置绘制文字x轴的缩放比例,可以实现文字的拉伸的效果  

     *   

     * setTextSize(float textSize);  

     * 设置绘制文字的字号大小  

     *   

     * setTextSkewX(float skewX);  

     * 设置斜体文字,skewX为倾斜弧度  

     *   

     * setTypeface(Typeface typeface);  

     * 设置Typeface对象,即字体风格,包括粗体,斜体以及衬线体,非衬线体等  

     *   

     * setUnderlineText(boolean underlineText);  

     * 设置带有下划线的文字效果  

     *   

     * setStrikeThruText(boolean strikeThruText);  

     * 设置带有删除线的效果  

     *   

     */ 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章