Android CreateBitmap

本文介绍了一个使用自定义位图类创建不同格式图像,并通过编码解码过程将其转换为JPEG和PNG格式的方法。

  1. public class CreateBitmap extends Activity{  
  2.     @Override  
  3.     protected void onCreate(Bundle savedInstanceState) {  
  4.         // TODO Auto-generated method stub  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(new SampleView(this));  
  7.     }  
  8.       
  9.     private class SampleView extends View{  
  10.         private Bitmap[] mBitmaps;  
  11.         private Bitmap[] mJPEGs;  
  12.         private Bitmap[] mPNGs;  
  13.         private int[] mColors;  
  14.         private Paint mPaint;  
  15.           
  16.         private static final int WIDTH=50;  
  17.         private static final int HEIGHT=50;  
  18.         private static final int STRIDE=64;//must be >=WIDTH  
  19.         public SampleView(Context context) {  
  20.             super(context);  
  21.             setFocusable(true);  
  22.               
  23.             mColors=initColors();  
  24.             mBitmaps=new Bitmap[6];  
  25.             mPaint=new Paint();  
  26.             mPaint.setDither(true);  
  27.               
  28.             //these three are initialized with colors[]  
  29.             mBitmaps[0]=Bitmap.createBitmap(mColors, 0, STRIDE, WIDTH, HEIGHT, Config.ARGB_8888);  
  30.             mBitmaps[1]=Bitmap.createBitmap(mColors, 0, STRIDE, WIDTH, HEIGHT, Config.RGB_565);  
  31.             mBitmaps[2]=Bitmap.createBitmap(mColors, 0, STRIDE, WIDTH, HEIGHT, Config.ARGB_4444);  
  32.               
  33.             //these three will have their colors[] set later.  
  34.             mBitmaps[3]=Bitmap.createBitmap(WIDTH, HEIGHT, Config.ARGB_8888);  
  35.             mBitmaps[4]=Bitmap.createBitmap(WIDTH, HEIGHT, Config.RGB_565);  
  36.             mBitmaps[5]=Bitmap.createBitmap(WIDTH, HEIGHT, Config.ARGB_4444);  
  37.               
  38.             for (int i = 3; i <=5; i++) {  
  39.                 mBitmaps[i].setPixels(mColors, 0, STRIDE, 00, WIDTH, HEIGHT);  
  40.             }  
  41.               
  42.             //now decode/encode using JPEG and PNG  
  43.             mJPEGs=new Bitmap[mBitmaps.length];  
  44.             mPNGs=new Bitmap[mBitmaps.length];  
  45.             for (int i = 0; i < mBitmaps.length; i++) {  
  46.                 mJPEGs[i]=codec(mBitmaps[i],Bitmap.CompressFormat.JPEG,80);  
  47.                 mPNGs[i]=codec(mBitmaps[i],Bitmap.CompressFormat.PNG,0);  
  48.             }  
  49.         }  
  50.           
  51.         @Override  
  52.         protected void onDraw(Canvas canvas) {  
  53.             // TODO Auto-generated method stub  
  54.             super.onDraw(canvas);  
  55.             canvas.drawColor(Color.WHITE);  
  56.               
  57.             for (int i = 0; i < mBitmaps.length; i++) {  
  58.                 canvas.drawBitmap(mBitmaps[i], 00null);  
  59.                 canvas.drawBitmap(mJPEGs[i], 80,0null);  
  60.                 canvas.drawBitmap(mPNGs[i], 1600null);  
  61.                 canvas.translate(0, mBitmaps[i].getHeight());  
  62.             }  
  63.               
  64.             //draw the color array directly,w/o(without) creating a bitmap object  
  65.             canvas.drawBitmap(mColors, 0, STRIDE, 00, WIDTH, HEIGHT, truenull);  
  66.             canvas.translate(0, HEIGHT);  
  67.             canvas.drawBitmap(mColors, 0, STRIDE, 00, WIDTH, HEIGHT, true, mPaint);  
  68.         }  
  69.         private Bitmap codec(Bitmap bitmap, CompressFormat format, int quality) {  
  70.             ByteArrayOutputStream baos=new ByteArrayOutputStream();  
  71.             bitmap.compress(format, quality, baos);  
  72.             byte[] data=baos.toByteArray();  
  73.             return BitmapFactory.decodeByteArray(data, 0, data.length);  
  74.         }  
  75.         private int[] initColors() {  
  76.             int[] colors=new int[STRIDE*HEIGHT];  
  77.             for (int y = 0; y < HEIGHT; y++) {//use of x,y is legible then the use of i,j  
  78.                 for (int x = 0; x < WIDTH; x++) {  
  79.                     int r = x * 255 / (WIDTH - 1);  
  80.                     int g = y * 255 / (HEIGHT - 1);  
  81.                     int b = 255 - Math.min(r, g);  
  82.                     int a = Math.max(r, g);  
  83.                     colors[y*STRIDE+x]=(a<<24)|(r<<16)|(g<<8)|(b);//the shift operation generates the color ARGB  
  84.                 }  
  85.             }  
  86.             return colors;  
  87.         }  
  88.           
  89.     }  
  90. }  
 

Bitmap中setPiexls的解释:

 

public void setPixels (int[] pixels, int offset, int stride, int x, int y, int width, int height)
Since:  API Level 1

Replace pixels in the bitmap with the colors in the array. Each element in the array is a packed int prepresenting a Color

Parameters
pixelsThe colors to write to the bitmap
offsetThe index of the first color to read from pixels[]
strideThe number of colors in pixels[] to skip between rows. Normally this value will be the same as the width of the bitmap, but it can be larger (or negative).
xThe x coordinate of the first pixel to write to in the bitmap.
yThe y coordinate of the first pixel to write to in the bitmap.
widthThe number of colors to copy from pixels[] per row
heightThe number of rows to write to the bitmap
Throws
IllegalStateExceptionif the bitmap is not mutable
IllegalArgumentExceptionif x, y, width, height are outside of the bitmap's bounds.
ArrayIndexOutOfBoundsExceptionif the pixels array is too small to receive the specified number of pixels.
生成颜色值数组的函数:
  1. private int[] initColors() {  
  2.             int[] colors=new int[STRIDE*HEIGHT];  
  3.             for (int y = 0; y < HEIGHT; y++) {//use of x,y is legible then the use of i,j  
  4.                 for (int x = 0; x < WIDTH; x++) {  
  5.                     int r = x * 255 / (WIDTH - 1);  
  6.                     int g = y * 255 / (HEIGHT - 1);  
  7.                     int b = 255 - Math.min(r, g);  
  8.                     int a = Math.max(r, g);  
  9.                     colors[y*STRIDE+x]=(a<<24)|(r<<16)|(g<<8)|(b);//the shift operation generates the color ARGB  
  10.                 }  
  11.             }  
  12.             return colors;  
  13.         }  
 

 


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值