Android 图像

1. 从资源文件中创建Bitmap

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
		R.drawable.ic_launcher);



2. 从assets中创建Bitmap
Bitmap bitmap = BitmapFactory.decodeStream(getAssets().open(
		"Penguins.jpg"));


3. 获取图片的缩略图
	/**
	 * 获取图片的缩略图
	 * 
	 * @param id
	 *            资源ID
	 * @param width
	 *            图片显示宽度
	 * @param height
	 *            图片显示高度
	 */
	public Bitmap getThumb(int id, int width, int height) {
		BitmapFactory.Options opts = new BitmapFactory.Options();
		opts.inJustDecodeBounds = true; // 不在内存中创建该图像
		BitmapFactory.decodeResource(getResources(), id, opts);

		int imageWidth = opts.outWidth;
		int imageHeight = opts.outHeight;

		// 获取压缩比率
		int widthRatio = (int) Math.ceil(imageWidth / width);
		int heightRatio = (int) Math.ceil(imageHeight / height);

		int ratio = 1;
		if (widthRatio > 1 && heightRatio > 1) {
			if (widthRatio > heightRatio)
				ratio = widthRatio;
			else
				ratio = heightRatio;
		}

		opts.inSampleSize = ratio; // 设置压缩比率
		opts.inJustDecodeBounds = false; // 在内存中创建该图像
		return BitmapFactory.decodeResource(getResources(), R.drawable.koala,
				opts);
	}


4. 从View中获取Bitmap
	public Bitmap getBitmapFromView(View view) {
		// 创建一个与View同样大小的Bitmap
		Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),
				Bitmap.Config.ARGB_8888);
		// 创建Canvas对象,并将其与Bitmap对象绑定
		Canvas canvas = new Canvas(bitmap);
		// 绘制View的背景
		Drawable bgDrawable = view.getBackground();
		if (bgDrawable != null)
			bgDrawable.draw(canvas);
		else
			// 如果View没有背景,将Bitmap的背景设为白色
			canvas.drawColor(Color.WHITE);
		// 将View绘制在Bitmap上
		view.draw(canvas);
		return bitmap;
	}


	public Bitmap viewToBitmap(View view) {
		view.setDrawingCacheEnabled(true);
		view.buildDrawingCache();
		Bitmap bm = view.getDrawingCache();
		return bm;
	}

注:从View中获取Bitmap时,必须保证View可见


5. Bitmap保存

	public boolean saveBitmapToFile(Bitmap bmp, CompressFormat format,
			int quality, String path) {
		OutputStream stream = null;
		try {
			stream = new FileOutputStream(path);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return bmp.compress(format, quality, stream);
	}


6. 创建自定义Bitmap

	public void customImage() {
		mBitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas();
		canvas.setBitmap(mBitmap);
		canvas.drawColor(getResources().getColor(
				android.R.color.holo_green_light));
		Paint paint = new Paint();
		paint.setColor(Color.BLACK);
		paint.setTypeface(Typeface.createFromAsset(getAssets(),
				"ChopinScript.ttf"));
		paint.setTextSize(40);
		canvas.drawText("Hello", 55, 110, paint);

		mBitmap2 = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
		Canvas canvas2 = new Canvas();
		canvas2.setBitmap(mBitmap2);
		canvas2.drawColor(getResources().getColor(
				android.R.color.holo_green_light));
		Paint paint2 = new Paint();
		paint2.setColor(Color.BLACK);
		paint2.setTypeface(Typeface.createFromAsset(getAssets(), "fzxkt.ttf"));
		paint2.setTextSize(50);
		canvas2.drawText("孙芦波", 30, 110, paint2);
	}


Android 图像

你可能感兴趣的:(Android 图像)