[Android]【安卓】将自定义View里的属性实时传递出来

[Android]【安卓】 将自定义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;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值