setAlpha表现得很奇怪

时间:2017-06-28 23:23:40

标签: android alpha

我使用windowmanager添加了一个带特殊参数的视图,它覆盖了屏幕,我试图使用搜索栏更改此视图的alpha。我尝试更改按钮的alpha只是为了测试我的代码,它工作正常,但是当我尝试更改叠加视图的alpha时,它只是将颜色更改为透明黑色(通常是透明的粉红色或黄色) 。我希望改变黄色或粉红色的不透明度,但它似乎不会那样工作。我认为这是因为特殊的参数,或者它是使用windowmanager添加的。 float alpha = intent.getExtras().getFloat("alpha");从我的主要活动向下传递,是我的搜索栏(max = 100)/ 100的进度,因此它最终为0.0到1.0的值。如果有人有任何想法让我知道,谢谢。这是服务:

`public class DrawOverAppsService extends Service {

    public static final String TAG = "DrawOverAppsService";



    View mOverlayView;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        Log.d(TAG, "onCreate");


        final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                        WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
                        WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN|
                        WindowManager.LayoutParams.FLAG_DIM_BEHIND,
                PixelFormat.TRANSLUCENT);

        // An alpha value to apply to this entire window.
        // An alpha of 1.0 means fully opaque and 0.0 means fully transparent
        params.alpha = 0.2F;

        // When FLAG_DIM_BEHIND is set, this is the amount of dimming to apply.
        // Range is from 1.0 for completely opaque to 0.0 for no dim.
        params.dimAmount = 0.5F;

        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);


        LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


        mOverlayView = inflater.inflate(R.layout.overlay_view, null);

        wm.addView(mOverlayView, params);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (mOverlayView != null) {
            Boolean isBlack = intent.getExtras().getBoolean("black");
            Boolean isPink = intent.getExtras().getBoolean("pink");
            Boolean isOrange = intent.getExtras().getBoolean("orange");
            Boolean isAlpha = intent.getExtras().getBoolean("isAlpha");

            if(isBlack == true) {
                mOverlayView.setBackgroundColor(Color.parseColor("#000000"));
            }
            if(isPink == true) {
                mOverlayView.setBackgroundColor(Color.parseColor("#3d0d00"));
            }
            if(isOrange == true) {
                mOverlayView.setBackgroundColor(Color.parseColor("#3d2f00"));
            }
            if (isAlpha == true) {
                float alpha = intent.getExtras().getFloat("alpha");

                mOverlayView.getBackground().setAlpha((int)alpha);
                //mOverlayView.setAlpha(intent.getExtras().getFloat("alpha"));

            }

        }
        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        Log.d(TAG, "onDestroy");

            WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
            wm.removeView(mOverlayView);


    }
}`

1 个答案:

答案 0 :(得分:0)

打开()

mOverlayView .animate().scaleX(1.0f).scaleY(1.0f).setDuration(200)
                .start();
        mOverlayView .setAlpha(0.5f);

Close()方法

mOverlayView .animate().scaleX(0f).scaleY(0f).setDuration(200).start();