I have 2 ImageViews, 1 for Canvas and other for a ImageBackground.
The ImageViews have the size of the screen.
When I touch a Screen I need to redraw the Canvas like the example:
private void reDraw() {
Display display = getWindowManager().getDefaultDisplay();
bmOverlay = Bitmap.createBitmap(display.getWidth(), display.getHeight(), oBitmap.getConfig());
oCanvas = new Canvas(bmOverlay);
//I need to erase this line
oCanvas.drawBitmap(bmpBackGround, new Matrix(), null);
//this code paint some bitmaps on the canvas
for (ColorBall ball : colorballs) {
oCanvas.drawBitmap(ball.getBitmap(), ball.getX(), ball.getY(), null);
}
//set the canvas on the image view
getImageView2().setImageBitmap(bmOverlay);
}
But, on this example I redraw the Bitmap, when I touch the screen. I need to redraw a transparent Canvas because I don´t need to redraw the Bitmap on the background, because the bitmap never change. But if I don´t redraw the bitmap, the background stay black and the bitmap stay on the back. Any help?
Check to make sure the overlay bitmap you've created is actually transparent.
Create it with config ARGB_8888 explicitly.
http://developer.android.com/reference/android/graphics/Bitmap.Config.html
And you might want to move the creation of the bitmap out of the reDraw method and only create it once instead.
Related
I have one image image 1 and one is coming from server that is image 2 i am trying to draw second one just at the center of the first. as result i want single image like in pic .
This should do what you're looking for:
The backgroundBitmap variable would be your image1 and the bitmapToDrawInTheCenter would be your image2.
public void centerImageInOtherImage()
{
Bitmap backgroundBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
Bitmap bitmapToDrawInTheCenter = BitmapFactory.decodeResource(getResources(), R.drawable.ic_action_search);
Bitmap resultingBitmap = Bitmap.createBitmap(backgroundBitmap.getWidth(), backgroundBitmap.getHeight(), backgroundBitmap.getConfig());
Canvas canvas = new Canvas(resultingBitmap);
canvas.drawBitmap(backgroundBitmap, new Matrix(), null);
canvas.drawBitmap(bitmapToDrawInTheCenter, (backgroundBitmap.getWidth() - bitmapToDrawInTheCenter.getWidth()) / 2, (backgroundBitmap.getHeight() - bitmapToDrawInTheCenter.getHeight()) / 2, new Paint());
ImageView image = (ImageView)findViewById(R.id.myImage);
image.setImageBitmap(resultingBitmap);
}
Courtesy : Draw text/Image on another Image in Android
Drawing images over each other is fairly simple with Canvas. Canvas basically acts as the drawing board to draw text/Image. You just need to construct a canvas with the first Image and then draw the second Image at the center as shown below
/* This ImageOne will be used as the canvas to draw an another image over it. Hence we make it mutable using the copy API
as shown below
*/
Bitmap imageOne = BitmapFactory.decodeResource(getResources(), R.drawable.imageOne).copy(Bitmap.Config.ARGB_8888,true);
// Decoding the image two resource into a Bitmap
Bitmap imageTwo= BitmapFactory.decodeResource(getResources(), R.drawable.imageTwo);
// Here we construct the canvas with the specified bitmap to draw onto
Canvas canvas=new Canvas(imageOne);
/*Here we draw the image two on the canvas using the drawBitmap API.
drawBitmap takes in four parameters
1 . The Bitmap to draw
2. X co-ordinate to draw from
3. Y co ordinate to draw from
4. Paint object to define style
*/
canvas.drawBitmap(imageTwo,(imageOne.getWidth())/2,(imageOne.getHeight())/2,new Paint());
imageView.setImageBitmap(imageOne);
Hi every am new to this android development.
Currently am developing drawing application with adding stamps/labels to drawn image.so i have done drawing part so now i have to implement adding stamps/labels to that drawn image.
So please help me out this..
Bitmap Rbitmap = Bitmap.createBitmap(bitmap).copy(Config.ARGB_4444, true);
Canvas canvas = new Canvas(Rbitmap);
canvas.drawBitmap(label, -9, Rbitmap.getHeight()-label.getHeight()-10, null);
canvas.save();
return Rbitmap;
Making your question little more specific will help you more.If I understood is correct this piece of code will help you out to draw a bitmap to a drawn canvas.
private Paint green = new Paint();
private int greenx , greeny;
green.setColor(Color.GREEN);
green.setAntiAlias(false);
canvas.drawCircle(greenx,greeny,20,green);
how to add image in this code replace drawcircle with image how ?
You could be a little more specific, i.e posting some code to show what you have to get more specific answers. Anyway, you can draw a bitmap on top of another bitmap by using something like this:
//You will have a Bitmap bottomBmp, a Bitmap topBmp and a Canvas canvas.
//If you are inside an onDraw() method the canvas will be provided to you, otherwise you will have to create it yourself, use a mutable bitmap of the same size as the bottomBmp.
canvas.drawBitmap(bottomBmp, 0, 0, null); //Draw the bottom bitmap in the upper left corner of the canvas without any special paint effects.
canvas.drawBitmap(topBmp, 0, 0, null); //Draw the top bitmap over the bottom bitmap, change the zeroes to offset this bitmap.
Try with this code:
private Bitmap background;
public birdClass(Context context) {
super(context);
background = BitmapFactory.decodeResource(getResources(),R.drawable.splash );
}
I want to set a background of a View with a tiled bitmap, but the tiling needs to be anchored to the bottom-left, instead of the top-left corner (the default). For example, if the tiles are the smiley faces below, I want it to be tiled like:
Using xml drawables I could achieve either tiling (using tileMode="repeat") or bottom positioning (using gravity="bottom"), but combining both is not possible, even the documentation says so:
android:tileMode
Keyword. Defines the tile mode. When the tile mode is
enabled, the bitmap is repeated. Gravity is ignored when the tile mode
is enabled.
Although it's not internally supported, is there any way to achieve this, perhaps using custom views?
Another way would be to extend BitmapDrawable and override the paint() method:
In this method we avoid creating a new bitmap having the size of the view.
class MyBitmapDrawable extends BitmapDrawable {
private Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
private boolean mRebuildShader = true;
private Matrix mMatrix = new Matrix();
#Override
public void draw(Canvas canvas) {
Bitmap bitmap = getBitmap();
if (bitmap == null) {
return;
}
if (mRebuildShader) {
mPaint.setShader(new BitmapShader(bitmap, TileMode.REPEAT, TileMode.REPEAT));
mRebuildShader = false;
}
// Translate down by the remainder
mMatrix.setTranslate(0, getBounds().bottom % getIntrinsicHeight());
canvas.save();
canvas.setMatrix(mMatrix);
canvas.drawRect(getBounds(), mPaint);
canvas.restore();
}
}
It can be set to the view like this:
view.setBackgroundDrawable(new MyBitmapDrawable(getResources().getDrawable(R.drawable.smiley).getBitmap()));
Just a thought, and it's pretty roundabout, but could you flip your image vertically, and then apply a transform to your background to flip that vertically as well?
Using a custom view might involve handling all the drawing yourself, not just the background image.
Instead, I propose to set the view's background programmatically as shown:
// This drawable refers to an image directly and NOT an XML
BitmapDrawable smiley = (BitmapDrawable) getResources().getDrawable(R.drawable.smiley);
// Create a new bitmap with the size of the view
Bitmap bgBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bgBitmap);
// Translate down by the remainder
Matrix matrix = new Matrix();
matrix.setTranslate(0, view.getHeight() % smiley.getIntrinsicHeight());
canvas.setMatrix(matrix);
// Tile the smileys
Paint paint = new Paint();
paint.setShader(new BitmapShader(smiley.getBitmap(), TileMode.REPEAT, TileMode.REPEAT));
canvas.drawPaint(paint);
view.setBackgroundDrawable(new BitmapDrawable(bgBitmap));
Points to consider:
I'm not sure if view.getWidth() & view.getHeight() are the correct
methods to get the dimensions.
What if smiley size is bigger than the view?
I have made a custom ImageView which has an onDraw method that draws a circle on particular pixels. The problem is I am using the ImageView to load a bitmap image and I need the canvas to be the same size as the bitmap image and the ImageView holder to be the same size again..
does anyone know how to achieve this?
final SurfaceHolder holder = getSurfaceHolder();
Canvas c = new Canvas();
c = holder.lockCanvas();
c.save();
c.drawBitmap(yourBitmap,0,0,null);
c.restore();
holder.unlockCanvasAndPost(c);
It is my understanding that you don't need to worry about the canvas size.
Wouldn't determining the Bitmap size in your onMeasure() and setting the custom view size accordingly work?
I'm drawing a bitmap in a canvas and I want to have the result in a new bitmap, but I still have a black screen as result.
This is my code, part of the onDraw(Canvas canvas) method:
if (bitmapTemplate == null) {
canvasBis = new Canvas();
bitmapTemplate = Bitmap.createBitmap(canvas.getWidth()+30,canvas.getHeight(),Bitmap.Config.ARGB_8888);
drawZones(canvasBis,bitmapTemplate);
}
bitmapRes = Bitmap.createBitmap(canvas.getWidth()+30,canvas.getHeight(),Bitmap.Config.ARGB_8888);
canvas.setBitmap(bitmapRes);
canvas.drawBitmap(bitmapTemplate, matrix, null);
My goal is to have a new bitmap (bitmapRes) by applying a matrix on an existing bitmap (bitmapTemplate). With this code I always have a black screen, but when I remove the line canvas.setBitmap(bitmapRes), I have a result but not in a new bitmap. Any ideas please? Maybe transparency? Thanks in advance.
drawZones draws some zones in bitmapTemplate.
You should try using the Canvas(Bitmap) constructor instead of the empty constructor. This sets the bitmap to the canvas for you.