When I have a custom Android View (e.g. it extends FrameLayout) and I can draw on Canvas and I can add views with addView. How one relates to the other, e.g. when I draw sth on Canvas and I add some children which is at the top and which is at the bottom?
I am drawing a rectangle in the canvas on the first attempt and again I am attempting to draw one more rectangle at a different position. When I finally add them to LinearLayout I see the only 2nd rectangle, the first rectangle is lost.
Bitmap place = Bitmap.createBitmap(400,800,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(place);
DrawRect dr = new DrawRect();
dr.setLocation(10,10);
dr.draw(canvas);
dr.setLocation(10,80);
dr.draw(canvas);
ll.removeAllViews();
ll.addView(dr);
How do I make both the rectangles to be visible?
removeAllViews() will remove all view of LinearLayout.
You are adding view after removeAllViews() thats why you see 2nd rectangle only.
So remove thisll.removeAllViews(); line and run again.
I found the problem in the code, i should not use the same DrawRect object for drawing one more item, instead use the same but different DrawRect object does the magic.
I have a precut image i.e One base image and multiple transparent layer of the the base image.
I am trying to add the layer image on top of base image to select the layer and apply different color on it.
I have tried the following way to achieve it but not able to finish.
ImageView - I am overlapping imageview with transparent layer image. It shows the blended image but the touch event detects the finally overlapped image id always. Because I draw the image with fill parent, all the layer image is also the same
Layer Drawable - It can allow only drawable images, but my use case is to load the precut from gallery or other resource. Even I can't select layer on touch.
GPUImage library - The image is not showing full imageview.
Regards
Sathiya
Extend the View class, make the onDraw() function draw what you want to draw. The onTouchEvent() function implement the touch events. What you're trying to do isn't subsumed in any view already made but it's pretty easy to make a view. Just draw the bitmaps on the canvas in the correct order with the correct paints with the right transparency.
#Override
protected void onDraw(Canvas canvas) {
int numLayers = mLayers.size();
for (int i = 0; i < numLayers; i++) {
canvas.save();
canvas.concat(matrix);
//or
canvas.translate(rect.left,rect.top);
Bitmap b = mLayers.get(i);
canvas.drawBitmap(b, paint);
canvas.restore();
}}
You end up doing this a bunch just adding bitmaps to the canvas in whatever places and order as needed.
I have prepared a custom view. My view contains one alphabet which i have drawn using drawText in onDraw().Now i need to change this view to bitmap.
Here my OnDraw() is,
public void onDraw(Canvas canvas) {
drawText("A",100,200,mPaint);
}
I need to convert this view to bitmap...please help me...thanks in advance .
For those that already have their views set up and want a shorter solution than AutoCoders:
Bitmap result = Bitmap.createBitmap(dst.getWidth(), dst.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(result);
dst.draw(c);
After this the Bitmap 'result' will contain your view.
'dst' is the View you want as a bitmap.
Bitmap viewCapture = null;
theViewYouWantToCapture.setDrawingCacheEnabled(true);
viewCapture = Bitmap.createBitmap(theViewYouWantToCapture.getDrawingCache());
theViewYouWantToCapture.setDrawingCacheEnabled(false);
In Android, the word “bitmap”, is a class that help us to work with images as a map of bits or an array of bits. This Bitmap class is very useful when we are working with graphics interfaces like Canvas or OpenGL.
Canvas, is another known class in Java, and is used to draw things. In Canvas we have control of all the pixels.
We need to have some variables initialized,
This is the Canvas we are going to draw in.
Canvas canvas = null
This is the layout we are going to use to create our view.
RelativeLayout relativeView ;
This is the background we are going to set in our view, we get it from a resource file (R.drawable.blackBgrnd). The BitmapFactory.decodeResource method is used to get a resource image and convert it in a Bitmap (we will use the Bitmap class). The object mContext (context) must be passed from the Activity we are working on.
Bitmap viewBgrnd = BitmapFactory.decodeResource(mContext.getResources(),R.drawable.blackBgrnd);
We need another bitmap, to draw it on the canvas. We set the width and the height of this bitmap relative to the width and height we have created in our layout. Now this Bitmap is empty, but we are going to associate it with the canvas, so every time we draw in the canvas, it will be drawn in this bitmap object.
Bitmap returnedBitmap = Bitmap.createBitmap(relativeView .width, relativeView.height,Bitmap.Config.ARGB_8888);
First of all, we had the canvas = null, now we create a Canvas object using the auxiliary bitmap we had created before.
canvas = new Canvas(auxBitmap);
Now its time to create our view.
We can add Images, for example:
ImageView newImage = new ImageView(mContext);
newImage.setImageBitmap(bitmapWithImage)
We can set the imageView position in the view using “layout” method:
newImage.layout(l,t,r,b);
l Left position, relative to parent
t Top position, relative to parent
r Right position, relative to parent
b Bottom position, relative to parent
and finally adding it to our layout:
relativeView.addView(newImage);
or we can add text:
TextView newText = new TextView(mContext);
newText.setText(“This is the text that its going to appear”);
adding it to the layout in the same way:
relativeView.addView(newText);
Once we have added all elements we want to our layout, we have to create a paint object:
Paint paint = new Paint();
just to define default values of painting.
We use the “drawBitmap” method from the canvas:
canvas.drawBitmap(ViewBgrnd, 0, 0, paint);
and finally we call dispatchDraw in the layout to draw the children views (imageView, textView) in the canvas.
relativeView.dispatchDraw(canvas);
The returnedBitmap is the bitmap that contains the drawing of the views in the canvas, on it, we have the layout and its childrens as a Bitmap, after painting them in the Canvas.
Conclusion
This is really tricky and maybe difficult to understand. It took me time to understand how it worked. I will try to summarize it:
We need to create a empty bitmap. This bitmap will be the final bitmap with the views on it.
We create the canvas using that bitmap.
We create a layout and we add as many elements as we want.
We attach the layout to the canvas.
Because we have created the canvas using a bitmap object, all that is drawn in the canvas, will be drawn in the bitmap.
From: Bright hub
i have a seekbar which i implemented in an xml file. i wanted to place this seekbar on a custom drawable rectangle which i have drawn using the canvas draw method and i have named the the class as "controlButtons.java", i.e different rectangles will act as menus as if.
my main problem is how should i make the seekbar appear on the rectangle without having to make the class "extend linearlayout".
is there a way to import something as a linearlayout and place it on the canvas
hope you'll be able to help me..thanks
raaj
Hope this help:
Bitmap bitmap = Bitmap.createBitmap(seekBarView.getMeasuredWidth(),
seekBarView.getMeasuredHeight() ,Bitmap.Config.ARGB_8888);
Canvas barCanvas = new Canvas(bitmap);
seekBarView.draw(canvas);
baseCanvas.drawBitmap(bitmap, x, y, null);
What I would do is create a bitmap from the canvas that contains the "custom drawable rectangle" using canvas.setBitmap(bitmap);. Then I would use TableLayout in XML to place the bitmap (using imageView) behind the seekbar. The gives you the ability to control the seekbar and get data from it without having to redraw the canvas again completly.