当前位置:   article > 正文

Android CreateBitmap

android createbitmap

public class CreateBitmap extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(new SampleView(this)); } private class SampleView extends View{ private Bitmap[] mBitmaps; private Bitmap[] mJPEGs; private Bitmap[] mPNGs; private int[] mColors; private Paint mPaint; private static final int WIDTH=50; private static final int HEIGHT=50; private static final int STRIDE=64;//must be >=WIDTH public SampleView(Context context) { super(context); setFocusable(true); mColors=initColors(); mBitmaps=new Bitmap[6]; mPaint=new Paint(); mPaint.setDither(true); //these three are initialized with colors[] mBitmaps[0]=Bitmap.createBitmap(mColors, 0, STRIDE, WIDTH, HEIGHT, Config.ARGB_8888); mBitmaps[1]=Bitmap.createBitmap(mColors, 0, STRIDE, WIDTH, HEIGHT, Config.RGB_565); mBitmaps[2]=Bitmap.createBitmap(mColors, 0, STRIDE, WIDTH, HEIGHT, Config.ARGB_4444); //these three will have their colors[] set later. mBitmaps[3]=Bitmap.createBitmap(WIDTH, HEIGHT, Config.ARGB_8888); mBitmaps[4]=Bitmap.createBitmap(WIDTH, HEIGHT, Config.RGB_565); mBitmaps[5]=Bitmap.createBitmap(WIDTH, HEIGHT, Config.ARGB_4444); for (int i = 3; i <=5; i++) { mBitmaps[i].setPixels(mColors, 0, STRIDE, 0, 0, WIDTH, HEIGHT); } //now decode/encode using JPEG and PNG mJPEGs=new Bitmap[mBitmaps.length]; mPNGs=new Bitmap[mBitmaps.length]; for (int i = 0; i < mBitmaps.length; i++) { mJPEGs[i]=codec(mBitmaps[i],Bitmap.CompressFormat.JPEG,80); mPNGs[i]=codec(mBitmaps[i],Bitmap.CompressFormat.PNG,0); } } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); canvas.drawColor(Color.WHITE); for (int i = 0; i < mBitmaps.length; i++) { canvas.drawBitmap(mBitmaps[i], 0, 0, null); canvas.drawBitmap(mJPEGs[i], 80,0, null); canvas.drawBitmap(mPNGs[i], 160, 0, null); canvas.translate(0, mBitmaps[i].getHeight()); } //draw the color array directly,w/o(without) creating 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, true, mPaint); } private Bitmap codec(Bitmap bitmap, CompressFormat format, int quality) { ByteArrayOutputStream baos=new ByteArrayOutputStream(); bitmap.compress(format, quality, baos); byte[] data=baos.toByteArray(); return BitmapFactory.decodeByteArray(data, 0, data.length); } private int[] initColors() { int[] colors=new int[STRIDE*HEIGHT]; for (int y = 0; y < HEIGHT; y++) {//use of x,y is legible then the use of i,j 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);//the shift operation generates the color ARGB } } return colors; } } }

Bitmap中setPiexls的解释:

public voidsetPixels(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 aColor

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.
生成颜色值数组的函数:
private int[] initColors() { int[] colors=new int[STRIDE*HEIGHT]; for (int y = 0; y < HEIGHT; y++) {//use of x,y is legible then the use of i,j 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);//the shift operation generates the color ARGB } } return colors; }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/48428
推荐阅读
相关标签
  

闽ICP备14008679号