github源碼地址 https://github.com/xiangfenr/TVAmplification
放大效果如圖
一.新建TV項(xiàng)目,或者別的項(xiàng)目添加TV識(shí)別也行
<!--AndroidManifest.xml 中標(biāo)記為電視應(yīng)用 -->
<uses-feature
android:name="android.hardware.type.television"
android:required="true"></uses-feature>
<uses-feature
android:name="android.hardware.touchscreen"
android:required="false" />
<uses-feature
android:name="android.software.leanback"
android:required="true" />
二.添加drawable 焦點(diǎn)選中樣式
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--是否取得焦點(diǎn)泞辐,比如用戶選擇了一個(gè)文本框。-->
<item android:drawable="@mipmap/imgchecked" android:state_focused="true" />
<item android:drawable="@mipmap/imgdefault" android:state_focused="false" />
</selector>
三. xml樣式設(shè)置
<ImageView
android:id="@+id/imageBtn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="30dp"
android:focusable="true"
android:src="@drawable/banner1_clicked"
android:tag="imageBtn1" />
四. java代碼編寫,添加Focus焦點(diǎn)監(jiān)聽,利用ImageView的 setScaleX,setScaleY設(shè)置放大
也可以自己編寫一個(gè)屬性動(dòng)畫
imageView.setOnFocusChangeListener((view, b) -> {
Log.e("xf", "initEvent: " + view.getTag().toString() + " focus = " + b);
if (b) {
view.setScaleX(1.4f);
view.setScaleY(1.4f);
imageView.bringToFront(); //此屬性是將view添加到最上層
} else {
view.setScaleX(1.0f);
view.setScaleY(1.0f);
}
});