setShadowLayer Android的API差异差异、setShadowLayer、Android、API

2023-09-12 07:41:42 作者:一枕相思泪

我开发的自定义视图组件为我的应用程序,我挣扎与添加阴影圆。

I develop a custom view component for my application and I am struggling with adding a shadow to a circle.

下面是我的扩展视图类的code

Here is the code of my class extending View

public class ChartView extends View {


    public ChartView(Context context, AttributeSet attributeSet){
        super(context, attributeSet);
        init();


    }
    Paint paint;
    public void init(){
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.FILL);
        paint.setShadowLayer(30, 0, 0, Color.RED);

    }
    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawCircle(getWidth()/2, getHeight()/2,50, paint);
    }
}

不过,我注意到,根据API,上有shadowLayer有很大的影响。

However, I noticed that depending on the API, There is a big impact on the shadowLayer.

下面是输出

<uses-sdk android:targetSdkVersion="13"/>

这里是与输出

<uses-sdk android:targetSdkVersion="14"/> //Higher target API yields the same output.

不知道如何克服这种不必要的行为?

Any idea how to overcome this unwanted behaviour ?

最好的问候

推荐答案

setShadowLayer()只支持文本时,硬件加速开启。硬件加速是在默认情况下,当 targetSdk = 14 或更高。一个简单的解决方法是把你查看一个软件层: myView.setLayerType(View.LAYER_TYPE_SOFTWARE,空)

setShadowLayer() is only supported on text when hardware acceleration is on. Hardware acceleration is on by default when targetSdk=14 or higher. An easy workaround is to put your View in a software layer: myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null).

 
精彩推荐