Amokrane
10/8/2011 - 4:01 PM

Eric Burke's PlasticLinearLayout

Eric Burke's PlasticLinearLayout

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import com.movl.swipeit.R;

public class PlasticLinearLayout extends LinearLayout {
   
   private final Paint shinePaint;
   private Path shinePath;

   public PlasticLinearLayout(Context context, AttributeSet attrs) {
      super(context, attrs);
      
      shinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
      // The subtle gradient draws behind everything.
      setBackgroundResource(R.drawable.plastic_background);
   }

   @Override
   protected void dispatchDraw(Canvas canvas) {
      if (shinePath == null) {
         createShinePath();
      }
      // Draw the shine behind the children.
      canvas.drawPath(shinePath, shinePaint);
      // Draw the children.
      super.dispatchDraw(canvas);
   }

   @Override
   protected void onLayout(boolean changed, int l, int t, int r, int b) {
      super.onLayout(changed, l, t, r, b);
      // Invalidate the path whenever the size changes.
      shinePath = null;
   }

   private void createShinePath() {
      int width = getWidth();
      int height = (int) (0.85 * getHeight());
      shinePath = new Path();
      shinePath.moveTo(0, 0);
      shinePath.lineTo(width, 0);
      shinePath.lineTo(width, height);
      shinePath.close();
      shinePaint.setShader(new LinearGradient(0, 0, 0, height, 0x66ffffff, 0x10ffffff, Shader.TileMode.CLAMP));
   }
}
<shape xmlns:android="http://schemas.android.com/apk/res/android">
	<!-- Nearly white at the top, light blue at the bottom. -->
	<gradient android:startColor="#fff7f7f7" android:endColor="#aecee6"
		android:angle="270" />
</shape>