2014-05-09 111 views
1

我也要用一些按鈕構建一個繪畫應用程序。現在我想添加一些按鈕。我的代碼如下。如何將按鈕添加到自定義視圖?

package com.example.drawing; 

    import android.os.Bundle; 
    import android.app.Activity; 
    import android.view.Menu; 


    public class TouchActivity extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(new SingleTouchEventView(this, null)); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_touch, menu); 
     return true; 
    } 
    } 


package com.example.drawing; 

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Path; 
import android.util.AttributeSet; 
import android.view.MotionEvent; 
import android.view.View; 

public class SingleTouchEventView extends View { 
    private Paint paint = new Paint(); 
    private Path path = new Path(); 

    public SingleTouchEventView(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     paint.setAntiAlias(true); 
     paint.setStrokeWidth(6f); 
     paint.setColor(Color.RED); 
     paint.setStyle(Paint.Style.STROKE); 
     paint.setStrokeJoin(Paint.Join.ROUND); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     canvas.drawPath(path, paint); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     float eventX = event.getX(); 
     float eventY = event.getY(); 

     switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
      path.moveTo(eventX, eventY); 
      return true; 
     case MotionEvent.ACTION_MOVE: 
      path.lineTo(eventX, eventY); 
      break; 
     case MotionEvent.ACTION_UP: 
      // nothing to do 
      break; 
     default: 
      return false; 
     } 

     // Schedules a repaint. 
     invalidate(); 
     return true; 
    } 
} 

我該如何爲此添加一個按鈕?在一些網站和一些問題的答案在Stackoverflow(link 1link 2),但無法找到答案。

+0

你能不能放在一個RelativeLayout的是自定義視圖,並添加按鈕有? – daentech

回答

0

創建一個類GraphicsActivity,如:

class GraphicsActivity extends Activity { 

private static final boolean TEST_PICTURE = true; 
int f=0; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 


} 



@Override 
public void setContentView(View view) { 
    if (TEST_PICTURE) { 

     if(f==0){ 
     ViewGroup vg = new PictureLayout(this); 
     vg.addView(view); 

     LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View vi = inflater.inflate(R.layout.activity_main, vg); 
     view = vg; 
     } 

    } 



    super.setContentView(view); 
} 



/*@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    // TODO Auto-generated method stub 
    super.onConfigurationChanged(newConfig); 
    if(newConfig.orientation==Configuration.ORIENTATION_PORTRAIT){ 
     f=0; 
     }else{ 
     f=1; 


     } 

}*/ 

}

創建activity_main並在其上添加必要的按鈕。 以下是您PictureLayout.java

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Picture; 
import android.graphics.Rect; 
import android.graphics.drawable.Drawable; 
import android.util.AttributeSet; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.ViewParent; 

class PictureLayout extends ViewGroup { 
    private final Picture mPicture = new Picture(); 

    public PictureLayout(Context context) { 
     super(context); 
    } 

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

    @Override 
    public void addView(View child) { 
     if (getChildCount() > 1) { 
      throw new IllegalStateException(
        "PictureLayout can host only one direct child"); 
     } 

     super.addView(child); 
    } 

    @Override 
    public void addView(View child, int index) { 
     if (getChildCount() > 1) { 
      throw new IllegalStateException(
        "PictureLayout can host only one direct child"); 
     } 

     super.addView(child, index); 
    } 

    @Override 
    public void addView(View child, LayoutParams params) { 
     if (getChildCount() > 1) { 
      throw new IllegalStateException(
        "PictureLayout can host only one direct child"); 
     } 

     super.addView(child, params); 
    } 

    @Override 
    public void addView(View child, int index, LayoutParams params) { 
     if (getChildCount() > 1) { 
      throw new IllegalStateException(
        "PictureLayout can host only one direct child"); 
     } 

     super.addView(child, index, params); 
    } 

    @Override 
    protected LayoutParams generateDefaultLayoutParams() { 
     return new LayoutParams(LayoutParams.MATCH_PARENT, 
       LayoutParams.MATCH_PARENT); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     final int count = getChildCount(); 

     int maxHeight = 0; 
     int maxWidth = 0; 

     for (int i = 0; i < count; i++) { 
      final View child = getChildAt(i); 
      if (child.getVisibility() != GONE) { 
       measureChild(child, widthMeasureSpec, heightMeasureSpec); 
      } 
     } 

     maxWidth += getPaddingLeft() + getPaddingRight(); 
     maxHeight += getPaddingTop() + getPaddingBottom(); 

     Drawable drawable = getBackground(); 
     if (drawable != null) { 
      maxHeight = Math.max(maxHeight, drawable.getMinimumHeight()); 
      maxWidth = Math.max(maxWidth, drawable.getMinimumWidth()); 
     } 

     setMeasuredDimension(resolveSize(maxWidth, widthMeasureSpec), 
       resolveSize(maxHeight, heightMeasureSpec)); 
    } 

    private void drawPict(Canvas canvas, int x, int y, int w, int h, float sx, 
      float sy) { 
     canvas.save(); 
     canvas.translate(x, y); 
     canvas.clipRect(0, 0, w, h); 
     canvas.scale(0.5f, 0.5f); 
     canvas.scale(sx, sy, w, h); 
     canvas.drawPicture(mPicture); 
     canvas.restore(); 
    } 

    @Override 
    protected void dispatchDraw(Canvas canvas) { 
     super.dispatchDraw(mPicture.beginRecording(getWidth(), getHeight())); 
     mPicture.endRecording(); 

     int x = getWidth()/2; 
     int y = getHeight()/2; 

     if (true) { 
      canvas.drawPicture(mPicture); 
     } else { 
      drawPict(canvas, 0, 0, x, y, 1, 1); 
      //drawPict(canvas, x, 0, x, y, -1, 1); 
      //drawPict(canvas, 0, y, x, y, 1, -1); 
      //drawPict(canvas, x, y, x, y, -1, -1); 
     } 
    } 

    @Override 
    public ViewParent invalidateChildInParent(int[] location, Rect dirty) { 
     location[0] = getLeft(); 
     location[1] = getTop(); 
     dirty.set(0, 0, getWidth(), getHeight()); 
     return getParent(); 
    } 

    @Override 
    protected void onLayout(boolean changed, int l, int t, int r, int b) { 
     final int count = super.getChildCount(); 

     for (int i = 0; i < count; i++) { 
      final View child = getChildAt(i); 
      if (child.getVisibility() != GONE) { 
       final int childLeft = getPaddingLeft(); 
       final int childTop = getPaddingTop(); 
       child.layout(childLeft, childTop, 
         childLeft + child.getMeasuredWidth(), 
         childTop + child.getMeasuredHeight()); 

      } 
     } 
    } 
} 

上述過程應該work.Your類應該擴展GraphicsActivity而不是Activity

+0

謝謝。但它不起作用。 –

0

您可以通過編程方式添加按鈕,或者您的自定義視圖可以使xml膨脹。你添加到這個XML你想要的按鈕,你仍然可以在畫布上繪製。

0

你只是需要延長的ViewGroup,而不是查看您的自定義視圖的一些子項添加到它,並動態地添加按鈕,將其作爲

SingleTouchEventView view = new SingleTouchEventView(this, null); 

Button btn = new Button(this); 
LayoutParams pars = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
btn.setLayoutParams(pars); 
btn.setText("Hello World"); 
btn.setPadding(10, 10, 10, 10); 
view.addView(btn); 

setContentView(view); 
相關問題