how to draw path into canvas in android? - android

hi i want to draw path into canvas but it does not do anything, it does not draw the canvas either.
my code:
Path path = new Path();
Canvas canvas = new Canvas();
Paint paint = new Paint();
paint.setColor(Color.BLACK);
canvas.drawColor(Color.CYAN);
for (int i = 5; i < 50; i++)
{
path.setLastPoint(4, i - 1);
path.lineTo(4, i);
}
path.close();
for (int i = 0; i < 5; i++)
{
View iview = inflater.inflate(R.layout.linear_layout, null);
iview.findViewById(R.id.imageView1);//.setBackgroundColor(backgroundColors[i]);
ShapeDrawable mDrawable = new ShapeDrawable(new OvalShape());
mDrawable.getPaint().setColor(Color.YELLOW);
mDrawable.setBounds(10, 10, 15, 15);
canvas.drawPath(path, paint);
mDrawable.draw(canvas);
iview.draw(canvas);
realViewSwitcher.addView(iview);
}
it shows me only green color which is the default value of the view.backroundColor.
thanks

Canvas is just a "tool" for drawing over a Bitmap. You need to associate them this way:
Canvas canvas = new Canvas(someBitmap);
However this only lets you draw on someBitmap. If you want to draw in the view, you have to do that in onDraw(Canvas), using the Canvas you're provided with, which is already bound with the bitmap that is the real view's drawing.
To use onDraw(), you have to make a custom View (that is, extend from some View).
Then there is SurfaceView but that's another thing (and I don't know much about it).

Related

draw free hand path on bitmap and create new bitmap from drawing part of bitmap : ANDROID

in detail please refer below image,
i think image clearly describe my problem that i want crop part as selected area not area inside it!
currently i am using canvas for draw free hand path and storing all Points on which user touches and than get crop image like,
Canvas canvas = new Canvas(resultingImage);
Paint paint = new Paint();
paint.setAntiAlias(true);
Path path = new Path();
for (int i = 0; i < SomeView.points.size(); i++)
{
path.lineTo(SomeView.points.get(i).x, SomeView.points.get(i).y);
}
canvas.drawPath(path, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC));
canvas.drawBitmap(bitmap2, 0, 0, paint);
so here resultingImage bitmap is my final cropped bitmap.
how can i achieve my above problem's solution.

Android View.draw(bitmap) saving view to bitmap does not render BlurMaskFilter properly

I am trying to programmatically create a gray Rect and black Rect with BlurMaskFilter layer (drop shadow effect) by overriding onDraw in a custom View. I am able to get it to draw on screen without any issues, but when I try to draw the view to a bitmap the results are different. In drawing to bitmap, it appears BlueMaskFilter is applied to the View rather than the specific layer of the black Rect.
What am I missing to make the drawn bitmap same as the output drawn on screen?
CustomView's onDraw override:
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// draw black box (shadow) first
RectF rectShadow = new RectF();
rectShadow.set(10, 10, 120, 120);
Paint shadowPaint = new Paint();
shadowPaint.setMaskFilter(new BlurMaskFilter(5, Blur.NORMAL));
shadowPaint.setStyle(Paint.Style.FILL);
shadowPaint.setColor(Color.DKGRAY);
shadowPaint.setAntiAlias(true);
canvas.drawRect(rectShadow, shadowPaint);
setLayerType(View.LAYER_TYPE_SOFTWARE, shadowPaint);
// draw grey box
RectF rectGray = new RectF();
rectGray.set(0, 0, 100, 100);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.parseColor("#656565"));
paint.setAntiAlias(true);
canvas.drawRoundRect(rectGray, paint);
}
Function to save view to bitmap:
public void saveViewToBitmap( View customView int outWidth, int outHeight ) {
customView.setLayoutParams(new ViewGroup.LayoutParams(outWidth, outHeight));
customView.measure(
ViewGroup.MeasureSpec.makeMeasureSpec(customView.getLayoutParams().width,
ViewGroup.MeasureSpec.EXACTLY),
ViewGroup.MeasureSpec.makeMeasureSpec(customView.getLayoutParams().height,
ViewGroup.MeasureSpec.EXACTLY));
customView.layout(0, 0,
customView.getMeasuredWidth(), customView.getMeasuredHeight());
customView.requestLayout();
Bitmap outputBitmap = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(outputBitmap);
customView.draw(canvas);
return outputBitmap;
}
Note in the following images the screen image has crisp edges for the gray rect, whereas the drawn bitmap has the top and left edges of the gray rect blurred demonstrating the problem.
Image on screen:http://i.stack.imgur.com/0Hl0t.png
Image saved from bitmap created by saveViewToBitmap: http://i.stack.imgur.com/xNzi5.png
Thanks!

transparent Triangle inside a circle how can i override the color of the circle

I have to draw a custom shapeDrawable consisting of a triangle inside a circle. I can draw the circle and the triangle the question is that i want the triangle to be transparent but when i give a transparent color to the path drawing a triangle i will see the color of the circle insde the triangle i want to not have the color of the circle as backround of the triangle ?
here's the code i'm using :
#Override
protected void onDraw(Canvas canvas) {
Paint p = new Paint ();
int x = 1;
int y = 1;
Rect bounds = canvas.getClipBounds ();
p.setColor (color.getBackgroundColor());
p.setStyle(Style.FILL);
p.setStrokeWidth (0);
Path path = new Path();
Point p1 = new Point(bounds.centerX()-bounds.height()/4, bounds.centerY()-bounds.height()/4);
Point p2 = new Point(bounds.centerX()-bounds.height()/4, bounds.centerY()+bounds.height()/4);
Point p3 = new Point(bounds.centerX()+bounds.height()/4, bounds.centerY());
path.moveTo(p1.x, p1.y);
path.lineTo(p2.x, p2.y);
path.lineTo(p3.x, p3.y);
path.close();
Paint pTriang = new Paint();
pTriang.setColor(Color.TRANSPARENT);
pTriang.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.CLEAR));
canvas.drawCircle(bounds.exactCenterX(), bounds.exactCenterY(), bounds.height()/2, p);
canvas.drawPath(path, pTriang );
}
BTW i'm overriding an ImageView!
Instead of trying to draw the Triangle, how about clipping the canvas?
/* set up triangle path... */
path.close();
canvas.save();
canvas.clipPath(path, Region.Op.XOR);
canvas.drawCircle(...);
canvas.restore();
I think you need to set the layer type to software on Honeycomb and later. Add this at the end of your constructor:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setLayerType(LAYER_TYPE_SOFTWARE, null);
}

Android Mapview slows after drawing too much polygon?

I've this custom class that extends the Overlay that is being added into my mapview. i have just one of this class that adds all my polygon and text into this overlay class. However this results in a very slow mapview. I added a draw integer and tested out that this class will draw 84 times each time the ondraw function is being called. Is there any solution that will help to reduce the loading speed of the mapview? Right now the mapview is very slow, each time i move left right or even zoom will be very slow. looking at the android catlog, it seems to me that overlay class ondraw is being called everysecond? should i be looking at another type of layer instead of using overlay?
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow)
{
shadow=false;
int numberofdraw= 0;
//outline
Paint paint = new Paint();
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStrokeWidth(2);
//paint.setColor(0x10000000);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
paint.setAntiAlias(true);
Point point1_draw = new Point();
for (int i =0;i<data.getCustomPolygonList().size();i++)
{
CustomPolygon customPolygon= data.getCustomPolygonList().get(i);
Path path = new Path();
path.setFillType(Path.FillType.EVEN_ODD);
for(int n=0;n<customPolygon.getCorrdinateList().size();n++)
{
GeoPoint sector1 = new GeoPoint((int)(customPolygon.getCorrdinateList().get(n).getLatitude()*1e6), (int)((customPolygon.getCorrdinateList().get(n).getLongitude())*1e6));
if(n==0){
mapView.getProjection().toPixels(sector1, point1_draw);
path.moveTo(point1_draw.x,point1_draw.y);
}else
{
mapView.getProjection().toPixels(sector1, point1_draw);
path.lineTo(point1_draw.x,point1_draw.y);
}
}
path.close();
canvas.drawPath(path, paint);
numberofdraw++;
// canvas.clipPath(path, Op.DIFFERENCE);
}
//inside sector color
for (int i =0;i<data.getCustomPolygonList().size();i++)
{
CustomPolygon customPolygon= data.getCustomPolygonList().get(i);
paint = new Paint();
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStrokeWidth(2);
paint.setColor(0x186666ff);
//paint.setColor(customPolygon.getColor());
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setAntiAlias(true);
point1_draw = new Point();
Path path = new Path();
path.setFillType(Path.FillType.EVEN_ODD);
for(int n=0;n<customPolygon.getCorrdinateList().size();n++)
{
GeoPoint sector1 = new GeoPoint((int)(customPolygon.getCorrdinateList().get(n).getLatitude()*1e6), (int)((customPolygon.getCorrdinateList().get(n).getLongitude())*1e6));
if(n==0){
mapView.getProjection().toPixels(sector1, point1_draw);
path.moveTo(point1_draw.x,point1_draw.y);
}else
{
mapView.getProjection().toPixels(sector1, point1_draw);
path.lineTo(point1_draw.x,point1_draw.y);
}
}
path.close();
numberofdraw++;
canvas.drawPath(path, paint);
}
//inside sector text
for (int i =0;i<data.getCustomPolygonList().size();i++)
{
CustomPolygon customPolygon= data.getCustomPolygonList().get(i);
TextPaint paintText = new TextPaint();
Point point1 = new Point();
String text=customPolygon.getName();
for(int n=0;n<customPolygon.getCorrdinateList().size();n++)
{
if(customPolygon.getTextLocation()!=null)
{
paintText.setTextSize(24);
Rect rect = new Rect();
paintText.getTextBounds(text, 0, text.length(), rect);
paintText.setTextAlign(Paint.Align.CENTER);
paintText.setTypeface(Typeface.DEFAULT_BOLD);
paintText.setColor(Color.BLACK);
GeoPoint sector1 = new GeoPoint((int)(customPolygon.getTextLocation().getLatitude()*1e6), (int)((customPolygon.getTextLocation().getLongitude())*1e6));
mapView.getProjection().toPixels(sector1, point1);
}
}
numberofdraw++;
canvas.drawText(text, point1.x, point1.y, paintText);
}
Log.e(Config.log_id,"draw no. "+ numberofdraw+"");
}
there are many ways you can improve it, I will suggest you one.
Buffering:
For each polygon create a new canvas and a new bitmap
Canvas myBufferCanvas;
Bitmap myBufferBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
myBufferCanvas = new Canvas(myBufferBitmap);
Then only call draw when the polygon is change, call the draw with myBufferCanvas and then call drawBitmap on the true canvas.
The advantage of this method is performance, it will be extremely fast! The disadvantage is memory, if you have to many polygons, you can kill the device. Just try reusing them some home and you will be fine.
Just remember that you can apply any transformation to your buffered image without redrawing it.
I think you should be using a custom ItemizedOverlay to manage drawing items on your MapView. That aside, there are some changes you can make to your existing code which I'm sure would speed it up. In your draw() method you are creating new Paint objects each time, setting their properties and then disposing of them. Worse still, inside the draw() method you loop through the polygon list twice, creating more Paint objects or TextPaint objects. Instead, you can create one of each Paint object once when the Overlay is initialised and then re-use them in the loop. You may also be able to do everything in one loop.
Process the data, then build the paths and finally paint the paths on the map, see the response from TWiStErRob in this post.

Canvas object as thumb in SeekBar, Test align. Android

I have a problem with set up the right position of the text as a paint in canvas object,
which I use as thumb in customise SeekBar.
public BitmapDrawable writeOnDrawable(int drawableId, String text)
{
Bitmap bm = BitmapFactory.decodeResource(getResources(),drawableId).copy(Bitmap.Config.ARGB_8888, true);
bm.setDensity(165);
Paint paint = new Paint();
paint.setStyle(Style.FILL);
paint.setColor(Color.WHITE);
paint.setTextSize(size);
paint.setTypeface(my.b1);
paint.setTextAlign(Paint.Align.CENTER);
Canvas canvas = new Canvas(bm);
canvas.setDensity(165);
canvas.drawText(text, 0, bm.getHeight()/2, paint);
return new BitmapDrawable(bm);
}
There is a circle with current progress. So the text is
dynamic cause can be from 2 characters ("0-9%") to
4 characters ("100%"). Also the problem is not only with position,
but also with the size of the text.

Categories

Resources