仅针对点击后执行里面的onDraw(在code) [英] Execute the code inside onDraw() only after the click in view

查看:239
本文介绍了仅针对点击后执行里面的onDraw(在code)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个扩展视图类。
这个类被称为XML来构建我的看法。现在的观点调用的onDraw自动加载上OnDraw函数。但如何我能做些什么就做的onDraw()函数后,才单击该视图?最后,我只需要针对点击后执行里面的onDraw(在code)。

I have a class that extends a view. This class is called in xml to build my view. Now the view call the onDraw automatically the onDraw function on loaded. But how i can do what is do on onDraw() function only after i click in that view?? In conclusion i need to execute the code inside onDraw() only after the click in view.

DrawView.java:

DrawView.java:

public class DrawView extends View {
    Paint mPaint = new Paint();
    Context context;

    public DrawView(Context context, AttributeSet attrs){
        super(context, attrs);
        this.context = context;
        // setWillNotDraw(true);

    }

    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);
        mPaint.setColor(Color.MAGENTA);
        canvas.drawRect((float) (getWidth()*0.3), (float) (getHeight()*0.3), getWidth(), getHeight(), mPaint);

main.xml中:

main.xml:

<LinearLayout
      android:orientation="vertical"
      android:layout_height="fill_parent"
      android:layout_width="0dip"
      android:layout_weight="1">    
      <com.example.sliding.DrawView 
            android:id="@+id/tv_listRow_item1"
            android:tag="tv_listRow_item1_1"
            android:layout_height="0dip"
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:width="100dip"
            android:height="30dip"
            android:background="@drawable/textview_listrow_border"/>
</LinearLayout>

main.java:

main.java:

 ((DrawView)v.findViewById(R.id.tv_listRow_item1)).setOnClickListener(listener);
  private View.OnClickListener listener = new OnClickListener() {

    @Override
    public void onClick(View v) {
    }
}

有什么建议?感谢您的时间和精力。

Any suggestions? Thanks for your time and attention.

推荐答案

尝试重写

    public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// your actions here
}
}

然后调用无效()函数,它会设置一个标志,打电话给你的视图的onDraw()。

then call invalidate() function, which will set a flag to call the onDraw() of your view.

这篇关于仅针对点击后执行里面的onDraw(在code)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆