将焦点转移到该组件时更改ImageView或Button的不透明度

问题描述 投票:0回答:1

所以,我正在使用覆盖另一个ImageViewImageView.默认情况下,该图像的不透明度或alpha值设置为0.2f或其他值。当我移动或将光标放在该图像上时,我想将ImageView的不透明度更改为完全可见。我在Android工作室电视模拟器中运行此应用程序。我已阅读文档和其他解决方案,但它无法正常工作。

这是我的布局文件,我使用两个彼此重叠的ImageView。在这种情况下,第二个是重叠。

<RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="350dp"
            android:gravity="center">

               <ImageView
                android:id="@+id/premiumimageDescription"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingLeft="@dimen/margin"
                android:paddingRight="@dimen/margin"
                android:scaleType="fitXY" />


            <ImageView
                android:focusable="true"

               android:alpha="0.2"
                android:id="@+id/trailerView"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:layout_centerInParent="true"
                android:adjustViewBounds="true"
                android:src="@drawable/playbutton" />

        </RelativeLayout>

Java文件,我使用focuschange方法在光标到达ImageView时更改不透明度:

trailerView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                    @Override
                    public void onFocusChange(View v, boolean hasFocus) {
                        switch (v.getId())
                        {
                            case R.id.trailerView:
                                if(hasFocus)
                                {
                                    trailerView.getDrawable().setAlpha(1);
                                    trailerView.getBackground().setAlpha(255);
                                    trailerView.setImageAlpha(1);
                                }
                                else
                                {
                                    trailerView.getBackground().setAlpha(50);
                                    trailerView.setImageAlpha((int) 0.2);
                                }
                        }
                    }
                });
android opacity android-tv television
1个回答
1
投票

可能解决方案

1:我认为创建可绘制文件是有害的,它负责检查统计数据并相应地替换可绘制图像。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/image_pressed"/>
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/image_pressed"/>
<item android:state_focused="true" android:drawable="@drawable/image_selected"/>
<item android:state_focused="false" android:state_pressed="false" android:drawable="@drawable/image_default"/>

2:而不是在ImageView上的onFocusChange,它使用setOnTouchListener,如下所示

        trailerView.setOnTouchListener(new View.OnTouchListener(){
        @Override
        public boolean onTouch (View v,MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                Toast.makeText( getApplicationContext(),"Got the focus",Toast.LENGTH_LONG ).show();
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                Toast.makeText( getApplicationContext(),"Got the focus",Toast.LENGTH_LONG ).show();
            }

            // also let the framework process the event
            return false;
        }
    });
© www.soinside.com 2019 - 2024. All rights reserved.