如何在 api level 7 (Android 2.1) 中设置整个视图的 alpha 值

发布于 2024-11-03 13:56:36 字数 128 浏览 2 评论 0原文

我有一个任意视图,我想淡入另一个视图之上。在 api level 11 中,我看到有一个 setAlpha,但我一直支持 api level 7。我还没有遇到一个简单的方法来做到这一点。如何设置整个视图的 Alpha,而不弄乱每个单独的组件?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心如狂蝶 2024-11-10 13:56:36

您应该能够使用 API 级别 7 的 AlphaAnimation 实现合理的效果。

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

    AlphaAnimation aa = new AlphaAnimation(0f,1f);
    aa.setDuration(5000);
    v.startAnimation(aa);

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);
心凉 2024-11-10 13:56:36

对于大多数过渡来说,使用 AlphaAnimation 将是一个很好的解决方案,如果我找不到一种方法来完成我想要做的事情,这肯定会为我工作,这涉及到基于倾斜以渐变方式在两个视图之间淡入淡出设备的角度。幸运的是我有!这是我采取的策略:我将视图包装在 FrameLayout 的自定义子类中,并实现 onDraw。在那里,我将子视图捕获为位图,然后使用预期的 Alpha 重新绘制位图。这是一些代码。当我清理干净后我会进行编辑,这只是概念证明,但它的作用就像一个魅力:

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);
        }
    }
}

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);
        }
    }
}
橪书 2024-11-10 13:56:36

这取决于视图类型。

对于 TextView,在 xml 中您可以具有以下属性:

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

前两个数字是从 00 到 FF(十六进制)的 alpha 值。
背景将是完全透明的,而文本将是白色且部分透明。我还没有测试过这个,但它应该有效。

如果您的背景是图像,那么最简单的方法就是预先创建具有透明度的 png 可绘制对象。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文