`
老七的米店
  • 浏览: 46702 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Api Demo - .graphics(9)

 
阅读更多
//关键字:颜色合成,JPEG,PNG图片解压,Bitmap压缩。

package com.example.android.apis.graphics;

import com.example.android.apis.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.*;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.*;

import java.io.ByteArrayOutputStream;

public class CreateBitmap extends GraphicsActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new SampleView(this));
    }
    
    private static final int WIDTH = 50;
    private static final int HEIGHT = 50;
    private static final int STRIDE = 64;   // must be >= WIDTH
    
    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;
    }
    /*
     * 设alpha,red,green,blue都是0~255之间的数
     * color32 = alpha << 24 | red << 16 | green << 8 | blue;
     * 色彩提取:
     * alpha = color32 >> 24;
     * red = color32 >> 16 & 0xFF;
     * green = color32 >> 8 & 0xFF;
     * blue = color32 & 0xFF;
     */
        
    private static class SampleView extends View {
        private Bitmap[] mBitmaps;
        private Bitmap[] mJPEG;
        private Bitmap[] mPNG;
        private int[]    mColors;
        private Paint    mPaint;
        
        private static Bitmap codec(Bitmap src, Bitmap.CompressFormat format,
                                    int quality) {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            src.compress(format, quality, os);  //将Bitmap压缩成图片文件。       

            byte[] array = os.toByteArray();
            return BitmapFactory.decodeByteArray(array, 0, array.length);//将byte数组解压为Bitmap;
        }

         /*
         *  如果返回true,可以通过传递一个相应的输出流到BitmapFactory.decodeStream()来重构该位图。
         *  注意:并非所有的格式都直接支持位图结构,
         *   format  图像的压缩格式;
         *   quality 图像压缩比的值,0-100。 0 意味着小尺寸压缩,100意味着高质量压缩。对于有些格式,比如无损压缩的PNG,它就会忽视quality这个参数设置。
         *   stream  写入压缩数据的输出流 
         */

boolean compress(Bitmap.CompressFormat format, int quality, OutputStream stream) 

        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);
            
            //构造方法中直传COLORS数组。
            // 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);
            }
            //通过setPixels传入;
            
            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);

            //将Bitmap压缩成JPEG和PNG,再解压成Bitmap 的显示效果不同。JPEG 只支持不透明像素
                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);
        }
    }
}

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics