canvas教程

Android小白的探索:2D绘图之Android简易版Microsoft Visio学习之路 一(3)

字号+ 作者:H5之家 来源:H5之家 2017-08-28 17:04 我要评论( )

MainActivity public class MainActivity extends AppCompatActivity {RelativeLayout relativeLayout;DrawView drawView;BaseData dataRoot;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.

MainActivity

public class MainActivity extends AppCompatActivity { RelativeLayout relativeLayout; DrawView drawView; BaseData dataRoot; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); relativeLayout = (RelativeLayout) findViewById(R.id.main_relativeLayout); drawView = new DrawView(this); relativeLayout.addView(drawView); float[] rootFloat = {100, 100, 100, 100, 600, 900, 210, 165, 123, 516, 165, 165}; dataRoot = new CompositeData(TypeEnum.CompositeDraw, 20, 100, 0, 0, rootFloat, null, Paint.Style.STROKE); float[] root1Float = {100, 100, 100, 100, 600, 900, 210, 165, 123, 516, 165, 165}; BaseData dataroot1 = new CompositeData(TypeEnum.CompositeDraw, 20, 100, 0, 0, root1Float, null, Paint.Style.STROKE); float[] root2Float = {100, 100, 100, 100, 700, 900, 210, 165, 123, 516, 165, 165}; BaseData dataroot2 = new CompositeData(TypeEnum.CompositeDraw, 20, 100, 0, 0, root2Float, null, Paint.Style.STROKE); float[] leaf1Float = {100, 300, 100, 500, 100, 100, 100, 100, 100, 100, 100, 100}; RectF rectF = new RectF(); rectF.top = 40; rectF.right = 700; rectF.left = 50; rectF.bottom = 200; // float left, float top, float right, float bottom BaseData dataLeaf1 = new EllipseData(TypeEnum.Ellipse, 20, 100, 100, 100, leaf1Float, rectF, Paint.Style.STROKE); float[] leaf2Float = {100, 100, 100, 300, 200, 500, 310, 265, 223, 316, 265, 265}; BaseData dataLeaf2 = new PathData(TypeEnum.Path, 20, 100, 0, 0, leaf2Float, null, Paint.Style.STROKE); float[] leaf3Float = {100, 100, 100, 100, 300, 600, 410, 165, 423, 216, 665, 465}; RectF rectF1 = new RectF(100, 300, 400, 500); BaseData dataLeaf3 = new RectangleData(TypeEnum.Rectangle, 20, 100, 0, 0, leaf3Float, rectF1, Paint.Style.STROKE); float[] leaf4Float = {100, 100, 100, 100, 300, 600, 410, 165, 423, 216, 665, 465}; BaseData dataLeaf4 = new CircularData(TypeEnum.Circular, 20, 100, 0, 0, leaf4Float, null, Paint.Style.STROKE); float[] leaf5Float = {200, 100, 300, 200, 100, 400, 410, 165, 423, 216, 665, 465}; BaseData dataLeaf5 = new LineData(TypeEnum.Line, 20, 100, 0, 0, leaf5Float, null, Paint.Style.STROKE); dataRoot.addChild(dataroot1); dataRoot.addChild(dataroot2); dataroot1.addChild(dataLeaf1); dataroot1.addChild(dataLeaf2); dataroot2.addChild(dataLeaf3); dataroot2.addChild(dataLeaf4); dataroot2.addChild(dataLeaf5); dataLeaf3.addChild(dataLeaf1); drawView.upData(dataRoot); relativeLayout.setOnTouchListener(new TouchListenerImp()); } List dataList; String szie = null; float x = 0; float y = 0; private class TouchListenerImp implements View.OnTouchListener { public boolean onTouch(View v, MotionEvent event) { String show = " "; switch (event.getAction()) { case MotionEvent.ACTION_DOWN: System.out.println("---action down-----"); // show = ("起始位置为:" + "(" + event.getX() + " , " + event.getY() + ")"); dataList = dataRoot.getChild(); for (int i = 0; i < dataList.size(); i++) { DataType[] dataType = dataList.get(i).pointIsInside(event.getX(), event.getY()); if (dataType[0] == DataType.In) { x = event.getX(); y = event.getY(); szie = String.valueOf(i); Log.e("axz", "a = "+Integer.parseInt(szie)); break; } } break; case MotionEvent.ACTION_MOVE: System.out.println("---action move-----"); // show = ("移动中坐标为:" + "(" + event.getX() + " , " + event.getY() + ")"); break; case MotionEvent.ACTION_UP: System.out.println("---action up-----"); if (szie != null) { dataRoot.getChild().get(Integer.parseInt(szie)).move(event.getX() - x, event.getY() - y); Log.e("axz", "a = "+Integer.parseInt(szie)+"x = " + x + "Y = " + y); drawView.upData(dataRoot); szie = null; x = 0; y = 0; } // show = ("最后位置为:" + "(" + event.getX() + " , " + event.getY() + ")"); } // Toast.makeText(MainActivity.this, show, Toast.LENGTH_SHORT).show(); return true; } } }

现在是对枝和叶手动添加的,能够运行,能够对两个枝进行分别的拖动,添加叶也能单独的拖动,拆分、组合。其实就是对枝里list的增、删、改、查。暂时代表组合模式基本成功,如果我理解的有什么不对的,欢迎指正。



文章转载自 开源中国社区 []

本文地址:https://my.oschina.net/zhenghaoLi/blog/1519550

如若转载,请注明出处:

声明:本站所有转载文章仅用于学习和交流目的,如有侵犯您的版权,请联系我们处理。

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
网友点评