将任意图片转换成指定大小的图片
2012-04-20 12:55
357 查看
public class ImageConvertShowActivity extends Activity { private ImageView iv_examples, iv_method01, iv_method02; private static final int WIDTH = 200; private static final int HEIGHT = 150; private static final float SHADOW_RADIUS = 12.0f; private static final Paint SHADOW_PAINT = new Paint(); private static final Paint SCALE_PAINT = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.image_size_show); iv_examples = (ImageView) findViewById(R.id.iv_examples); iv_method01 = (ImageView) findViewById(R.id.iv_method01); iv_method02 = (ImageView) findViewById(R.id.iv_method02); iv_examples.setImageResource(R.drawable.img_01); Resources resources = getResources(); iv_method01.setImageBitmap(getBitmap1(BitmapFactory.decodeResource(resources, R.drawable.img_01), WIDTH, HEIGHT)); } public Bitmap getBitmap1(Bitmap bitmap, int width, int height) { final int bitmapWidth = bitmap.getWidth(); final int bitmapHeight = bitmap.getHeight(); final float scale = Math.min((float) width / (float) bitmapWidth, (float) height / (float) bitmapHeight); final int scaledWidth = (int) (bitmapWidth * scale); final int scaledHeight = (int) (bitmapHeight * scale); return createScaledBitmap(bitmap, scaledWidth, scaledHeight, SHADOW_RADIUS, false, SHADOW_PAINT); } private static Bitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, float offset, boolean clipShadow, Paint paint) { Matrix m= new Matrix(); final int width = src.getWidth(); final int height = src.getHeight(); final float sx = dstWidth / (float) width; final float sy = dstHeight / (float) height; m.setScale(sx, sy); Bitmap b = createBitmap(src, 0, 0, width, height, m, offset, clipShadow, paint); return b; } private static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, float offset, boolean clipShadow, Paint paint) { int scaledWidth = width; int scaledHeight = height; final Canvas canvas = new Canvas(); canvas.translate(offset / 2.0f, offset / 2.0f); Bitmap bitmap; final Rect from = new Rect(x, y, x + width, y + height); final RectF to = new RectF(0, 0, width, height); if (m == null || m.isIdentity()) { bitmap = Bitmap.createBitmap(scaledWidth + (int) offset, scaledHeight + (int) (clipShadow ? (offset / 2.0f) : offset), Bitmap.Config.ARGB_8888); paint = null; } else { RectF mapped = new RectF(); m.mapRect(mapped, to); scaledWidth = Math.round(mapped.width()); scaledHeight = Math.round(mapped.height()); bitmap = Bitmap.createBitmap(scaledWidth + (int) offset, scaledHeight + (int) (clipShadow ? (offset / 2.0f) : offset), Bitmap.Config.ARGB_8888); canvas.translate(-mapped.left, -mapped.top); canvas.concat(m); } canvas.setBitmap(bitmap); canvas.drawRect(0.0f, 0.0f, width, height, paint); canvas.drawBitmap(source, from, to, SCALE_PAINT); return bitmap; } }
相关文章推荐
- Qt学习笔记,修改指定图片的大小,转换为QIcon用于QPushButton等控件
- 把图片文件转换成指定大小尺寸格式的文件
- Qt学习笔记,修改指定图片的大小,转换为QIcon用于QPushButton等控件
- android 图片倒影 bitmap与drawable的转换 把图片处理成指定大小 图片圆角处理 dip转换成px
- gdiplus将一种图片格式转换成另外一个指定图片格式,且可设置图片大小
- js 图片转换为指定大小的DataUrl
- 输入一个图片,获得转换成为指定大小的图片
- 图片截取并缩放—以任意点为起点截取任意长度并缩放至指定大小算法
- ASP.NET实现自适应图片大小的弹出窗口(窗口可任意编辑) [转载,佚名]
- bitmap 将图片压缩到指定的大小
- asp.net 图片超过指定大小后等比例压缩图片的方法
- 任意图片格式相互转换(ImageIO)
- 图片生成指定大小的略缩图(只支持JPG)
- 以图片的中心为基准,按照任意尺寸自动调整大小并截取出规定的素材,保存为缩略图或以php形式输出为图片
- 对给定的一个图片生成一个指定大小的缩略图(上传图片很大是可用)
- 【自用】Android 改变Bitmap图片资源为指定大小
- 通过ImageIO创建任意大小图片缩略图(image thumbnail)
- java图片大小处理(缩放&切割&类型转换&色彩转换)
- 任意大小缩放图片
- --==vb6中用图片框任意大小播放AVI电影(New)==--