I am adding 1 layout dynamically. In that adding custom view. now I want to draw image on canvas exactly of same size as the size of that layout.
Below is the code of my custom view
which i have tried.
public BlockView(Context context, Bitmap bitmap, int layoutHeight,
int layoutWidth) {
super(context);
this.bitmap = bitmap;
this.width = bitmap.getWidth();
this.height = bitmap.getHeight();
this.layoutHeight = layoutHeight;
this.layoutWidth = layoutWidth;
if (drawableWidth <= viewWidth && drawableHeight <= viewHeight) {
scale = 1.0f;
} else {
scale = Math.min((float) viewWidth / (float) drawableWidth,
(float) viewHeight / (float) drawableHeight);
}
mDrawable = new BitmapDrawable(getResources(), bitmap);
}
private static float getDegreesFromRadians(float angle) {
return (float) (angle * 180.0 / Math.PI);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
drawableWidth = mDrawable.getIntrinsicWidth();
drawableHeight = mDrawable.getIntrinsicHeight();
viewWidth = layoutWidth;
viewHeight = layoutHeight;
setMeasuredDimension(viewWidth, viewHeight);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int saveCount = canvas.save();
matrix.setScale(scale, scale);
matrix.postRotate(getDegreesFromRadians(angle));
matrix.postTranslate(position.getX(), position.getY());
canvas.concat(matrix);
mDrawable.setBounds(0, 0, drawableWidth, drawableHeight);
mDrawable.draw(canvas);
canvas.restoreToCount(saveCount);
}
With above code,I am successfully able to draw image and perform drag,zoom and rotate. But facing problem of not able to draw image of same size as the size of layout. and also need some way to proceed on how to check that image should not be scaled too small than the layout size.
Any help will be really appreciated.