在 SomeView Class onDrawBitmap 方法中使位图适合屏幕
Fitting Bitmap to Screen In SomeView Class onDrawBitmap method
首先,它不仅仅是将位图缩放到所有屏幕。它不是重复的。我搜索了它。
我有一个名为 SomeView 的 class,我在 MainActivity[ 上调用此 SomeView class喜欢;
setContentView(new SomeView(MainActivity.this, bitmap ));
从 MainActivity.
发送位图
我正在使用
加载图像
canvas.drawBitmap(bitmap, 0, 0, null);
我也试过了。
RectF dest = new RectF(0,0,getWidth(),getHeight());
canvas.drawBitmap(bitmap, null, dest, null);
也尝试过..
RectF dest = new RectF(0,0,bitmap.getWidth(),bitmap.getHeight());
canvas.drawBitmap(bitmap, null, dest, null);
但对我来说没有任何效果...即将到来的位图不适合屏幕,也不适合居中。
我有什么..
我需要的
我的观点Activity 代码。
SomeView.Java
public SomeView(Context c, Bitmap b) {
super(c);
bitmap = b;
mContext = c;
setFocusable(true);
setFocusableInTouchMode(true);
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.STROKE);
paint.setPathEffect(new DashPathEffect(new float[] { 10, 20 }, 0));
paint.setStrokeWidth(15);
paint.setColor(Color.GREEN);
this.setOnTouchListener(this);
points = new ArrayList<Point>();
bfirstpoint = false;
}
public SomeView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
setFocusable(true);
setFocusableInTouchMode(true);
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(2);
paint.setColor(Color.WHITE);
this.setOnTouchListener(this);
points = new ArrayList<Point>();
bfirstpoint = false;
}
public void onDraw(Canvas canvas) {
canvas.drawBitmap(bitmap, 0, 0, null);
Path path = new Path();
boolean first = true;
for (int i = 0; i < points.size(); i += 2) {
Point point = points.get(i);
if (first) {
first = false;
path.moveTo(point.x, point.y);
} else if (i < points.size() - 1) {
Point next = points.get(i + 1);
path.quadTo(point.x, point.y, next.x, next.y);
} else {
mlastpoint = points.get(i);
path.lineTo(point.x, point.y);
}
}
canvas.drawPath(path, paint);
}
这是解决方案。
RectF src = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
RectF dst = new RectF(0, 0, getWidth(), getHeight());
matrix = new Matrix();
matrix.setRectToRect(src, dst, Matrix.ScaleToFit.CENTER);
Log.d(TAG, "MATRIX VALUE: " + matrix);
canvas.drawBitmap(bitmap, matrix, null);
首先,它不仅仅是将位图缩放到所有屏幕。它不是重复的。我搜索了它。
我有一个名为 SomeView 的 class,我在 MainActivity[ 上调用此 SomeView class喜欢;
setContentView(new SomeView(MainActivity.this, bitmap ));
从 MainActivity.
发送位图我正在使用
加载图像 canvas.drawBitmap(bitmap, 0, 0, null);
我也试过了。
RectF dest = new RectF(0,0,getWidth(),getHeight());
canvas.drawBitmap(bitmap, null, dest, null);
也尝试过..
RectF dest = new RectF(0,0,bitmap.getWidth(),bitmap.getHeight());
canvas.drawBitmap(bitmap, null, dest, null);
但对我来说没有任何效果...即将到来的位图不适合屏幕,也不适合居中。
我有什么..
我需要的
我的观点Activity 代码。 SomeView.Java
public SomeView(Context c, Bitmap b) {
super(c);
bitmap = b;
mContext = c;
setFocusable(true);
setFocusableInTouchMode(true);
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.STROKE);
paint.setPathEffect(new DashPathEffect(new float[] { 10, 20 }, 0));
paint.setStrokeWidth(15);
paint.setColor(Color.GREEN);
this.setOnTouchListener(this);
points = new ArrayList<Point>();
bfirstpoint = false;
}
public SomeView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
setFocusable(true);
setFocusableInTouchMode(true);
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(2);
paint.setColor(Color.WHITE);
this.setOnTouchListener(this);
points = new ArrayList<Point>();
bfirstpoint = false;
}
public void onDraw(Canvas canvas) {
canvas.drawBitmap(bitmap, 0, 0, null);
Path path = new Path();
boolean first = true;
for (int i = 0; i < points.size(); i += 2) {
Point point = points.get(i);
if (first) {
first = false;
path.moveTo(point.x, point.y);
} else if (i < points.size() - 1) {
Point next = points.get(i + 1);
path.quadTo(point.x, point.y, next.x, next.y);
} else {
mlastpoint = points.get(i);
path.lineTo(point.x, point.y);
}
}
canvas.drawPath(path, paint);
}
这是解决方案。
RectF src = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
RectF dst = new RectF(0, 0, getWidth(), getHeight());
matrix = new Matrix();
matrix.setRectToRect(src, dst, Matrix.ScaleToFit.CENTER);
Log.d(TAG, "MATRIX VALUE: " + matrix);
canvas.drawBitmap(bitmap, matrix, null);