├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── drawable-xhdpi
│ │ │ │ └── bg_light_focus.9.png
│ │ │ ├── values
│ │ │ │ ├── colors.xml
│ │ │ │ ├── strings.xml
│ │ │ │ └── styles.xml
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── layout
│ │ │ │ ├── fragment_scrollable_view_focus.xml
│ │ │ │ ├── recycler_item.xml
│ │ │ │ ├── dialog_for_focus_demo.xml
│ │ │ │ ├── activity_main.xml
│ │ │ │ └── fragment_simple_view_focus.xml
│ │ │ ├── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ │ └── drawable
│ │ │ │ └── ic_launcher_background.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── ryanhuen
│ │ │ └── androidtvfocus
│ │ │ ├── fragment
│ │ │ ├── ScrollableViewFocusFragment.java
│ │ │ └── SimpleViewFocusFragment.java
│ │ │ ├── adapter
│ │ │ └── SampleAdapter.java
│ │ │ └── MainActivity.java
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── ryanhuen
│ │ │ └── androidtvfocus
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── ryanhuen
│ │ └── androidtvfocus
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── lib
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── integers.xml
│ │ │ │ └── ids.xml
│ │ │ └── drawable-xhdpi
│ │ │ │ └── focus_highlight.9.png
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── ryanhuen
│ │ │ └── lib
│ │ │ └── widget
│ │ │ ├── FocusHighlightOptions.java
│ │ │ ├── FocusHighlightHelper.java
│ │ │ └── ShadowFocusView.java
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── ryanhuen
│ │ │ └── lib
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── ryanhuen
│ │ └── lib
│ │ └── ExampleInstrumentedTest.java
├── build.gradle
└── proguard-rules.pro
├── settings.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── README.md
├── gradle.properties
├── .gitignore
├── gradlew.bat
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/lib/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':lib'
2 |
--------------------------------------------------------------------------------
/lib/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | FocusLib
3 |
4 |
--------------------------------------------------------------------------------
/lib/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RyanHuen/AndroidTVFocus/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RyanHuen/AndroidTVFocus/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RyanHuen/AndroidTVFocus/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RyanHuen/AndroidTVFocus/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RyanHuen/AndroidTVFocus/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RyanHuen/AndroidTVFocus/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xhdpi/bg_light_focus.9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RyanHuen/AndroidTVFocus/HEAD/app/src/main/res/drawable-xhdpi/bg_light_focus.9.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RyanHuen/AndroidTVFocus/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RyanHuen/AndroidTVFocus/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RyanHuen/AndroidTVFocus/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RyanHuen/AndroidTVFocus/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RyanHuen/AndroidTVFocus/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/lib/src/main/res/drawable-xhdpi/focus_highlight.9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RyanHuen/AndroidTVFocus/HEAD/lib/src/main/res/drawable-xhdpi/focus_highlight.9.png
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/lib/src/main/res/values/integers.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 500
4 | 300
5 | 100
6 |
7 |
--------------------------------------------------------------------------------
/lib/src/main/res/values/ids.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Mar 19 09:35:41 CST 2018
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
7 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # AndroidTVFocus
2 | Android TV的Focus动态效果(支持滚动)
3 | # Demo APK下载
4 | [Demo APK 地址](https://github.com/RyanHuen/AndroidTVFocus/releases/download/v1.0.0/demo_apk.apk)
5 | 也可以前往本工程release处自行下载
6 | # 使用方式:
7 | 请查阅本人博客文章,地址如下:
8 | [Android TV Focus效果库使用方式](https://ryanhuen.tech/blog/article/25/)
9 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | AndroidTVFocus
3 | 普通View Focus
4 | 滚动View Focus
5 |
6 |
7 | Hello blank fragment
8 |
9 |
--------------------------------------------------------------------------------
/lib/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'com.github.dcendents.android-maven'
3 |
4 | group='com.github.ryanhuen'
5 |
6 | android {
7 | compileSdkVersion 27
8 |
9 | defaultConfig {
10 | minSdkVersion 23
11 | targetSdkVersion 27
12 | }
13 |
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 |
21 | }
--------------------------------------------------------------------------------
/lib/src/test/java/com/ryanhuen/lib/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.ryanhuen.lib;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/app/src/test/java/com/ryanhuen/androidtvfocus/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.ryanhuen.androidtvfocus;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_scrollable_view_focus.xml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
13 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/lib/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/lib/src/androidTest/java/com/ryanhuen/lib/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.ryanhuen.lib;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.ryanhuen.lib", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Built application files
2 | *.apk
3 | *.ap_
4 |
5 | # Files for the ART/Dalvik VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # Generated files
12 | bin/
13 | gen/
14 | out/
15 |
16 | # Gradle files
17 | .gradle/
18 | build/
19 |
20 | # Local configuration file (sdk path, etc)
21 | local.properties
22 |
23 | # Proguard folder generated by Eclipse
24 | proguard/
25 |
26 | # Log Files
27 | *.log
28 |
29 | # Android Studio Navigation editor temp files
30 | .navigation/
31 |
32 | # Android Studio captures folder
33 | captures/
34 |
35 | # Intellij
36 | *.iml
37 | .idea/
38 | # Keystore files
39 | *.jks
40 |
41 | # External native build folder generated in Android Studio 2.2 and later
42 | .externalNativeBuild
43 |
44 | # Google Services (e.g. APIs or Firebase)
45 | google-services.json
46 |
47 | # Freeline
48 | freeline.py
49 | freeline/
50 | freeline_project_description.json
51 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/recycler_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
14 |
15 |
23 |
24 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/ryanhuen/androidtvfocus/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.ryanhuen.androidtvfocus;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.ryanhuen.androidtvfocus", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/dialog_for_focus_demo.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
16 |
17 |
25 |
26 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 27
5 | defaultConfig {
6 | applicationId "com.ryanhuen.androidtvfocus"
7 | minSdkVersion 23
8 | targetSdkVersion 27
9 | versionCode 1
10 | versionName "1.0"
11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | dependencies {
22 | implementation fileTree(include: ['*.jar'], dir: 'libs')
23 | implementation 'com.android.support:appcompat-v7:27.1.0'
24 | testImplementation 'junit:junit:4.12'
25 | androidTestImplementation 'com.android.support.test:runner:1.0.1'
26 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
27 | implementation project(':lib')
28 | implementation 'com.android.support:design:27.1.0'
29 | }
30 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/java/com/ryanhuen/androidtvfocus/fragment/ScrollableViewFocusFragment.java:
--------------------------------------------------------------------------------
1 | package com.ryanhuen.androidtvfocus.fragment;
2 |
3 |
4 | import android.app.Fragment;
5 | import android.os.Bundle;
6 | import android.support.v7.widget.GridLayoutManager;
7 | import android.support.v7.widget.RecyclerView;
8 | import android.view.LayoutInflater;
9 | import android.view.View;
10 | import android.view.ViewGroup;
11 |
12 | import com.ryanhuen.androidtvfocus.R;
13 | import com.ryanhuen.androidtvfocus.adapter.SampleAdapter;
14 | import com.ryanhuen.lib.widget.FocusHighlightHelper;
15 |
16 | /**
17 | * A simple {@link Fragment} subclass.
18 | */
19 | public class ScrollableViewFocusFragment extends Fragment {
20 |
21 | private RecyclerView mRecyclerView;
22 |
23 | public ScrollableViewFocusFragment() {
24 | // Required empty public constructor
25 | }
26 |
27 | @Override
28 | public View onCreateView(LayoutInflater inflater, ViewGroup container,
29 | Bundle savedInstanceState) {
30 | // Inflate the layout for this fragment
31 | View view = inflater.inflate(R.layout.fragment_scrollable_view_focus, container, false);
32 | mRecyclerView = view.findViewById(R.id.recycler_view);
33 | mRecyclerView.setLayoutManager(
34 | new GridLayoutManager(getActivity(), 4,
35 | GridLayoutManager.VERTICAL, false));
36 | mRecyclerView.setAdapter(new SampleAdapter(getActivity()));
37 | mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
38 | @Override
39 | public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
40 | super.onScrollStateChanged(recyclerView, newState);
41 | if (newState != RecyclerView.SCROLL_STATE_IDLE) {
42 | FocusHighlightHelper.setMetroClipView(mRecyclerView);
43 | } else {
44 | FocusHighlightHelper.clearMetroClipView(mRecyclerView);
45 | }
46 | }
47 | });
48 | return view;
49 | }
50 |
51 | }
52 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
13 |
14 |
20 |
21 |
30 |
31 |
40 |
41 |
42 |
43 |
47 |
48 |
52 |
53 |
54 |
55 |
--------------------------------------------------------------------------------
/app/src/main/java/com/ryanhuen/androidtvfocus/adapter/SampleAdapter.java:
--------------------------------------------------------------------------------
1 | package com.ryanhuen.androidtvfocus.adapter;
2 |
3 | import android.content.Context;
4 | import android.support.annotation.NonNull;
5 | import android.support.v7.widget.RecyclerView;
6 | import android.view.LayoutInflater;
7 | import android.view.View;
8 | import android.view.ViewGroup;
9 | import android.widget.ImageView;
10 | import android.widget.TextView;
11 |
12 | import com.ryanhuen.androidtvfocus.R;
13 | import com.ryanhuen.lib.widget.FocusHighlightHelper;
14 | import com.ryanhuen.lib.widget.FocusHighlightOptions;
15 |
16 | /**
17 | * Created by ryanhuen on 18-3-19.
18 | */
19 |
20 | public class SampleAdapter extends RecyclerView.Adapter {
21 |
22 | private Context mContext;
23 |
24 | public SampleAdapter(Context context) {
25 | mContext = context;
26 | }
27 |
28 | @NonNull
29 | @Override
30 | public SampleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
31 | return new SampleViewHolder(LayoutInflater.from(mContext)
32 | .inflate(R.layout.recycler_item, parent, false));
33 | }
34 |
35 | @Override
36 | public void onBindViewHolder(@NonNull final SampleViewHolder holder, int position) {
37 | if (position % 2 == 0) {
38 | holder.itemView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
39 | @Override
40 | public void onFocusChange(View v, boolean hasFocus) {
41 | FocusHighlightHelper.focusHighlightView(holder.itemView, hasFocus, new FocusHighlightOptions());
42 | }
43 | });
44 | } else {
45 | holder.itemView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
46 | @Override
47 | public void onFocusChange(View v, boolean hasFocus) {
48 | FocusHighlightHelper.focusHighlightView(holder.itemView, hasFocus, new FocusHighlightOptions.Builder()
49 | .specifiedViewWithBorder(holder.mIcon).build());
50 | }
51 | });
52 |
53 | }
54 | }
55 |
56 | @Override
57 | public int getItemCount() {
58 | return 500;
59 | }
60 |
61 | class SampleViewHolder extends RecyclerView.ViewHolder {
62 | ImageView mIcon;
63 | TextView mTitle;
64 |
65 | public SampleViewHolder(View itemView) {
66 | super(itemView);
67 | mIcon = itemView.findViewById(R.id.icon);
68 | mTitle = itemView.findViewById(R.id.title);
69 | }
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/app/src/main/java/com/ryanhuen/androidtvfocus/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.ryanhuen.androidtvfocus;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.view.View;
6 | import android.widget.FrameLayout;
7 | import android.widget.LinearLayout;
8 | import android.widget.TextView;
9 |
10 | import com.ryanhuen.androidtvfocus.fragment.ScrollableViewFocusFragment;
11 | import com.ryanhuen.androidtvfocus.fragment.SimpleViewFocusFragment;
12 | import com.ryanhuen.lib.widget.FocusHighlightHelper;
13 | import com.ryanhuen.lib.widget.FocusHighlightOptions;
14 |
15 | public class MainActivity extends AppCompatActivity implements View.OnFocusChangeListener {
16 |
17 | private LinearLayout mSwitchMenu;
18 | private TextView mSimpleViewFocus;
19 | private TextView mScrollableViewFocus;
20 | private FrameLayout mMainContent;
21 |
22 | private ScrollableViewFocusFragment mScrollableViewFocusFragment;
23 | private SimpleViewFocusFragment mSimpleViewFocusFragment;
24 |
25 | @Override
26 | protected void onCreate(Bundle savedInstanceState) {
27 | super.onCreate(savedInstanceState);
28 | setContentView(R.layout.activity_main);
29 | initView();
30 | initFragments();
31 | }
32 |
33 | private void initFragments() {
34 | mScrollableViewFocusFragment = new ScrollableViewFocusFragment();
35 | mSimpleViewFocusFragment = new SimpleViewFocusFragment();
36 | }
37 |
38 | private void initView() {
39 | mSwitchMenu = findViewById(R.id.switch_menu);
40 | mSimpleViewFocus = findViewById(R.id.simple_view_focus);
41 | mScrollableViewFocus = findViewById(R.id.scrollable_view_focus);
42 | mMainContent = findViewById(R.id.main_content);
43 |
44 | mSimpleViewFocus.setOnFocusChangeListener(this);
45 | mScrollableViewFocus.setOnFocusChangeListener(this);
46 | }
47 |
48 | @Override
49 | public void onFocusChange(View v, boolean hasFocus) {
50 | v.setTag(R.id.shadow_focus_item_pivot_horizontal,FocusHighlightHelper.VIEW_SCALE_PIVOT_X_LEFT);
51 | FocusHighlightHelper.focusHighlightView(v, hasFocus, new FocusHighlightOptions());
52 | if (v.getId() == R.id.simple_view_focus) {
53 | //simple view focus
54 | getFragmentManager().beginTransaction().replace(R.id.main_content
55 | , mSimpleViewFocusFragment).commit();
56 | } else if (v.getId() == R.id.scrollable_view_focus) {
57 | //scrollable view focus
58 | getFragmentManager().beginTransaction().replace(R.id.main_content
59 | , mScrollableViewFocusFragment).commit();
60 | }
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_simple_view_focus.xml:
--------------------------------------------------------------------------------
1 |
8 |
9 |
17 |
18 |
26 |
27 |
35 |
36 |
44 |
45 |
52 |
53 |
60 |
61 |
67 |
68 |
69 |
76 |
77 |
78 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/ryanhuen/lib/widget/FocusHighlightOptions.java:
--------------------------------------------------------------------------------
1 | package com.ryanhuen.lib.widget;
2 |
3 | import android.graphics.drawable.Drawable;
4 | import android.view.View;
5 |
6 | public class FocusHighlightOptions {
7 | public static final class Builder {
8 | private boolean needsScale = true;
9 | private boolean needsBorder = true;
10 | private boolean needsSpecialBorder = false;
11 | private boolean needsSpecialBackground = false;
12 | private boolean needsMoveToBottom = false;
13 | private View specifiedBorderView;
14 | private Drawable specifiedBackground;
15 |
16 | /**
17 | * @param needsScale whether scale view
18 | */
19 | public Builder needsScale(boolean needsScale) {
20 | this.needsScale = needsScale;
21 | return this;
22 | }
23 |
24 | /**
25 | * @param needsBorder whether draw border
26 | */
27 | public Builder needsBorder(boolean needsBorder) {
28 | this.needsBorder = needsBorder;
29 | return this;
30 | }
31 |
32 | /**
33 | * specified an extra child view to draw border
34 | *
35 | * @param view the child view whom be draw border
36 | */
37 | public Builder specifiedViewWithBorder(View view) {
38 | this.needsSpecialBorder = true;
39 | this.specifiedBorderView = view;
40 | return this;
41 | }
42 |
43 | /**
44 | * whether specified an extra background drawable to draw
45 | *
46 | * @param specifiedBackground extra background drawable
47 | * @param needsMoveToBottom foreground or background
48 | */
49 | public Builder specifiedBackground(Drawable specifiedBackground, boolean needsMoveToBottom) {
50 | this.needsSpecialBackground = true;
51 | this.needsMoveToBottom = needsMoveToBottom;
52 | this.specifiedBackground = specifiedBackground;
53 | return this;
54 | }
55 |
56 | public FocusHighlightOptions build() {
57 | final FocusHighlightOptions handler = new FocusHighlightOptions();
58 | handler.needsScale = needsScale;
59 | handler.needsBorder = needsBorder;
60 | if (needsSpecialBorder) {
61 | handler.specifiedBorderView = specifiedBorderView;
62 | handler.needsSpecialBorder = true;
63 | }
64 | if (needsSpecialBackground) {
65 | handler.needsSpecialBackground = true;
66 | handler.needsMoveToBottom = needsMoveToBottom;
67 | handler.specifiedBackground = specifiedBackground;
68 | }
69 | return handler;
70 | }
71 | }
72 |
73 | boolean needsScale = true;
74 | boolean needsBorder = true;
75 | boolean needsSpecialBorder = false;
76 | View specifiedBorderView;
77 | boolean needsSpecialBackground = false;
78 | boolean needsMoveToBottom = false;
79 | Drawable specifiedBackground;
80 | }
81 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/ryanhuen/lib/widget/FocusHighlightHelper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2014 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 | * in compliance with the License. You may obtain a copy of the License at
6 | *
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | *
9 | * Unless required by applicable law or agreed to in writing, software distributed under the License
10 | * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 | * or implied. See the License for the specific language governing permissions and limitations under
12 | * the License.
13 | */
14 |
15 | package com.ryanhuen.lib.widget;
16 |
17 | import android.app.Activity;
18 | import android.app.Dialog;
19 | import android.util.Log;
20 | import android.view.View;
21 | import android.view.ViewGroup;
22 |
23 | import com.ryanhuen.lib.R;
24 |
25 |
26 | public class FocusHighlightHelper {
27 | public static final String TAG = FocusHighlightHelper.class.getName();
28 | public static final boolean DEBUG = false;
29 |
30 | public static final int VIEW_SCALE_PIVOT_X_LEFT = 0;
31 | public static final int VIEW_SCALE_PIVOT_X_RIGHT = 1;
32 | public static final int VIEW_SCALE_PIVOT_Y_TOP = 0;
33 | public static final int VIEW_SCALE_PIVOT_Y_BOTTOM = 1;
34 |
35 | private static Dialog sDialog;
36 |
37 | public static void focusHighlightView(View view, boolean hasFocus, FocusHighlightOptions options) {
38 | if (view.getContext() instanceof Activity) {
39 | final ShadowFocusView cursorView = getFocusCursorView(view);
40 | if (hasFocus) {
41 | cursorView.setFocusView(view, options);
42 | } else {
43 | cursorView.setUnFocusView(view);
44 | }
45 | }
46 | }
47 |
48 | public static void focusHighlightView(Dialog dialog, View view,
49 | boolean hasFocus, FocusHighlightOptions options) {
50 | sDialog = dialog;
51 | final ShadowFocusView cursorView = getFocusCursorViewFromDialogWindow(dialog);
52 | if (hasFocus) {
53 | cursorView.setFocusView(view, options);
54 | } else {
55 | cursorView.setUnFocusView(view);
56 | }
57 | }
58 |
59 | private static ShadowFocusView getFocusCursorView(View view) {
60 | ViewGroup decorView;
61 | if (view.getContext() instanceof Activity) {
62 | decorView = (ViewGroup) ((Activity) view.getContext()).getWindow().getDecorView();
63 | } else {
64 | return getFocusCursorViewFromDialogWindow(sDialog);
65 | }
66 | ShadowFocusView metroCursorView;
67 | metroCursorView = decorView.findViewById(R.id.shadow_focus_view);
68 | if (null == metroCursorView) {
69 | if (DEBUG) {
70 | Log.e(TAG, "didn't find MetroCursorView,we will create one");
71 | }
72 | metroCursorView = new ShadowFocusView(view.getContext());
73 | metroCursorView.setId(R.id.shadow_focus_view);
74 | ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
75 | ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
76 | decorView.addView(metroCursorView, layoutParams);
77 | }
78 | return metroCursorView;
79 | }
80 |
81 | private static ShadowFocusView getFocusCursorViewFromDialogWindow(Dialog dialog) {
82 | if (dialog.getWindow() != null) {
83 | ViewGroup decorView = (ViewGroup) dialog.getWindow().getDecorView();
84 | ShadowFocusView metroCursorView;
85 | metroCursorView = decorView.findViewById(R.id.shadow_focus_view);
86 | if (null == metroCursorView) {
87 | if (DEBUG) {
88 | Log.e(TAG, "didn't find MetroCursorView,we will create one");
89 | }
90 | metroCursorView = new ShadowFocusView(dialog.getContext());
91 | metroCursorView.setId(R.id.shadow_focus_view);
92 | ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
93 | ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
94 | decorView.addView(metroCursorView, layoutParams);
95 | }
96 | return metroCursorView;
97 | } else {
98 | throw new RuntimeException("The Dialog which want to add CZFocusCursorView didn't get its host Window object");
99 | }
100 | }
101 |
102 | public static void setMetroClipView(View v) {
103 | getFocusCursorView(v).setClipView(v);
104 | }
105 |
106 | public static void clearMetroClipView(View v) {
107 | getFocusCursorView(v).setClipView(null);
108 | getFocusCursorView(v).invalidate();
109 | }
110 |
111 | public static void invalidateFocusView(View view) {
112 | ShadowFocusView czFocusCursorView = getFocusCursorView(view);
113 | czFocusCursorView.invalidate();
114 | }
115 | }
116 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/app/src/main/java/com/ryanhuen/androidtvfocus/fragment/SimpleViewFocusFragment.java:
--------------------------------------------------------------------------------
1 | package com.ryanhuen.androidtvfocus.fragment;
2 |
3 |
4 | import android.app.Dialog;
5 | import android.app.Fragment;
6 | import android.os.Bundle;
7 | import android.view.LayoutInflater;
8 | import android.view.View;
9 | import android.view.ViewGroup;
10 | import android.widget.Button;
11 | import android.widget.ImageView;
12 | import android.widget.LinearLayout;
13 | import android.widget.TextView;
14 |
15 | import com.ryanhuen.androidtvfocus.R;
16 | import com.ryanhuen.lib.widget.FocusHighlightHelper;
17 | import com.ryanhuen.lib.widget.FocusHighlightOptions;
18 |
19 | /**
20 | * A simple {@link Fragment} subclass.
21 | */
22 | public class SimpleViewFocusFragment extends Fragment {
23 |
24 | private TextView mDefaultConfig;
25 | private TextView mNoScale;
26 | private TextView mScaleNoBorder;
27 | private LinearLayout mAdvanceFocus;
28 | private ImageView mTargetDrawBorder;
29 | private TextView mChangeBorder;
30 | private Button mHitDialog;
31 |
32 | public SimpleViewFocusFragment() {
33 | // Required empty public constructor
34 | }
35 |
36 |
37 | @Override
38 | public View onCreateView(LayoutInflater inflater, ViewGroup container,
39 | Bundle savedInstanceState) {
40 | // Inflate the layout for this fragment
41 | View root = inflater.inflate(R.layout.fragment_simple_view_focus, container, false);
42 | initView(root);
43 | return root;
44 | }
45 |
46 | private void initView(View root) {
47 | mDefaultConfig = root.findViewById(R.id.default_config);
48 | mNoScale = root.findViewById(R.id.no_scale);
49 | mScaleNoBorder = root.findViewById(R.id.scale_no_border);
50 | mAdvanceFocus = root.findViewById(R.id.advance_focus);
51 | mTargetDrawBorder = root.findViewById(R.id.target_draw_border);
52 | mChangeBorder = root.findViewById(R.id.change_border);
53 | mHitDialog = (Button) root.findViewById(R.id.hit_dialog);
54 |
55 | //默认效果
56 | mDefaultConfig.setOnFocusChangeListener(new View.OnFocusChangeListener() {
57 | @Override
58 | public void onFocusChange(View v, boolean hasFocus) {
59 | FocusHighlightHelper.focusHighlightView(v, hasFocus, new FocusHighlightOptions());
60 | }
61 | });
62 |
63 | mNoScale.setOnFocusChangeListener(new View.OnFocusChangeListener() {
64 | @Override
65 | public void onFocusChange(View v, boolean hasFocus) {
66 | //不放大
67 | FocusHighlightHelper.focusHighlightView(mNoScale, hasFocus, new FocusHighlightOptions
68 | .Builder()
69 | .needsScale(false)
70 | .build());
71 | }
72 | });
73 | mScaleNoBorder.setOnFocusChangeListener(new View.OnFocusChangeListener() {
74 | @Override
75 | public void onFocusChange(View v, boolean hasFocus) {
76 | //只放大,不增加边框
77 | FocusHighlightHelper.focusHighlightView(mScaleNoBorder, hasFocus, new FocusHighlightOptions
78 | .Builder()
79 | .needsBorder(false)
80 | .build());
81 | }
82 | });
83 |
84 | mChangeBorder.setOnFocusChangeListener(new View.OnFocusChangeListener() {
85 | @Override
86 | public void onFocusChange(View v, boolean hasFocus) {
87 | //修改Focus的边框样式
88 | FocusHighlightHelper.focusHighlightView(mChangeBorder, hasFocus, new FocusHighlightOptions
89 | .Builder()
90 | .specifiedBackground(getResources().getDrawable(R.drawable.bg_light_focus), true)
91 | .build());
92 | }
93 | });
94 |
95 |
96 | mAdvanceFocus.setOnFocusChangeListener(new View.OnFocusChangeListener() {
97 | @Override
98 | public void onFocusChange(View v, boolean hasFocus) {
99 | //进阶Focus效果,指定ViewGroup中的某个View绘制边框
100 | FocusHighlightHelper.focusHighlightView(mAdvanceFocus, hasFocus, new FocusHighlightOptions
101 | .Builder()
102 | .specifiedViewWithBorder(mTargetDrawBorder)
103 | .build());
104 | }
105 | });
106 |
107 | mHitDialog.setOnFocusChangeListener(new View.OnFocusChangeListener() {
108 | @Override
109 | public void onFocusChange(View v, boolean hasFocus) {
110 | FocusHighlightHelper.focusHighlightView(mHitDialog, hasFocus, new FocusHighlightOptions());
111 | }
112 | });
113 | mHitDialog.setOnClickListener(new View.OnClickListener() {
114 | @Override
115 | public void onClick(View v) {
116 | final Dialog dialog = new Dialog(getActivity(), android.R.style.Theme_Dialog);
117 | dialog.setContentView(LayoutInflater.from(getActivity())
118 | .inflate(R.layout.dialog_for_focus_demo, null));
119 |
120 | View view = dialog.getWindow().getDecorView();
121 | final TextView textView = view.findViewById(R.id.text_dialog_focus);
122 | final ImageView imageView = view.findViewById(R.id.image_dialog_focus);
123 | textView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
124 | @Override
125 | public void onFocusChange(View v, boolean hasFocus) {
126 | FocusHighlightHelper.focusHighlightView(dialog, textView, hasFocus, new FocusHighlightOptions());
127 | }
128 | });
129 | imageView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
130 | @Override
131 | public void onFocusChange(View v, boolean hasFocus) {
132 | FocusHighlightHelper.focusHighlightView(dialog, imageView, hasFocus, new FocusHighlightOptions());
133 | }
134 | });
135 | dialog.show();
136 | }
137 | });
138 | }
139 |
140 | }
141 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/ryanhuen/lib/widget/ShadowFocusView.java:
--------------------------------------------------------------------------------
1 | package com.ryanhuen.lib.widget;
2 |
3 | import android.animation.ValueAnimator;
4 | import android.content.Context;
5 | import android.graphics.Canvas;
6 | import android.graphics.Paint;
7 | import android.graphics.Rect;
8 | import android.graphics.drawable.Drawable;
9 | import android.util.AttributeSet;
10 | import android.view.View;
11 | import android.view.animation.DecelerateInterpolator;
12 |
13 | import com.ryanhuen.lib.R;
14 |
15 |
16 | public class ShadowFocusView extends View implements ValueAnimator.AnimatorUpdateListener {
17 | private View mFocusView;
18 | private View mUnFocusView;
19 | private int[] mFocusLocation = new int[2];
20 | private int[] mRealDrawBorderFocusLocation = new int[2];
21 | private int[] mLocation = new int[2];
22 | private Drawable mDrawablePaint;
23 | private Drawable mDrawableDefault;
24 | private float mScaleUp = 1.0f;
25 | private Paint mPaint = new Paint();
26 | private boolean mBorderNeeded = true;
27 | private boolean mScaleNeeded = true;
28 | private boolean mMoveHighLightToBottom = false;
29 | private View mSpecifiedBorderView;
30 |
31 | public static final float DEFAULT_VIEW_SCALE = 1.1f;
32 |
33 | ValueAnimator animScaleUp = ValueAnimator.ofFloat(1.0F, DEFAULT_VIEW_SCALE)
34 | .setDuration(getResources().getInteger(R.integer.scale_up_duration));
35 |
36 | public ShadowFocusView(Context context) {
37 | super(context);
38 | init();
39 | }
40 |
41 | public ShadowFocusView(Context context, AttributeSet attrs) {
42 | super(context, attrs);
43 | init();
44 | }
45 |
46 | public ShadowFocusView(Context context, AttributeSet attrs, int defStyleAttr) {
47 | super(context, attrs, defStyleAttr);
48 | init();
49 | }
50 |
51 | void init() {
52 | mDrawablePaint = getResources().getDrawable(R.drawable.focus_highlight);
53 | mDrawableDefault = getResources().getDrawable(R.drawable.focus_highlight);
54 | mPaint.setColor(0xff000000);
55 | animScaleUp.addUpdateListener(this);
56 | animScaleUp.setInterpolator(new DecelerateInterpolator());
57 | }
58 |
59 | @Override
60 | protected void onDraw(Canvas canvas) {
61 | drawOnShadowView(canvas, mFocusView, mScaleNeeded ? mScaleUp : 1.0f);
62 | }
63 |
64 | View mClipView = null;
65 | private int[] mClipViewLocation = new int[2];
66 |
67 | public void setClipView(View v) {
68 | mClipView = v;
69 | }
70 |
71 | private void drawOnShadowView(Canvas canvas, View view, float scale) {
72 | if (view != null) {
73 |
74 | if (null == mLocation) {
75 | mLocation = new int[2];
76 | }
77 | if (null == mFocusLocation) {
78 | mFocusLocation = new int[2];
79 | }
80 | if (null == mRealDrawBorderFocusLocation) {
81 | mRealDrawBorderFocusLocation = new int[2];
82 | }
83 | getLocationInWindow(mLocation);
84 | view.getLocationInWindow(mFocusLocation);
85 |
86 | int width = view.getWidth();
87 | int height = view.getHeight();
88 |
89 | int offsetW = (int) (width * (scale - 1) / 2);
90 | int offsetH = (int) (height * (scale - 1) / 2);
91 |
92 | clipCanvasRect(canvas, offsetW, offsetH);
93 |
94 | Rect padding = new Rect();
95 | mDrawablePaint.getPadding(padding);
96 |
97 | int left = mFocusLocation[0] - mLocation[0] - offsetW;
98 | int top = mFocusLocation[1] - mLocation[1] - offsetH;
99 |
100 | if (null == mSpecifiedBorderView) {
101 | canvas.save();
102 | if (mMoveHighLightToBottom) {
103 | //border be background
104 | caculateDrawConfig(canvas, view, scale, offsetW, offsetH, padding, left, top);
105 | if (mBorderNeeded) {
106 | drawWholeViewBorder(canvas, width, height, padding);
107 | }
108 | view.draw(canvas);
109 | } else {
110 | caculateDrawConfig(canvas, view, scale, offsetW, offsetH, padding, left, top);
111 | view.draw(canvas);
112 | if (mBorderNeeded) {
113 | drawWholeViewBorder(canvas, width, height, padding);
114 | }
115 | }
116 | canvas.restore();
117 | } else {
118 | //border be foreground
119 | if (mMoveHighLightToBottom) {
120 | canvas.save();
121 | drawSpecifiedDrawable(canvas, scale, padding, left, top);
122 | canvas.restore();
123 |
124 | canvas.save();
125 | caculateDrawConfig(canvas, view, scale, offsetW, offsetH, padding, left, top);
126 | view.draw(canvas);
127 | canvas.restore();
128 | } else {
129 | canvas.save();
130 | caculateDrawConfig(canvas, view, scale, offsetW, offsetH, padding, left, top);
131 | view.draw(canvas);
132 | canvas.restore();
133 |
134 | canvas.save();
135 | drawSpecifiedDrawable(canvas, scale, padding, left, top);
136 | canvas.restore();
137 | }
138 | }
139 | }
140 | }
141 |
142 | private void drawWholeViewBorder(Canvas canvas, int width, int height, Rect padding) {
143 | mDrawablePaint.setBounds(-padding.left, -padding.top,
144 | width + padding.right,
145 | height + padding.bottom);
146 | mDrawablePaint.draw(canvas);
147 | }
148 |
149 | private void caculateDrawConfig(Canvas canvas, View view, float scale, int offsetW, int offsetH, Rect padding, int left, int top) {
150 | canvas.translate(left, top);
151 | float canvasPivotX = whetherViewSetPivot(view, R.id.shadow_focus_item_pivot_horizontal, padding, scale);
152 | float canvasPivotY = whetherViewSetPivot(view, R.id.shadow_focus_item_pivot_vertical, padding, scale);
153 | canvas.translate(canvasPivotX, canvasPivotY);
154 | canvas.scale(scale, scale);
155 | }
156 |
157 | /**
158 | * whether should clip canvas(every time restore the canvas,the clip params will be reset)
159 | *
160 | * @param canvas canvas
161 | * @param offsetW horizontal scale offset
162 | * @param offsetH vertical scale offset
163 | */
164 | private void clipCanvasRect(Canvas canvas, int offsetW, int offsetH) {
165 | if (mClipView != null) {
166 | mClipView.getLocationInWindow(mClipViewLocation);
167 | canvas.clipRect(mClipViewLocation[0] - offsetW, mClipViewLocation[1] - offsetH,
168 | mClipViewLocation[0] + mClipView.getWidth() + offsetW,
169 | mClipViewLocation[1] + mClipView.getHeight() + offsetH);
170 | }
171 | }
172 |
173 | private void drawSpecifiedDrawable(Canvas canvas, float scale, Rect padding, int left, int top) {
174 | mSpecifiedBorderView.getLocationInWindow(mRealDrawBorderFocusLocation);
175 | int toParentOffsetLeft = (int) ((mRealDrawBorderFocusLocation[0] - mFocusLocation[0]) * scale);
176 | int toParentOffsetTop = (int) ((mRealDrawBorderFocusLocation[1] - mFocusLocation[1]) * scale);
177 | int borderLeft = left + toParentOffsetLeft;
178 | int borderTop = top + toParentOffsetTop;
179 | canvas.translate(borderLeft, borderTop);
180 | mDrawablePaint.setBounds(-padding.left, -padding.top,
181 | mSpecifiedBorderView.getWidth() + padding.right,
182 | mSpecifiedBorderView.getHeight() + padding.bottom);
183 | float scalePivotX = whetherViewSetPivot(mSpecifiedBorderView, R.id.shadow_focus_item_pivot_horizontal, padding, scale);
184 | float scalePivotY = whetherViewSetPivot(mSpecifiedBorderView, R.id.shadow_focus_item_pivot_vertical, padding, scale);
185 | canvas.scale(scale, scale, scalePivotX, scalePivotY);
186 | mDrawablePaint.draw(canvas);
187 | }
188 |
189 | private float whetherViewSetPivot(View view, int key, Rect padding, float scale) {
190 | if (null == view.getTag(key)) {
191 | return 0f;
192 | }
193 | if (FocusHighlightHelper.VIEW_SCALE_PIVOT_X_LEFT == (int) view.getTag(key)) {
194 | return (view.getWidth()) * (scale - 1) / 2 + padding.left * scale;
195 | } else if (FocusHighlightHelper.VIEW_SCALE_PIVOT_X_RIGHT == (int) view.getTag(key)) {
196 | return -((view.getWidth()) * (scale - 1) / 2 + padding.right * scale);
197 | } else if (FocusHighlightHelper.VIEW_SCALE_PIVOT_Y_TOP == (int) view.getTag(key)) {
198 | return -(view.getHeight() * (scale - 1) / 2 + padding.top * scale);
199 | } else if (FocusHighlightHelper.VIEW_SCALE_PIVOT_Y_BOTTOM == (int) view.getTag(key)) {
200 | return view.getHeight() * (scale - 1) / 2 + padding.bottom * scale;
201 | } else {
202 | return 0f;
203 | }
204 | }
205 |
206 | public void setFocusView(View view, FocusHighlightOptions focusHighlightOptions) {
207 | if (focusHighlightOptions.needsSpecialBorder) {
208 | mSpecifiedBorderView = focusHighlightOptions.specifiedBorderView;
209 | } else {
210 | mBorderNeeded = focusHighlightOptions.needsBorder;
211 | }
212 | if (focusHighlightOptions.needsSpecialBackground) {
213 | mDrawablePaint = focusHighlightOptions.specifiedBackground;
214 | mMoveHighLightToBottom = focusHighlightOptions.needsMoveToBottom;
215 | mBorderNeeded = true;
216 | }
217 | mScaleNeeded = focusHighlightOptions.needsScale;
218 | view.setAlpha(0f);
219 | if (mFocusView != view) {
220 | mFocusView = view;
221 | mScaleUp = 1.0f;
222 | animScaleUp.start();
223 | }
224 | invalidate();
225 | }
226 |
227 | public void setUnFocusView(final View view) {
228 | mFocusView = null;
229 | view.setAlpha(1f);
230 | mBorderNeeded = true;
231 | mScaleNeeded = true;
232 | mMoveHighLightToBottom = false;
233 | mSpecifiedBorderView = null;
234 | mDrawablePaint = mDrawableDefault;
235 | if (mUnFocusView != view) {
236 | mUnFocusView = view;
237 | }
238 | invalidate();
239 | }
240 |
241 | @Override
242 | public void onAnimationUpdate(ValueAnimator animation) {
243 | mScaleUp = (float) animation.getAnimatedValue();
244 | invalidate();
245 | }
246 | }
247 |
--------------------------------------------------------------------------------