摘要:本文将带你了解Android应用开发Android_Develop:如何将自定义View里的属性实时传递出来,希望本文对大家学Android有所帮助。
本文将带你了解Android应用开发Android_Develop:如何将自定义View里的属性实时传递出来,希望本文对大家学Android有所帮助。
【Android_Develop】将自定义View里的属性实时传递出来
1、在你的自定义View里创建一个接口。
public interface GetRGBListener{
void onRGBChanged(int red,int green,int blue);
}
2、类成员变量里声明一个这个接口的引用。
GetRGBListener getRGBListener;
3、写一个方法获取并持有Activity实现的接口的实例
public void setRGBColor(GetRGBListener getRGBListener){
this.getRGBListener = getRGBListener;
}
4、在Activity里实现这个接口,可以看到,这里使用了自定义View的RGB值
/**
* 圆环控件接口实现,获取圆环中的数据,并设置背景实时改变
*/
private RotateCircle.GetRGBListener getRGBListener = new RotateCircle.GetRGBListener() {
@Override
public void onRGBChanged(int red, int green, int blue) {
getCircleRGB(selected,red,green,blue);
setBgColor(red,green,blue,bg);
}
};
5、Activity里绑定XML里的自定义View属性,并向XML创建的自定义View对象传递Activity实现的接口对象。
@Bind(R.id.island_atmo_circle)
RotateCircle rotateCircle;
@Override
protected void initView() {
super.initView();
rotateCircle.setRGBColor(getRGBListener);
}
这样,你就可以实时获取自定义View的属性值,自定义View完整代码如下:
public class RotateCircle extends AppCompatImageView {
public RotateCircle(Context context) {
super(context);
init();
}
public RotateCircle(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public RotateCircle(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
paint = new Paint();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
invalidate();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN://手指按下
return true;
case MotionEvent.ACTION_MOVE://手指移动
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);
getRGBListener.onRGBChanged(r,g,b);
invalidate();
}
case MotionEvent.ACTION_UP://手指离开
break;
default:
break;
}
return super.onTouchEvent(event);
}
public interface GetRGBListener{
void onRGBChanged(int red,int green,int blue);
}
public void setRGBColor(GetRGBListener getRGBListener){
this.getRGBListener = getRGBListener;
}
}
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号