android xml 焦点,android TV 焦点选中放大效果

使用github开源项目(非常感谢作者): 这里是项目地址:https://github.com/evilbinary/TvWidget

1.个人只需要RelativeLayout 布局,所以只是用了里边的TvZorderRelativeLayout

将TvZorderRelativeLayout.class复制到项目中

public class TvZorderRelativeLayout extends RelativeLayout {

private int position = 0;

public TvZorderRelativeLayout(Context context) {

super(context);

}

public TvZorderRelativeLayout(Context context, AttributeSet attrs) {

super(context, attrs);

this.setChildrenDrawingOrderEnabled(true);

}

public void setCurrentPosition(int pos) {

this.position = pos;

}

@Override

public boolean dispatchKeyEvent(KeyEvent event) {

View focused = findFocus();

int pos = indexOfChild(focused);

if (pos >= 0 && pos < getChildCount()) {

setCurrentPosition(pos);

postInvalidate();

}

return super.dispatchKeyEvent(event);

}

@Override

protected int getChildDrawingOrder(int childCount, int i) {

View v = getFocusedChild();

int pos = indexOfChild(v);

if (pos >= 0 && pos < childCount)

setCurrentPosition(pos);

if (i == childCount - 1) {//这是最后一个需要刷新的item

return position;

}

if (i == position) {//这是原本要在最后一个刷新的item

return childCount - 1;

}

return i;//正常次序的item

}

}

这个类主要是修改了ViewGroup 的绘制顺序,解决放大布局不被后边的布局遮挡 参看:https://blog.youkuaiyun.com/liweicai137/article/details/50826596

protected int getChildDrawingOrder (int childCount, int i)

返回迭代的绘制子类索引。如果你想改变子类的绘制顺序就要重写该方法。默认返回 i 值。

提示:为了能够调用该方法,你必须首先调用setChildrenDrawingOrderEnabled(boolean)来允许子类排序。

在调用draw()方法时,将会调用getChildDrawingOrder(int childCount ,int i)方法 ViewGroup 中默认是从上绘制到下,如果有需要改变绘制的 先后的顺序,就可以从写改方法(不改变位置)

2.还有两个类 BorderView,BorderEffect 复制到项目中,这两个类主要是 边框view移动放大动画还有宽高计算等

3.使用:

1.activity_main.xml ---------TvZorderRelativeLayout作为父布局

android:id="@+id/main"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:clipChildren="false" //用来定义他的子控件是否要在他应有的边界内进行绘制。 默认情况下,clipChild被设置为true

android:clipToPadding="false" //用来定义ViewGroup是否允许在padding中绘制。默认情况下,cliptopadding被设置为ture

>

android:id="@+id/test1_rl"

android:layout_width="260dp"

android:layout_height="match_parent"

android:background="@color/color1"

android:clickable="true"

android:focusable="true"

>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:text="模块1"

android:textSize="36sp"

android:textColor="@color/white"

/>

android:id="@+id/test2_rl1"

android:layout_width="200dp"

android:layout_height="140dp"

android:layout_toRightOf="@+id/mirrcast_rl"

android:background="@color/color2"

android:layout_margin="2dp"

android:clickable="true"

android:focusable="true"

>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:text="模块2"

android:textSize="36sp"

android:textColor="@color/white"

/>

...

MainActivity

public class MainActivity extends Activity implements View.OnClickListener{

RelativeLayout main;

RelativeLayout test1_rl;

RelativeLayout test2_rl1;

...

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

BorderView border = new BorderView(this);

border.setBackgroundResource(R.drawable.border_highlight);//9.png高亮框

main = (RelativeLayout) findViewById(R.id.main);

test1_rl= findViewById(R.id.test1_rl);

test2_rl1 = findViewById(R.id.test2_rl1);

...

test1_rl.setOnClickListener(this);

test2_rl1 .setOnClickListener(this);

...

border.attachTo(main);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值