请教android图片处理技巧

骨灰级菜虫 2013-10-28 04:42:05
打个比方:这个图片的规格是:480*800的

然后处理成规格为:800*480

有没有可行的方案,只要生成800*480格式,原始图片不失真就行。跪求高手!
...全文
328 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
骨灰级菜虫 2013-11-01
  • 打赏
  • 举报
回复

MR__P 2013-10-31
  • 打赏
  • 举报
回复
感谢也不给分,谁还愿意管你啊!
xiaobeiweng 2013-10-30
  • 打赏
  • 举报
回复
引用 2 楼 youngc527 的回复:
创建一个800×480的 Bitmap Bitmap bitmap = Bitmap.createBitmap ... 创建一个Canvas Canvas canvas = new Canvas(bitmap) 计算一下中间的位置,把原图画上去 canvas.drawBitmap ... 如果需要,把bitmap保存成JPG/PNG,质量选高点 bitmap.compress ...
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;

	}
}
是不是这个,但是测试时没有显示
骨灰级菜虫 2013-10-30
  • 打赏
  • 举报
回复
引用 13 楼 youngc527 的回复:
[quote=引用 12 楼 xiaobeiweng 的回复:] [quote=引用 2 楼 youngc527 的回复:] 创建一个800×480的 Bitmap Bitmap bitmap = Bitmap.createBitmap ...
public class MyView extends View {
...
}
是不是这个,但是测试时没有显示[/quote]
引用 10 楼 u010749756 的回复:
[quote=引用 2 楼 youngc527 的回复:] 创建一个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);  //好像没有一个方法能将原图绘制上去
[/quote]


	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;
	}
[/quote] 真的可以了,真是太感谢了!如果加入对原图进行拖动,背景静止,怎么实现 ?
Darcy杨 2013-10-30
  • 打赏
  • 举报
回复
引用 12 楼 xiaobeiweng 的回复:
[quote=引用 2 楼 youngc527 的回复:] 创建一个800×480的 Bitmap Bitmap bitmap = Bitmap.createBitmap ...
public class MyView extends View {
...
}
是不是这个,但是测试时没有显示[/quote]
引用 10 楼 u010749756 的回复:
[quote=引用 2 楼 youngc527 的回复:] 创建一个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);  //好像没有一个方法能将原图绘制上去
[/quote]


	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;
	}
骨灰级菜虫 2013-10-29
  • 打赏
  • 举报
回复
引用 2 楼 youngc527 的回复:
创建一个800×480的 Bitmap Bitmap bitmap = Bitmap.createBitmap ... 创建一个Canvas Canvas canvas = new Canvas(bitmap) 计算一下中间的位置,把原图画上去 canvas.drawBitmap ... 如果需要,把bitmap保存成JPG/PNG,质量选高点 bitmap.compress ...
大哥,可否再详细点
骨灰级菜虫 2013-10-29
  • 打赏
  • 举报
回复
引用 4 楼 xidianstudent 的回复:
归根到底,是对图片做了一次缩放算法!
求详细算法代码
底层码农 2013-10-29
  • 打赏
  • 举报
回复
归根到底,是对图片做了一次缩放算法!
骨灰级菜虫 2013-10-29
  • 打赏
  • 举报
回复
引用 2 楼 youngc527 的回复:
创建一个800×480的 Bitmap
Bitmap bitmap = Bitmap.createBitmap ...
创建一个Canvas
Canvas canvas = new Canvas(bitmap)
计算一下中间的位置,把原图画上去
canvas.drawBitmap ...
如果需要,把bitmap保存成JPG/PNG,质量选高点
bitmap.compress ...

drawBitmap 方法中把原图绘制上去,没发现这个类似的参数
骨灰级菜虫 2013-10-29
  • 打赏
  • 举报
回复
引用 2 楼 youngc527 的回复:
创建一个800×480的 Bitmap Bitmap bitmap = Bitmap.createBitmap ... 创建一个Canvas Canvas canvas = new Canvas(bitmap) 计算一下中间的位置,把原图画上去 canvas.drawBitmap ... 如果需要,把bitmap保存成JPG/PNG,质量选高点 bitmap.compress ...
Bitmap bitmap = Bitmap.createBitmap(800, 480, Config.ARGB_8888);//创建一个800*480的背景!
Canvas canvas = new Canvas(bitmap);//创建一个Canvas
canvas.drawBitmap(bitmap , matrix, paint);  //好像没有一个方法能将原图绘制上去
骨灰级菜虫 2013-10-29
  • 打赏
  • 举报
回复
引用 8 楼 BuleRiver 的回复:
创建一个空的Bitmap,然后使用该bitmap创建画布,把原来的图片缩放后,绘制在这个画布上。
分享一下代码
BuleRiver 2013-10-29
  • 打赏
  • 举报
回复
创建一个空的Bitmap,然后使用该bitmap创建画布,把原来的图片缩放后,绘制在这个画布上。
happyLife002 2013-10-29
  • 打赏
  • 举报
回复
试试这个方法
/** 
     * 图片缩放 
     * @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;  
    }  
  • 打赏
  • 举报
回复
2楼正解,layout可以用ImageView,属性设置成fitY,或者center
Darcy杨 2013-10-28
  • 打赏
  • 举报
回复
创建一个800×480的 Bitmap Bitmap bitmap = Bitmap.createBitmap ... 创建一个Canvas Canvas canvas = new Canvas(bitmap) 计算一下中间的位置,把原图画上去 canvas.drawBitmap ... 如果需要,把bitmap保存成JPG/PNG,质量选高点 bitmap.compress ...
骨灰级菜虫 2013-10-28
  • 打赏
  • 举报
回复
背景可以自定义

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧