How to set alpha value of everything in api level 7 (Android 2.1)

I have an arbitrary point of view that I want to disappear on top of another view. In api level 11, I see that there is setAlpha, but I was stuck supporting api level 7. I did not come across an easy way to do this. How to set alpha for the whole view without conflict with each individual component?

+4
source share
3 answers

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); } } } 
+5
source

You should be able to achieve a reasonable effect using AlphaAnimation at API level 7.

  View v = findViewById(R.id.view2); AlphaAnimation aa = new AlphaAnimation(0f,1f); aa.setDuration(5000); v.startAnimation(aa); 
+13
source

This will depend on the type of view.

For a TextView in xml, you can have the following attributes:

 android:background="#00000000" android:textColor="#77FFFFFF" 

The first two numbers are alpha values ​​from 00 to FF (in hex). The background will be completely transparent, and the text will be white, partially transparent. I have not tested this, but it should work.

If you have a background that is an image, then the easiest thing to do is to first create your png with transparency.

+3
source

All Articles