2015-01-16 57 views
1

我正在開發一個繪圖板應用程序。如何在運行時更改爲自定義視圖中的參數?

public class DrawView extends View implements OnTouchListener { 

    private Paint bmPaint = new Paint(); 
    private Paint drawPaint = new Paint(); 
    private Path path = new Path(); 
    private Canvas cv = null; 
    private Bitmap bm = null; 
    private Drawable d; 
    private boolean firstTimeThru = true; 

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

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

    public void init(Context ctx) { 
     setFocusable(true); 
     setFocusableInTouchMode(true); 
     this.setOnTouchListener(this); 
    } 

    @Override 
    public void onDraw(Canvas canvas) { 
     // Set everything up the first time anything gets drawn: 
     if (firstTimeThru) { 
      firstTimeThru = false; 
      //d = getResources().getDrawable(R.drawable.zone0_over); 
      d = new ColorDrawable(Color.TRANSPARENT); 

      // Just quickly fill the view with a red mask:   
      canvas.drawColor(Color.TRANSPARENT); 

      // Create a new bitmap and canvas and fill it with a red mask: 
      bm = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(),Config.ARGB_8888);   
      cv = new Canvas(); 
      cv.setBitmap(bm); 
      d.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); 
      d.draw(cv); 

      // Specify that painting will be with fat strokes: 
      drawPaint.setStyle(Paint.Style.STROKE); 
      drawPaint.setColor(Color.RED); 
      drawPaint.setStrokeWidth(canvas.getWidth()/200); // default 15 

      // Specify that painting will clear the pixels instead of paining new ones: 
      drawPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.ADD)); 
     } 

     cv.drawPath(path, drawPaint); 
     canvas.drawBitmap(bm, 0, 0, bmPaint); 

     super.onDraw(canvas); 
    } 

    public boolean onTouch(View view, MotionEvent event) { 
     float xPos = event.getX(); 
     float yPos = event.getY(); 

     switch (event.getAction()) { 
      // Set the starting position of a new line: 
      case MotionEvent.ACTION_DOWN: 
       path.moveTo(xPos, yPos); 
       invalidate(); 
      return true; 

      // Draw a line to the ending position: 
      case MotionEvent.ACTION_MOVE: 
       path.lineTo(xPos, yPos); 
       invalidate(); 
      break; 

      case MotionEvent.ACTION_UP: 
      break; 

      default: 
      return false; 
     } 

     return true; 
    } 

} 

和XML這樣的:我已經通過這樣的自定義視圖完成繪製函數

<ImageView 
    android:id="@+id/change_color_btn" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

<com.myapp.tool.DrawView 
    android:id="@+id/draw" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

注意,我已經設置了線的大小和OnDraw函數裏面的線條顏色,它目前是紅色的,但是,我想按下按鈕並將顏色更改爲黃色,如何在運行時設置參數?

感謝

回答

2

一個成員變量和setter函數添加到drawView函數類:

private int mLineColor = Color.RED; 
public void setLineColor(int color) 
{ 
    mLineColor = color; 
    drawPaint.setColor(mLineColor); 
} 

更改您的onDraw使用它:

drawPaint.setColor(mLineColor); 

然後你可以從使用自定義類的活動設置,在Button onClick()處理程序中:

DrawView drawview = (DrawView)findViewById(R.id.draw); 
drawView.setLineColor(Color.YELLOW); 
drawView.invalidate(); // trigger a redraw 

對筆畫寬度也是這樣。

1

將按鈕添加到您的佈局切換顏色和clicklistener添加到它。然後,您可以更改路徑的顏色。你想讓當前路徑切換顏色還是僅切換新顏色?

0

採取類似mColor自定義視圖field值和字段值創建setter and getter,然後設置顏色創建自定義視圖也是這樣後:

private int mColor; 

public int getmColor() { 
    return mColor; 
} 

public void setmColor(int mColor) { 
    this.mColor = mColor; 
    this.invalidate(); 
} 
+0

您需要設置顏色爲用新顏色重繪。否則,不會發生變化。 –

相關問題