android view level,How can I set an entire view's alpha value in api level 7 (Android 2.1)

问题

I have an arbitrary view that I want to fade in on top of another view. In api level 11 I see there is a setAlpha, but I'm stuck supporting api level 7. I haven't run across a simple way to do this. How can I set the alpha for the entire view without messing with each individual component?

回答1:

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

View v = findViewById(R.id.view2);

AlphaAnimation aa = new AlphaAnimation(0f,1f);

aa.setDuration(5000);

v.startAnimation(aa);

回答2:

Using AlphaAnimation would be an excellent solution for most transitions, and would certainly have worked for me if I couldn't find a way to do exactly what I was trying to do, which involves fading between two views in a graduated way based on the tilt 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 captured the child view as a bitmap, and then redrew the bitmap with the intended alpha. Here's some code. I'll edit when I get cleaned up, this is just 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);

}

}

}

回答3:

It would depend on the view type.

For a TextView, in xml you could have these attributes:

android:background="#00000000"

android:textColor="#77FFFFFF"

The first two numbers are the alpha values from 00 to FF (in hex).

The background would be fully transparent, while the text would be white an partially transparent. I have not tested this, but it should work.

If you have an background that is an image, then the easiest thing to do would to pre-create your png drawable with transparency.

来源:https://stackoverflow.com/questions/5793007/how-can-i-set-an-entire-views-alpha-value-in-api-level-7-android-2-1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值