Reputation: 355
I have a Bitmap object in my main class. I need to send this bitmap to my custom view class to set it as a background for further processing on canvas.
For example, there is a method called setPicture that receives bitmap as a parameter. So, how it is possible to draw this bitmap on the canvas?
Please see the code below:
public class TouchView extends View {
final int MIN_WIDTH = 75;
final int MIN_HEIGHT = 75;
final int DEFAULT_COLOR = Color.RED;
int _color;
final int STROKE_WIDTH = 2;
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private float x, y;
private boolean touching = false;
public TouchView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
init();
}
public TouchView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
init();
}
public TouchView(Context context) {
super(context);
// TODO Auto-generated constructor stub
init();
}
private void init() {
setMinimumWidth(MIN_WIDTH);
setMinimumHeight(MIN_HEIGHT);
_color = DEFAULT_COLOR;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
MeasureSpec.getSize(heightMeasureSpec));
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
if (touching) {
paint.setStrokeWidth(STROKE_WIDTH);
paint.setColor(_color);
paint.setStyle(Paint.Style.FILL);
canvas.drawCircle(x, y, 75f, paint);
}
}
public void setPicture (Bitmap bitmap) {
///////
This method must receive my bitmap to draw it on canvas!!!!!!!!!!!!!!!
///////
}
public void setColor(int color) {
_color = color;
}
@Override
public boolean onTouchEvent(MotionEvent motionEvent) {
// TODO Auto-generated method stub
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_DOWN:
x = motionEvent.getX();
y = motionEvent.getY();
touching = true;
break;
default:
touching = false;
}
invalidate();
return true;
}
}
How should I send this bitmap to onDraw?
Upvotes: 7
Views: 11237
Reputation: 29
decodedString = Base64.decode(datadtlimgsItem.getImageStr(), Base64.DEFAULT);
decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
decodedByte = Util.resize(decodedByte, 1500, 1500);
mDrawingView.addBitmap(decodedByte);
public void addBitmap(Bitmap bitmap){
canvasBitmap = bitmap;
invalidate();
}
// set bitmap on draw canvas in your custom view class
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (canvasBitmap == null){
canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
}
drawCanvas = new Canvas(canvasBitmap);
}
Upvotes: 0
Reputation: 13855
Inside your onDraw()
method,
just do
canvas.drawBitmap(myBitmap, 0, 0, null);
myBitmap is your bitmap variable.
0,0 refers to the coordinates to draw at, aka the top left corner.
There are other Apis available, to draw to certain areas etc.
More info can be found here in the api docs.
Alternatively:
extend ImageView instead, and use setImageBitmap(Bitmap src);
method to achieve this.
Upvotes: 6
Reputation: 10518
Convert bitmap to drawable and use setBackgroundDrawable method of View class.
public void setPicture (Bitmap bitmap) {
setBackgroundDrawable(new BitmapDrawable(bitmap));
}
Upvotes: 0