Android的,从主要活动绘图 [英] Android, drawing from the main activity

查看:132
本文介绍了Android的,从主要活动绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DrawView 称为自定义视图中的主要活动创建的。我已经实现了的onDraw()方法在 DrawView 类和它最初绘制一个圆。然后,我添加了一个触摸监听器,这样当用户点击,它就会绘制一个正方形。我到其中用户点击和一个正方形绘制的部分。我不知道如何去这个问题。

 公共类测试活动扩展了活动{
    DrawView图;    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        ViewGroup中myLayout =(ViewGroup中)findViewById(R.id.mainLayout);        绘图=新DrawView(本);
        myLayout.addView(图);        drawing.setOnTouchListener(新View.OnTouchListener(){            公共布尔onTouch(视图V,MotionEvent事件){
                如果(event.getAction()== MotionEvent.ACTION_DOWN){
                    //画一个正方形
                }
                返回true;
            }
        });
    }    私有类DrawView扩展视图{        公共DrawView(上下文的背景下){
            超级(上下文);
        }        保护无效的onDraw(帆布油画){
            涂料myPaint =新的油漆();
            myPaint.setColor(Color.BLACK);
            //画一个圆
        }
    }
}

帮助将非常AP preciated。


解决方案

下面是绘制矩形,当用户向下,移动和向上触摸听者火,只是DrawView类中重写不是setOnTouchListener()

定义DrawView类矩形R =新的矩形(),然后实现DrawView类此code后

 公共布尔onTouch(视图V,MotionEvent事件){
   如果(event.getAction()== MotionEvent.ACTION_DOWN){
      SX = event.getX();
      SY = event.getY();
      r.set(SX,SY,SX,SY);
   }否则如果(event.getAction == MotionEvent.ACTION_UP){
      r.set(SX,SY,event.getX(),event.getY());
   }否则如果(event.getAction == MotionEvent.ACTION_MOVE){
      r.set(SX,SY,event.getX(),event.getY());
   }
   无效();
   返回true;

}

这里是的onDraw()

 公共无效的onDraw(帆布油画){
    super.onDraw(画布);
    canvas.drawRect(R,新的油漆());
}

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.

解决方案

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());
}

这篇关于Android的,从主要活动绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆