setAlpha表现怪异 [英] setAlpha behaving weird

查看:104
本文介绍了setAlpha表现怪异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用windowmanager添加了具有特殊参数的视图,它覆盖了屏幕,并且我正在尝试使用搜索栏更改该视图的alpha。我试图更改按钮的Alpha只是为了测试我的代码,它可以正常工作,但是当我尝试更改覆盖视图的Alpha时,它只是将颜色更改为透明的黑色(通常是透明的粉红色或黄色) 。我希望更改黄色或粉红色的不透明度,但这种方式似乎不起作用。我认为这是由于特殊的参数,或者是使用windowmanager添加的事实。 float alpha = intent.getExtras()。getFloat( alpha); 从我的主要活动中向下传递,是我的seekbar的进度(最大值= 100)/ 100,因此最终值为0.0到1.0。如果有人有任何想法请告诉我,谢谢。这是服务:

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


    }
}`


推荐答案

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

这篇关于setAlpha表现怪异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆