AndroidTV: ImageView focus

Let’s take a look at the effect first

It is simply to create two new imageviews, then add focus to the two imageviews, and return the imageviw where the focus is in real time. 

 

 Simple packaging

Because all my buttons need to be customized through the cloud, they are all new in the code.

ImageViewClick.java
public class ImageViewClick {
    ImageFocus imageFocus;
    //焦点
    public static void Image(ImageView[] imageViews, Context context, ImageFocus imageFocus) {
        for(ImageView s : imageViews) {
            //初始化一下样式
            s.setBackgroundColor(0x80CFCFCF);
            s.setFocusable(true);
            s.isFocusableInTouchMode();
            //焦点
            s.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    Log.e("Home230115-t","找到焦点");
                    ImageViewStyle(hasFocus,s,context,imageFocus);
                }
            });
        }
    }

    private static void ImageViewStyle(Boolean tf, ImageView imageView,Context context,ImageFocus imageFocus) {
        if (tf) {
            Log.e("Home230115-t",tf+"");
            imageView.setBackground(context.getResources().getDrawable(R.drawable.imageview_border_show));
            imageView.setPadding(10,10,10,10);
            imageFocus.ChooseImage(imageView);
        } else {
            Log.e("Home230115-f",tf+"");
            imageView.setBackground(context.getResources().getDrawable(R.drawable.imageview_border_hint));
            imageView.setPadding(10,10,10,10);
        }
    }
}

 This is the interface

When I get the focus, I call back and tell the UI which image I am currently on.


ImageFocus.java

public interface ImageFocus {

    void ChooseImage(ImageView imageView);
}

It’s very simple to use

ll_tab is the layout I drew in xml to limit the position of these imageviews

    private void setTab(){
        ImageView iv_Film = new ImageView(MainActivity.this);
        iv_Film.setImageDrawable(getResources().getDrawable((R.drawable.ic_tab_film_n)));
        ll_tab.addView(iv_Film);

        ImageView iv_Catering = new ImageView(MainActivity.this);
        iv_Catering.setImageDrawable(getResources().getDrawable((R.drawable.ic_tab_film_n)));
        ll_tab.addView(iv_Catering);


        ImageView imageViews[] = {iv_Film,iv_Catering};
        ImageViewClick.Image(imageViews, MainActivity.this, new ImageFocus() {
            @Override
            public void ChooseImage(ImageView imageView) {
                //imageView.setImageDrawable(getResources().getDrawable((R.drawable.logo)));
            }
        });
    }

Finally there are two styles selected and unselected

imageview_border_hint.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <corners android:radius="0px" />
    <stroke
        android:width="0px"
        android:color="@color/teal_200" />
    <solid android:color="@android:color/transparent" />

<!--    <padding-->
<!--        android:bottom="10dp"-->
<!--        android:left="10dp"-->
<!--        android:right="10dp"-->
<!--        android:top="10dp" />-->
</shape>

imageview_border_show.xml 

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <corners android:radius="5dp" />
    <stroke
        android:width="2dp"
        android:color="#ffffff" />
    <solid android:color="#00000000" />
<!--    <size-->
<!--        android:height="260dp"-->
<!--        android:width="155dp"/>-->

<!--        <padding-->
<!--            android:bottom="20dp"-->
<!--            android:left="20dp"-->
<!--            android:right="20dp"-->
<!--            android:top="20dp" />-->
</shape>

Guess you like

Origin blog.csdn.net/title71/article/details/130102498