Skip to content

Instantly share code, notes, and snippets.

@charlieCollins
Created October 6, 2011 20:32
Show Gist options
  • Save charlieCollins/1268574 to your computer and use it in GitHub Desktop.
Save charlieCollins/1268574 to your computer and use it in GitHub Desktop.
Eric Burke's PlasticLinearLayout
<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>
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));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment