2011-04-05 97 views
0

我在我的窗口小部件中重新制作畫布時遇到問題。我創建自定義小部件,並且想要在點擊時重新繪製第一個單元格。 有INS活動無法刷新畫布 - 通過畫圖但不會更改

public class TestView extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     ElementView ev = (ElementView)findViewById(R.id.surface2); 
     ev.setOnClickListener(evOnClick); 

    } 

    public OnClickListener evOnClick = new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      ElementView temp=(ElementView)v; 
      temp.paint.setColor(Color.RED); 
      temp.canvas.drawRect(new Rect(0,0,90,90),temp.paint); 
      temp.postInvalidate(); 
     } 
     }; 
} 

且有ElementView(窗口小部件,我要重繪)

public class ElementView extends View { 
    private final int width=100; 
    private final int height=100; 
    public Paint paint=null; 
    public Canvas canvas=null; 

    public ElementView(Context context) { 
     super(context); 
     paint = new Paint(); 
    } 

    public ElementView(Context context, AttributeSet attr) { 
     super(context, attr); 
     paint = new Paint(); 
    } 


    public ElementView(Context context, AttributeSet attr, int defaultStyles) { 
     super(context, attr, defaultStyles); 
     paint = new Paint(); 
    } 

    @Override 
    protected void onMeasure(int widthSpec, int heightSpec) { 
     int measuredWidth = MeasureSpec.getSize(widthSpec); 
     int measuredHeight = MeasureSpec.getSize(heightSpec); 
     setMeasuredDimension(this.width,this.height); 

    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     this.canvas=canvas; 
     // get the size of your control based on last call to onMeasure 
     int height = getMeasuredHeight(); 
     int width = getMeasuredWidth(); 

     // Now create a paint brush to draw your widget 

     paint.setColor(Color.GREEN); 

     //define border 
     this.canvas.drawLine(0, 0, 0, 99, paint); 
     this.canvas.drawLine(0, 0, 99,0, paint); 
     this.canvas.drawLine(99, 0, 99, 99, paint); 
     this.canvas.drawLine(0, 99, 99,99, paint); 

     //define cells 
     this.canvas.drawLine(0,50,99,50,paint); 
     this.canvas.drawLine(30,0,30,50,paint); 

     //draw green rectangle 
     this.canvas.drawRect(new Rect(0,0,50,50),paint); 
     //draw some text 
     paint.setTextSize(8); 
     paint.setColor(Color.RED); 
     String displayText = "test"; 


     Float textWidth = paint.measureText(displayText); 

     int px = width/2; 
     int py = height/2; 
     this.canvas.drawText(displayText, px - textWidth/2, py, paint); 
    } 

    @Override 
    public boolean dispatchTouchEvent(MotionEvent event) { 
     paint.setColor(Color.RED); 
     //change color of first cell 
     canvas.drawRect(new Rect(0,0,50,50),paint); 
     return super.dispatchTouchEvent(event); 
    } 

}

當我點擊ElementView它在的onClick活動進入並通過代碼沒有錯誤或異常,但不會改變視圖。有人可以告訴我哪裏錯了嗎?

+1

如果你有答案..如果它工作,請在你的問題部分添加這段代碼作爲「編輯」...以便其他人也可以學習 – rahul 2011-05-18 11:22:01

回答

1

我不認爲寫入以外的onDraw方法會對屏幕產生任何影響。

嘗試在您的onClick方法中設置一個標誌,您在onDraw中讀取的方法將觸發您的繪圖程序。

+0

你是什麼意思設置一個標誌..我相信視圖中的onDraw方法被一些調用所覆蓋...可以從外部顯式調用嗎? – rahul 2011-05-18 11:19:31