0

我有一个扩展视图的类。这个类在 xml 中被调用来构建我的视图。现在视图在加载时自动调用 onDraw 函数。但是只有在我单击该视图后,我才能在 onDraw() 函数上做什么?总之,我只需要在单击视图后执行 onDraw() 中的代码。

绘图视图.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);

主.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>

主.java:

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

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

有什么建议么?感谢您的时间和关注。

4

1 回答 1

0

尝试覆盖

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

然后调用 invalidate() 函数,该函数将设置一个标志来调用视图的 onDraw()。

于 2013-01-31T15:10:24.373 回答