Custom SHAPED Layout with clipping bounds - android

Well yes, I couldn't find anything that could help me for real so far... from StackOverflow to libraries and other things...
I need a custom shaped FrameLayout that is clipping his child inside of it. The problem is that I want this shape to be really custom, for example shirt / hat / etc..
So, somehow I want to clip child of FrameLayout inside the shirt let's say. I found something related to onDraw method, but I didn't find a way of getting pathData of a vector. . .
Any clear idea about how I could sort this up ( if possible ) is welcomed !
Thank you

You can use clip path. Your code can look something like:
private void updatePath() {
path = new Path();
// Draw your special shape...
}
#Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
canvas.clipPath(path);
return super.drawChild(canvas, child, drawingTime);
}
#Override
protected void onDraw(Canvas canvas) {
canvas.clipPath(path);
super.onDraw(canvas);
}

Related

Android canvas draw order

I want to draw a border around my circle drawable.
I have this code:
public void draw(Canvas canvas) {
myDrawable.draw(canvas);
canvas.drawArc(toHighlightBounds, 0F, 360F, /* useCenter= */ false, borderPaint);
}
How come the output is similar if I change the lines order?
public void draw(Canvas canvas) {
canvas.drawArc(toHighlightBounds, 0F, 360F, /* useCenter= */ false, borderPaint);
myDrawable.draw(canvas);
}
Shouldn't the order dictates the z-axis? What is drawn above the other?
I don't know for sure but I think it has something to do with the way variables work.
For example, Let's say I make a data class with a drawable in it.
I then set the drawable to an image view
myImageView.setImageDrawable(myClassObject.drawable)
Here come's the funny part, if I now change the drawable of my object to something else, it will also change in the image view.
Im not sure if this is it and if it is an easy explanation but this is my guess

onDraw method not drawing everything I put in to it

I have created a custom view class to use in a project I'm working on. To put is simply, I'm displaying an image, then adding images on top of the original image (currently by clicking on the image, but that's not final).
I have these 2 methods in my custom view:
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
for (Drawable d : drawableList)
{
d.draw(canvas);
}
}
public void AddPoint(float x, float y)
{
Drawable tempDrawable = pin;
tempDrawable.setBounds((int)x, (int)y, (int)x + 50, (int)y + 50);
drawableList.add(tempDrawable);
invalidate();
}
The AddPoint() method is called in an OnTouchListener and is passes the coordinates of the touch event.
The way it currently works is it displays the main image, but will only display the most recent images where I clicked, previous ones just disappear.
Does anybody know what I am doing wrong here?
I've figured out what I was doing wrong.
The line of code:
Drawable tempDrawable = pin;
I changed to:
Drawable tempDrawable = mainRes.getDrawable(R.drawable.pin);
I got mainRes from the init(Context context) method I wrote in the custom View class.

Insert Drawable shape to View

I would like to send each time different Drawable shape to view and then add it to my activity.
The problem is I can't find a way to add the drawable shape into the canvas when I'm overriding the onDraw method.
The design supposed to be reusable, I can draw rect first and then draw circle...
I want to find a way to send the view different shape. Is it possible?
#Override
protected void onDraw(Canvas canvas) {
// Draw the ball
ballBounds.set(ballX-ballRadius, ballY-ballRadius, ballX+ballRadius, ballY+ballRadius);
paint.setColor(Color.GREEN);
// canvas.drawOval(ballBounds, paint);
canvas.drawRect(ballBounds, paint);
}
Although, your question needs more explanation on what you need, but with what I could understand is you want rect, or circle on desire, here it is,
Add, global variable int ShapeStructure = 0;
#Override
protected void onDraw(Canvas canvas)
{
// Draw the ball
ballBounds.set(ballX-ballRadius, ballY-ballRadius, ballX+ballRadius, ballY+ballRadius);
paint.setColor(Color.GREEN);
if(ShapeStructure == 0){
canvas.drawOval(ballBounds, paint);
}else{
canvas.drawRect(ballBounds, paint);
}
}
Now that, you have a variable at your disposal, you can control what you want to paint.
Hope, this helps, let me know if you want something else.

Android Animate canvas.drawLine

I'm trying to draw a few lines on a DrawView which extends SurfaceView.
in my onDraw method I loop over a list of float arrays and draw lines.
#Override
public void onDraw(final Canvas canvas) {
synchronized (lines) {
super.onDraw(canvas);
for (float[] l : lines) {
paint.setAntiAlias(true);
canvas.drawLines(l, paint);
}
}
}
I would like to animate each of these lines. I've tried to use ViewAnimator even though it's only Honeycomb and above but either I don't understand how to use it or it just isn't meant to be used with canvas.drawLine(). I've tried to use paint.setPathEffect but it doesn't seem to work at all. Anyone have any idea how to do this? I just want each line to take something like 1 second to draw one after the other.

Android Bitmap OnDraw

I created a layout in my myviewlay.xml but this is not working, what am i missing?
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
imageBack = BitmapFactory.decodeResource(getResources(), R.layout.myviewlay, null);
}
Add this to do the actual drawing:
canvas.drawBitmap(imageBack, x, y, mPaint); //replace x,y,and mPaint with whatever you need to.
However, if you're trying to display an entire layout use setContentView(imageBack) or something similar. It's recommended to do your layout in XML.
Update: Sorry I misunderstood you at first. It looks like you ARE trying to inflate a layout from XML. In that case, in your onCreate(), call setContentView(R.layout.myviewlay);

Categories

Resources