Android, drawing from the main activity

梦想与她 提交于 2019-12-07 13:08:01

问题


I have a custom view called DrawView created in the main activity. I have implemented the onDraw() method in the DrawView class and it initially draws a circle. I have then added a touch listener, so that when a user clicks, it then draws a square. I am up to the part where the user clicks and a square is drawn. I'm not to sure how to go about this.

public class TestActivity extends Activity {
    DrawView drawing;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ViewGroup myLayout = (ViewGroup) findViewById(R.id.mainLayout);

        drawing = new DrawView(this);
        myLayout.addView(drawing);  

        drawing.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    // draw a square
                }
                return true;
            }
        });
    }

    private class DrawView extends View {

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

        protected void onDraw(Canvas canvas) {
            Paint myPaint = new Paint();
            myPaint.setColor(Color.BLACK);
            // draw a circle
        }
    }
}

Help would be much appreciated.


回答1:


here is the simple snippet for drawing the rectangle when user down,move and up of touch listener fire, just override in DrawView class not by setOnTouchListener()

define the Rect r = new Rect() in DrawView class then after implement this code in DrawView class

public boolean onTouch(View v, MotionEvent event) {
   if (event.getAction() == MotionEvent.ACTION_DOWN) {
      sx = event.getX();
      sy = event.getY();
      r.set(sx,sy,sx,sy);
   }else if(event.getAction==MotionEvent.ACTION_UP){
      r.set(sx,sy,event.getX(),event.getY());
   }else if(event.getAction==MotionEvent.ACTION_MOVE){
      r.set(sx,sy,event.getX(),event.getY());
   }
   invalidate();
   return true;

}

here is the onDraw()

public void onDraw(Canvas canvas){
    super.onDraw(canvas);
    canvas.drawRect(r, new Paint());
}



回答2:


You can create a draw listener:

public interface OnDrawListener {
    void onDraw(Canvas c);
}

...

private class DrawView extends View {
    private OnDrawListener onDrawListener;

    public void setOnDrawListener(OnDrawListener onDrawListener) {
        this.onDrawListener=onDrawListener;
    }

    @Override
    protected void onDraw(Canvas c) {
        onDrawListener.onDraw(c);
    }
}

and then edit override the listener:

// onCreate
drawing.setOnDrawListener(new OnDrawListener() {
    @Override
    public void onDraw(Canvas c) {
        // draw circle
    }
});

and then again with the touch event and override it to draw square.




回答3:


You have to overwrite the original class function:

@Override
public void onDraw(Canvas canvas

To force drawing, use viewObject.invalidate()




回答4:


    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setColor(Color.GREEN);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(5);   


   protected void onDraw(Canvas canvas) {   
        canvas.drawColor(Color.TRANSPARENT);
        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
        canvas.drawPath(mPath, mPaint);
    }


来源:https://stackoverflow.com/questions/8987632/android-drawing-from-the-main-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!