drawable와bitmap

1、Drawable , (BitmapDrawable), (ShapeDrawable), (LayerDrawable), , 
2、Canvas , , 
3、Bitmap , 
4、Matrix 

2. 비트맵


1. 자원에서 Bitmap 가져오기

		Resources res = getResources();
		Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.icon);

2、Bitmap → byte[]

	public byte[] Bitmap2Bytes(Bitmap bm) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
		return baos.toByteArray();
	}

 3、byte[] → Bitmap

	public Bitmap Bytes2Bimap(byte[] b) {
		if (b.length != 0) {
			return BitmapFactory.decodeByteArray(b, 0, b.length);
		} else {
			return null;
		}
	}

4. 비트맵 배율 조정


  public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) { int w = bitmap.getWidth(); int h = bitmap.getHeight(); Matrix matrix = new Matrix(); float scaleWidth = ((float) width / w); float scaleHeight = ((float) height / h); matrix.postScale(scaleWidth, scaleHeight); Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true); return newbmp; }


5. Drawable을 Bitmap으로 전환

public static Bitmap drawableToBitmap(Drawable drawable) {
		//   drawable  
		int w = drawable.getIntrinsicWidth();
		int h = drawable.getIntrinsicHeight();

		//   drawable  
		Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
				: Bitmap.Config.RGB_565;
		//   bitmap
		Bitmap bitmap = Bitmap.createBitmap(w, h, config);
		//   bitmap  
		Canvas canvas = new Canvas(bitmap);
		drawable.setBounds(0, 0, w, h);
		//   drawable  
		drawable.draw(canvas);
		return bitmap;
	}

6、원각 이미지 획득


public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) { int w = bitmap.getWidth(); int h = bitmap.getHeight(); Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, w, h); final RectF rectF = new RectF(rect); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; }  


7、그림자가 비친 그림 획득

	public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
		final int reflectionGap = 4;
		int w = bitmap.getWidth();
		int h = bitmap.getHeight();

		Matrix matrix = new Matrix();
		matrix.preScale(1, -1);

		Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, h / 2, w,
				h / 2, matrix, false);

		Bitmap bitmapWithReflection = Bitmap.createBitmap(w, (h + h / 2),
				Config.ARGB_8888);

		Canvas canvas = new Canvas(bitmapWithReflection);
		canvas.drawBitmap(bitmap, 0, 0, null);
		Paint deafalutPaint = new Paint();
		canvas.drawRect(0, h, w, h + reflectionGap, deafalutPaint);

		canvas.drawBitmap(reflectionImage, 0, h + reflectionGap, null);

		Paint paint = new Paint();
		LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
				bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff,
				0x00ffffff, TileMode.CLAMP);
		paint.setShader(shader);
		// Set the Transfer mode to be porter duff and destination in
		paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
		// Draw a rectangle using the paint with our linear gradient
		canvas.drawRect(0, h, w, bitmapWithReflection.getHeight()
				+ reflectionGap, paint);

		return bitmapWithReflection;
	}

3. Drawable


1. Bitmap을 Drawable로 변환

Bitmap bm=xxx; //xxx 
BitmapDrawable bd= new BitmapDrawable(getResource(), bm); 
 BtimapDrawable Drawable , bd 。

2. Drawable 배율 조정

	public static Drawable zoomDrawable(Drawable drawable, int w, int h) {
		int width = drawable.getIntrinsicWidth();
		int height = drawable.getIntrinsicHeight();
		// drawable bitmap
		Bitmap oldbmp = drawableToBitmap(drawable);
		//  Matrix 
		Matrix matrix = new Matrix();
		//  
		float sx = ((float) w / width);
		float sy = ((float) h / height);
		//  
		matrix.postScale(sx, sy);
		//  bitmap, bitmap 
		Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height,
				matrix, true);
		return new BitmapDrawable(newbmp);
	}

3. 그림의 질을 희생하는 대가로 그림을 축소한다.

BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(imageFile, opts);

inJustDecodeBounds를true로 설정하면 decodeFile는 공간을 분배하지 않지만 원시 그림의 길이와 너비, 즉opts를 계산할 수 있습니다.width와opts.height.이 두 개의 매개 변수가 있으면 일정한 알고리즘을 통과하면 적절한 inSampleSize를 얻을 수 있다.
Android는 동적 계산 방법을 제공합니다.다음과 같습니다.
public static int computeSampleSize(BitmapFactory.Options options,
        int minSideLength, int maxNumOfPixels) {
    int initialSize = computeInitialSampleSize(options, minSideLength,
            maxNumOfPixels);
 
    int roundedSize;
    if (initialSize <= 8) {
        roundedSize = 1;
        while (roundedSize < initialSize) {
            roundedSize <<= 1;
        }
    } else {
        roundedSize = (initialSize + 7) / 8 * 8;
    }
 
    return roundedSize;
}
 
private static int computeInitialSampleSize(BitmapFactory.Options options,
        int minSideLength, int maxNumOfPixels) {
    double w = options.outWidth;
    double h = options.outHeight;
 
    int lowerBound = (maxNumOfPixels == -1) ? 1 :
            (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
    int upperBound = (minSideLength == -1) ? 128 :
            (int) Math.min(Math.floor(w / minSideLength),
            Math.floor(h / minSideLength));
 
    if (upperBound < lowerBound) {
        return lowerBound;
    }
 
    if ((maxNumOfPixels == -1) &&
            (minSideLength == -1)) {
        return 1;
    } else if (minSideLength == -1) {
        return lowerBound;
    } else {
        return upperBound;
    }
}   

이 함수만 사용하면 됩니다.
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imageFile, opts);
             
opts.inSampleSize = computeSampleSize(opts, -1, 128*128);
// false, true      
opts.inJustDecodeBounds = false;
try {
    Bitmap bmp = BitmapFactory.decodeFile(imageFile, opts);
    imageView.setImageBitmap(bmp);
    } catch (OutOfMemoryError err) {
    }

4. 임시 공간 만들기


android 장치에 비교적 큰 그림 자원을 불러올 때, 불러온 자원을 임시 공간에 불러올 수 있는 임시 공간을 만들 수 있습니다.
BitmapFactory.Options bfOptions=new BitmapFactory.Options();
             bfOptions.inDither=false;                    
             bfOptions.inPurgeable=true;              
             bfOptions.inTempStorage=new byte[12 * 1024]; 
            // bfOptions.inJustDecodeBounds = true;
             File file = new File(pePicFile.getAbsolutePath() + "/"+info.getImage());
             FileInputStream fs=null;
             try {
                fs = new FileInputStream(file);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
             Bitmap bmp = null;
             if(fs != null)
                try {
                    bmp = BitmapFactory.decodeFileDescriptor(fs.getFD(), null, bfOptions);
                } catch (IOException e) {
                    e.printStackTrace();
                }finally{ 
                    if(fs!=null) {
                        try {
                            fs.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }

좋은 웹페이지 즐겨찾기