package uk.co.imallan.jellyrefresh;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;

/**
 * User: Yilun Chen
 * Date: 15/7/9
 */
public class JellyViewFrameLayout extends FrameLayout {
    Path path;

    Paint paint;

    private int minimumHeight = 0;

    private int jellyHeight;

    public JellyViewFrameLayout(Context context) {
        super(context);
        init();
    }

    public JellyViewFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public JellyViewFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @SuppressWarnings("unused")
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public JellyViewFrameLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        setWillNotDraw(false);
        if (isInEditMode()) {
            return;
        }
        path = new Path();
        paint = new Paint();
        paint.setColor(getContext().getResources().getColor(android.R.color.holo_blue_bright));
        paint.setAntiAlias(true);
    }

    public void setJellyColor(int jellyColor) {
        paint.setColor(jellyColor);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        path.reset();
        path.lineTo(0, minimumHeight);
        path.quadTo(getMeasuredWidth() / 2, minimumHeight + jellyHeight, getMeasuredWidth(), minimumHeight);
        path.lineTo(getMeasuredWidth(), 0);
        canvas.drawPath(path, paint);
    }

    @Override
    public void setMinimumHeight(int minimumHeight) {
        this.minimumHeight = minimumHeight;
    }

    public void setJellyHeight(int ribbonHeight) {
        this.jellyHeight = ribbonHeight;
    }

    @Override
    public int getMinimumHeight() {
        return minimumHeight;
    }

    public int getJellyHeight() {
        return jellyHeight;
    }
}

@@ -330,34 +330,16 @@
 er: 
-Yilun Chen%0A * Date: 15/7/9
+TaurusXi
 %0A */
@@ -410,17 +410,16 @@
 h path;%0A
-%0A
     Pain
@@ -427,17 +427,16 @@
  paint;%0A
-%0A
     priv
@@ -460,15 +460,10 @@
 ight
- = 0
 ;%0A
-%0A
     
@@ -547,29 +547,28 @@
         
-super
+this
 (context
 );%0A     
@@ -559,32 +559,25 @@
 (context
-);%0A        init(
+, null, 0
 );%0A    %7D
@@ -649,37 +649,36 @@
 ttrs) %7B%0A        
-super
+this
 (context, attrs)
@@ -676,32 +676,19 @@
 t, attrs
-);%0A        init(
+, 0
 );%0A    %7D
@@ -1594,24 +1594,72 @@
 aw(canvas);%0A
+        int measuredWidth = getMeasuredWidth();%0A
         path
@@ -1727,20 +1727,17 @@
 .quadTo(
-getM
+m
 easuredW
@@ -1740,18 +1740,16 @@
 redWidth
-()
  / 2, mi
@@ -1775,20 +1775,17 @@
 Height, 
-getM
+m
 easuredW
@@ -1788,18 +1788,16 @@
 redWidth
-()
 , minimu
@@ -1826,20 +1826,17 @@
 .lineTo(
-getM
+m
 easuredW
@@ -1839,18 +1839,16 @@
 redWidth
-()
 , 0);%0A  

optimize performance