Skip to content

Instantly share code, notes, and snippets.

@mr-archano
Created October 26, 2012 14:35
Show Gist options
  • Save mr-archano/3959176 to your computer and use it in GitHub Desktop.
Save mr-archano/3959176 to your computer and use it in GitHub Desktop.
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Bitmap.Config;
import android.util.AttributeSet;
import android.widget.LinearLayout;
public class FadableLinearLayout extends LinearLayout {
protected float mAlpha = 1f;
protected Bitmap offScreen;
public FadableLinearLayout(Context context) {
super(context);
myInit();
}
public FadableLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
myInit();
}
public FadableLinearLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
myInit();
}
protected void myInit() {
setWillNotDraw(false);
setDrawingCacheEnabled(true);
}
public void changeAlpha( float alpha ) {
mAlpha = alpha;
int x = 0, y = 0, w = getMeasuredWidth(), h = getMeasuredHeight();
invalidate(x,y,w,h);
}
protected Paint p = new Paint();
@Override
public void draw(Canvas canvas) {
if( mAlpha != 1f ) {
int x = 0, y = 0, w = getMeasuredWidth(), h = getMeasuredHeight();
int a = (int)(mAlpha * 255);
if( offScreen == null ) {
offScreen = Bitmap.createBitmap(w, h, Config.ARGB_8888);
Canvas c = new Canvas(offScreen);
super.draw(c);
}
p.setAlpha(a);
canvas.drawBitmap(offScreen, x, y, p);
}
else {
if( offScreen != null ) {
offScreen.recycle();
offScreen = null;
}
super.draw(canvas);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment