Created
October 26, 2012 14:35
-
-
Save mr-archano/3959176 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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