80,349
社区成员
发帖
与我相关
我的任务
分享
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;
}
}
是不是这个,但是测试时没有显示public class MyView extends View {
...
}
是不是这个,但是测试时没有显示[/quote]
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]
真的可以了,真是太感谢了!如果加入对原图进行拖动,背景静止,怎么实现 ?public class MyView extends View {
...
}
是不是这个,但是测试时没有显示[/quote]
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;
}
Bitmap bitmap = Bitmap.createBitmap(800, 480, Config.ARGB_8888);//创建一个800*480的背景!
Canvas canvas = new Canvas(bitmap);//创建一个Canvas
canvas.drawBitmap(bitmap , matrix, paint); //好像没有一个方法能将原图绘制上去
/**
* 图片缩放
* @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;
}