Alix Depauw
Alix Depauw

Reputation: 61

Animated view is translated behind oval view

Here is the current behaviour. I want the oval view to overlap the animation view. The 'shoulders' need to be hidden inside that oval view.

I'm already using FrameLayout and also tried to bringChildToFront, use translation, elevation but nothing seems to work.

If you need more details to have a bigger picture of the problem please let me know.

Layout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/parentLinearLayout"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/id_face" />

<a.b.c.OvalAnimationView
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</FrameLayout>

Draw logic:

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    if (w != 0 && h != 0) {
        if (horizontalMargin == 0) {
            horizontalMargin = getMeasuredWidth() / WIDTH_FACTOR;
        }

        if (verticalMargin == 0) {
            verticalMargin = 0.11f * getMeasuredHeight();
        }

        fullScreenRect = new Rect(0, 0, getMeasuredWidth(), getMeasuredHeight());

        firstArc = new RectF(
                horizontalMargin,
                verticalMargin,
                getMeasuredWidth() - horizontalMargin,
                getMeasuredHeight() - verticalMargin);

        secondArc = new RectF(
                horizontalMargin,
                verticalMargin,
                getMeasuredWidth() - horizontalMargin,
                getMeasuredHeight() - verticalMargin);
    }
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    canvas.drawArc(firstArc, 0, 360, true, borderPaint);
    canvas.drawArc(secondArc,0, 360, true, eraser);
}

public void init(int borderColor) {
    this.horizontalMargin = 0;
    this.verticalMargin = 0;

    borderPaint = new Paint();
    borderPaint.setColor(borderColor);
    borderPaint.setStrokeWidth(35);
    borderPaint.setStyle(Paint.Style.STROKE);

    eraser = new Paint();
    eraser.setColor(Color.TRANSPARENT);
    eraser.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
    eraser.setAntiAlias(true);
}

Animation:

private void moveViewToScreenCenter( View view )
{
    DisplayMetrics dm = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
    int statusBarOffset = dm.heightPixels - lLayout.getMeasuredHeight();

    int originalPos[] = new int[2];
    view.getLocationOnScreen( originalPos );

    int xDest = dm.widthPixels/2;
    xDest -= (view.getMeasuredWidth()/2);
    int yDest = dm.heightPixels/2 - (view.getMeasuredHeight()/2) - statusBarOffset;

    AnimationSet animSet = new AnimationSet(true);
    animSet.setFillAfter(true);
    animSet.setDuration(2500);
    animSet.setInterpolator(new AccelerateInterpolator());
    TranslateAnimation translateAnimation = new TranslateAnimation( 0, xDest - originalPos[0] , 0, yDest - originalPos[1]);
    animSet.addAnimation(translateAnimation);
    ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.70f, 1.0f, 0.70f, ScaleAnimation.RELATIVE_TO_PARENT, .5f, ScaleAnimation.RELATIVE_TO_PARENT, .4f);
    animSet.addAnimation(scaleAnimation);
    animSet.setZAdjustment(-1);

    new Handler().postDelayed(new Runnable() {
        public void run() {
            iFaceListener.onFaceAnimationFinished();
        }
    }, animSet.getDuration());
    view.startAnimation(animSet);
}

Upvotes: 2

Views: 146

Answers (1)

Zack Kaytranada
Zack Kaytranada

Reputation: 361

Fixed it by myself.

Code for posteriority:

public class OvalAnimationView extends View {

private Paint transparentPaint = new Paint();
private Paint tPaint = new Paint();
private Paint eraser = new Paint();
private Paint borderPaint = new Paint();
private float horizontalMargin;
private float verticalMargin;
private Canvas temp;

private float WIDTH_FACTOR = 15f;

private Rect fullScreenRect;
private RectF firstArc, secondArc;

private Bitmap bitmap;

public OvalAnimationView(Context context) {
    super(context);
}

public OvalAnimationView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
}

public OvalAnimationView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    setLayerType(LAYER_TYPE_SOFTWARE, null);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    if (w != 0 && h != 0) {
        if (horizontalMargin == 0) {
            horizontalMargin = getMeasuredWidth() / WIDTH_FACTOR;
        }

        if (verticalMargin == 0) {
            verticalMargin = 0.11f * getMeasuredHeight();
        }

        fullScreenRect = new Rect(0, 0, getMeasuredWidth(), getMeasuredHeight());

        firstArc = new RectF(
                horizontalMargin,
                verticalMargin,
                getMeasuredWidth() - horizontalMargin,
                getMeasuredHeight() - verticalMargin);

        secondArc = new RectF(
                horizontalMargin,
                verticalMargin,
                getMeasuredWidth() - horizontalMargin,
                getMeasuredHeight() - verticalMargin);
    }
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
}

public void init(int borderColor) {
    this.horizontalMargin = 0;
    this.verticalMargin = 0;

    temp = new Canvas();

    transparentPaint = new Paint();
    transparentPaint.setColor(Color.BLACK);
    transparentPaint.setStyle(Paint.Style.FILL);

    tPaint = new Paint();
    tPaint.setColor(Color.TRANSPARENT);
    tPaint.setStyle(Paint.Style.FILL);

    borderPaint = new Paint();
    borderPaint.setColor(borderColor);
    borderPaint.setStrokeWidth(35);
    borderPaint.setStyle(Paint.Style.STROKE);
}

@Override
protected void dispatchDraw(Canvas canvas) {
    super.dispatchDraw(canvas);

    if (bitmap == null) {
        createWindowFrame();
    }
    canvas.drawBitmap(bitmap, 0, 0, null);
}


protected void createWindowFrame() {
    bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
    Canvas osCanvas = new Canvas(bitmap);

    RectF outerRectangle = new RectF(0, 0, getWidth(), getHeight());
    osCanvas.drawRect(fullScreenRect, transparentPaint);


    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.TRANSPARENT);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
    float centerX = getWidth() / 2;
    float centerY = getHeight() / 2;
    float radius = 50;
    osCanvas.drawCircle(centerX, centerY, radius, paint);
    osCanvas.drawArc(firstArc, 0, 360, true, paint);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);
    bitmap = null;
}
}

Upvotes: 2

Related Questions