点击自定义framelayout类android中的drawable的监听器

时间:2013-07-10 11:08:19

标签: android drawable android-canvas android-framelayout

嗨我正在开发Android应用程序,其中我使用一个自定义框架布局类。在那个班级里面,我正在使用一个drawable,并在画布的帮助下绘制它。我是按照以下方式做到的:

public class BackgroundContainer extends FrameLayout implements OnTouchListener{

    Drawable mShadowedBackground;

    public BackgroundContainer(Context context) {
    super(context);
    init();
    }

    public BackgroundContainer(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
    }

    public BackgroundContainer(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
    }

    private void init() {
    mShadowedBackground =
            getContext().getResources().getDrawable(R.drawable.actionbar);

    }

    @Override  
    public boolean onTouchEvent(MotionEvent event)  
    {  
        Log.i("OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO", "OOOOOOOOOOOOOOOOOOOOOO");
       switch(event.getAction())  
       {  

        case MotionEvent.ACTION_DOWN: {  

            invalidate();  
        }  
       }  
       return true;  
    }  

    @Override
    protected void onDraw(Canvas canvas) {

        mShadowedBackground.setBounds(getWidth()-150, 0, getWidth(), mOpenAreaHeight);
        canvas.save();
        canvas.translate(0, mOpenAreaTop);
        mShadowedBackground.draw(canvas);
        canvas.restore();
    }
    }

}  

现在我想听点击甚至我的drawable。我实现ontouch事件,但它无法正常工作。我是以错误的方式做的。需要帮助,谢谢。

2 个答案:

答案 0 :(得分:1)

Drawables不可点击,因为它不被视为视图。

答案 1 :(得分:0)

就像deepak已经说过的那样:通过实现相应的监听器,您只需提供特定事件应该发生的行为。你仍然需要添加监听器:)在你的情况下,这将有所帮助(在你的init()):

setOnTouchListener(this);