roundimageview根据focuschangeListener实现不同的效果显示
时间: 2024-05-11 17:13:48 浏览: 134
RoundImageView可以通过focuschangeListener实现不同的效果显示,具体实现方法如下:
1.在RoundImageView类中添加一个focuschangeListener的接口
```
public interface OnFocusChangeListener {
void onFocusChanged(RoundImageView view, boolean hasFocus);
}
```
2.在RoundImageView类中添加setOnFocusChangeListener方法,用来设置focuschangeListener
```
public void setOnFocusChangeListener(OnFocusChangeListener listener) {
mOnFocusChangeListener = listener;
}
```
3.在RoundImageView类中重写onFocusChanged方法,当焦点改变时,调用focuschangeListener的onFocusChanged方法,实现不同的效果显示
```
@Override
public void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
if (mOnFocusChangeListener != null) {
mOnFocusChangeListener.onFocusChanged(this, focused);
}
if (focused) {
//实现获得焦点时的效果
} else {
//实现失去焦点时的效果
}
}
```
4.在使用RoundImageView的地方,设置focuschangeListener,实现不同的效果显示
```
RoundImageView roundImageView = findViewById(R.id.round_image_view);
roundImageView.setOnFocusChangeListener(new RoundImageView.OnFocusChangeListener() {
@Override
public void onFocusChanged(RoundImageView view, boolean hasFocus) {
if (hasFocus) {
//实现获得焦点时的效果
} else {
//实现失去焦点时的效果
}
}
});
```
阅读全文