setAlpha behaving weird

帅比萌擦擦* 提交于 2019-12-25 11:54:52

问题


I have added a view with special params using windowmanager, it overlays the screen and I am trying to change the alpha of this view using a seekbar. I have tried changing the alpha of a button just to test my code, and it works properly, but when I try to change the alpha of the overlay view, it just changes colour to a transparent black (it is normally transparent pink or yellow). I was hoping to change the opacity of the yellow or pink, but it does not seem to work that way. I think it is because of the special params, or the fact that it is added using windowmanager. The float alpha = intent.getExtras().getFloat("alpha"); is passed down from my main activity and is the progress of my seekbar(max = 100)/100 so it ends up being a value of 0.0 to 1.0. If anybody has any ideas let me know, thanks. Here is the service:

`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:


Open()

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


来源:https://stackoverflow.com/questions/44814042/setalpha-behaving-weird

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!