Using AlphaAnimation would be a great solution for most transitions, and it would certainly work for me if I couldn’t find a way to do exactly what I was trying to do, which includes fading between the two views using the principle of grading to the angle of the device. Fortunately, I have! Here is the strategy I took: I wrapped the view in a custom subclass of FrameLayout and implemented onDraw. There, I grabbed the child view as a bitmap, and then redraw the bitmap with the alleged alpha. Here is the code. I will edit when they remove me, this is just a proof of concept, but it works like a charm:
public class AlphaView extends FrameLayout { private int alpha = 255; public AlphaView(Context context) { super(context); setWillNotDraw(false); } public AlphaView(Context context, AttributeSet attrs) { super(context, attrs); setWillNotDraw(false); } public AlphaView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); setWillNotDraw(false); } public void setCustomAlpha(int alpha) { if (this.alpha != alpha) { this.alpha = alpha; invalidate(); } } public int getCustomAlpha() { return alpha; } @Override protected void onDraw(Canvas canvas) { for(int index = 0; index < getChildCount(); index++ ) { View child = getChildAt(index); child.setVisibility(View.INVISIBLE); child.setDrawingCacheEnabled(true); Bitmap bitmap = child.getDrawingCache(true); bitmap = Bitmap.createBitmap(bitmap); child.setDrawingCacheEnabled(false); Paint paint = new Paint(); paint.setAlpha(alpha); canvas.drawBitmap(bitmap, 0, 0, paint); } } }
source share