请教android图片处理技巧
打个比方:这个图片的规格是:480*800的
然后处理成规格为:800*480
有没有可行的方案,只要生成800*480格式,原始图片不失真就行。跪求高手! 图片处理 android 图片
[解决办法]
创建一个800×480的 Bitmap
Bitmap bitmap = Bitmap.createBitmap ...
创建一个Canvas
Canvas canvas = new Canvas(bitmap)
计算一下中间的位置,把原图画上去
canvas.drawBitmap ...
如果需要,把bitmap保存成JPG/PNG,质量选高点
bitmap.compress ...
[解决办法]
2楼正解,layout可以用ImageView,属性设置成fitY,或者center
[解决办法]
归根到底,是对图片做了一次缩放算法!
[解决办法]
试试这个方法
/**
* 图片缩放
* @param bigimage
* @param newWidth
* @param newHeight
* @return
*/
public Bitmap tochange(Bitmap bigimage,int newWidth,int newHeight){
// 获取这个图片的宽和高
int width = bigimage.getWidth();
int height = bigimage.getHeight();
// 创建操作图片用的matrix对象
Matrix matrix = new Matrix();
// 计算缩放率,新尺寸除原始尺寸
float scaleWidth = ((float) newWidth)/width;
float scaleHeight = ((float) newHeight)/height;
// 缩放图片动作
matrix.postScale(scaleWidth, scaleHeight);
Bitmap bitmap = Bitmap.createBitmap(bigimage, 0, 0, width, height,matrix, true);
return bitmap;
}
public class MyView extends View {
private Paint paint;
private Canvas canvas;
public MyView(Context context) {
super(context);
paint = new Paint();
paint.setAntiAlias(true);
this.setKeepScreenOn(true);
paint.setColor(Color.RED);
}
public void OnDraw(Canvas canvas) {
Bitmap bg = Bitmap.createBitmap(
BitmapFactory.decodeResource(getResources(), R.drawable.abc),
0, 0, 800, 480);//800*480的背景图片
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.canvas_top);//目标图片
canvas = new Canvas(bg);
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
int width = bitmap.getWidth();
int height = bitmap.getHeight();
tochange(bitmap, width / 2, height / 2);//对目标图片进行压缩
canvas.drawBitmap(bitmap, 0, 0, paint);
}
public Bitmap tochange(Bitmap bitmap, int newWidth, int newHeight) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
matrix.postScale(scaleWidth, scaleHeight);
Bitmap bm = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix,
true);
return bm;
}
}
[解决办法]
创建一个800×480的 Bitmap
Bitmap bitmap = Bitmap.createBitmap ...Bitmap bitmap = Bitmap.createBitmap(800, 480, Config.ARGB_8888);//创建一个800*480的背景!
Canvas canvas = new Canvas(bitmap);//创建一个Canvas
canvas.drawBitmap(bitmap , matrix, paint); //好像没有一个方法能将原图绘制上去
private Bitmap scale(Bitmap origin) {
Bitmap bitmap = Bitmap.createBitmap(800, 480, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Rect target = null;
int orgWidth = origin.getWidth(), orgHeight = origin.getHeight();
int bgHeight = bitmap.getHeight(), bgWidth = bitmap.getWidth();
if (orgWidth * bgHeight > orgHeight * bgWidth) {
int newWidth = bgWidth, newHeight = newWidth * orgHeight / orgWidth;
target = new Rect(0, (bgHeight - newHeight) / 2, newWidth, (bgHeight + newHeight) / 2);
} else {
int newHeight = bgHeight, newWidth = newHeight * orgWidth / orgHeight;
target = new Rect((bgWidth - newWidth) / 2, 0, (bgWidth + newWidth) / 2, newHeight);
}
canvas.drawBitmap(origin, null, target, new Paint(Paint.DITHER_FLAG
[解决办法]
Paint.FILTER_BITMAP_FLAG));
return bitmap;
}