Android TV 맞춤형 포커스 컨트롤
15511 단어 android 개발 기술 시리즈
public class FocusImageView extends ImageButton
{
private int defaultImageResources = -1;
private int focusImageResources = -1;
public FocusImageView(Context context)
{
super(context);
}
public FocusImageView(Context context, AttributeSet attrs)
{
this(context, attrs, 0);
}
public FocusImageView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
setAttributeSet(attrs);
}
@Override
protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect)
{
super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
if (gainFocus)
{
if (focusImageResources != -1)
{
setBackgroundResource(focusImageResources);
}
}
else
{
if (defaultImageResources != -1)
{
setBackgroundResource(defaultImageResources);
}
}
}
private void setAttributeSet(AttributeSet attrs)
{
if (attrs != null)
{
TypedArray typeArray = getContext().obtainStyledAttributes(attrs, R.styleable.FocusImageView);
defaultImageResources = typeArray.getResourceId(R.styleable.FocusImageView_defaultImageResources, -1);
focusImageResources = typeArray.getResourceId(R.styleable.FocusImageView_focusImageResources, -1);
if (defaultImageResources != -1)
{
setBackgroundResource(defaultImageResources);
}
}
}
}
attrs.xml
<resources>
<declare-styleable name="FocusImageView">
<attr name="defaultImageResources" format="integer" />
<attr name="focusImageResources" format="integer" />
declare-styleable>
resources>
레이아웃 파일 참조:
.widget.focusview.FocusImageView
android:id="@+id/iv_wulianwang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:defaultImageResources="@mipmap/wulianwangdefault"
app:focusImageResources="@mipmap/wulianwangselected">
.widget.focusview.FocusImageView>
물론 초점을 가져오거나 초점을 잃으면 그림을 전환하면selector를 정의할 수 있고drawable에서selector를 정의할 수 있습니다
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/bingliselected" android:state_focused="true" />
<item android:drawable="@mipmap/binglidefault" />
selector>
그리고 레이아웃에서 호출
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:focusable="true"
android:background="@drawable/focus_selector">
2. 내부 컨트롤이 초점을 얻고 확대, 초점을 잃고 내부 컨트롤을 줄일 수 있는 레이아웃
public class FocusRelativeLayout extends RelativeLayout {
private Animation scaleSmallAnimation;
private Animation scaleBigAnimation;
public FocusRelativeLayout(Context context) {
super(context);
}
public FocusRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FocusRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
if (gainFocus) {
zoomOut();
} else {
zoomIn();
}
}
private void zoomIn() {
if (scaleSmallAnimation == null) {
scaleSmallAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.anim_scale_small);
}
startAnimation(scaleSmallAnimation);
}
private void zoomOut() {
if (scaleBigAnimation == null) {
scaleBigAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.anim_scale_big);
}
startAnimation(scaleBigAnimation);
}
}
anim_scale_big.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:shareInterpolator="false">
<scale
android:duration="350"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50.0%"
android:pivotY="50.0%"
android:repeatCount="0"
android:toXScale="1.08"
android:toYScale="1.08" />
set>
anim_scale_small.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:shareInterpolator="false">
<scale
android:duration="350"
android:fromXScale="1.08"
android:fromYScale="1.08"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50.0%"
android:pivotY="50.0%"
android:repeatCount="0"
android:toXScale="1.0"
android:toYScale="1.0" />
set>
레이아웃 파일 참조:
.view.FocusRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#424242"
android:clipChildren="true"
android:focusable="true">
"100dp"
android:layout_height="100dp"
android:scaleType="fitXY"
android:src="@drawable/pic2"
android:layout_margin="5dp"/>
.view.FocusRelativeLayout>
3. 인터페이스에 들어가서 어떤 컨트롤러가 수요에 따라 초점을 잡게 하고 인터페이스에 들어갈 수 있다. 먼저 어떤 컨트롤러를 선택하면 레이아웃 파일에서 사용할 수 있다
.widget.focusview.FocusImageView
android:id="@+id/iv_bingli"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:defaultImageResources="@mipmap/binglidefault"
app:focusImageResources="@mipmap/bingliselected"
>
.widget.focusview.FocusImageView>