zoukankan      html  css  js  c++  java
  • android 绘图



    在main.xml文件中代码如下:

    <?xml version="1.0" encoding="utf-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:orientation="vertical" >

      <com.tarena.touch.MyPaintView

          android:id="@+id/paintView"

          android:layout_width="fill_parent"

          android:layout_height="fill_parent"/>

    </LinearLayout>

    在MyPaintView.java中代码如下:

    package com.tarena.touch;

    import java.util.ArrayList;

    import java.util.Iterator;

    import java.util.List;

    import android.content.Context;

    import android.graphics.Canvas;

    import android.graphics.Color;

    import android.graphics.Paint;

    import android.graphics.Point;

    import android.util.AttributeSet;

    import android.view.MotionEvent;

    import android.view.View;

    public class MyPaintView extends View{

      private List<Point> allPoint = new ArrayList<Point>();  //保存所有的操作坐标

      public MyPaintView(Context context, AttributeSet attrs) {  //接收Context,同时接收属性集合

         super(context, attrs); //调用父类的构造

         super.setOnTouchListener(new OnTouchListenerImpl());

      }

      private class OnTouchListenerImpl implements OnTouchListener{

         public boolean onTouch(View v, MotionEvent event) {

          Point p = new Point((int) event.getX(),(int) event.getY());  //将坐标保存在Point类中

           if(event.getAction() == MotionEvent.ACTION_DOWN){    //用户按下

             MyPaintView.this.allPoint = new ArrayList<Point>();  //重绘

             MyPaintView.this.allPoint.add(p);  //保存新的点

            

           }

           else if(event.getAction() == MotionEvent.ACTION_UP){  //用户松开

             MyPaintView.this.allPoint.add(p);  //记录坐标点

             MyPaintView.this.postInvalidate(); //重绘图形

           }

           else if(event.getAction() == MotionEvent.ACTION_MOVE){  //用户移动

             MyPaintView.this.allPoint.add(p);  //记录坐标点

             MyPaintView.this.postInvalidate(); //重绘图形

            

           }

           return true;  //表示下面的操作不再执行

         }

        

      }

      protected void onDraw(Canvas canvas){ //进行重绘

         Paint p = new Paint(); //依靠此类开始画线

         p.setColor(Color.RED); //定义线的颜色

         if(MyPaintView.this.allPoint.size() > 1){ // 有坐标点保存的时候开始绘图

           Iterator<Point> iter = MyPaintView.this.allPoint.iterator();

           Point first = null;  //开始的坐标点

           Point last = null; //结束的坐标点

           while(iter.hasNext()){

             if(first == null){

                first = (Point) iter.next();  //现在没有在绘图,取出坐标

             }

             else{

                if(last != null){  //前一阶段已经完成

                  first = last; //重新更新下一阶段

                }

                last = (Point) iter.next(); //结束点坐标

                canvas.drawLine(first.x, first.y, last.x, last.y, p);

             }

           }

         }

      }

    }

    在MyTouchDemo.java中代码如下:

    package com.tarena.touch;

    import android.app.Activity;

    import android.os.Bundle;

    import android.view.MotionEvent;

    import android.view.View;

    import android.view.View.OnTouchListener;

    import android.widget.TextView;

    public class MyTouchDemo extends Activity {

        @Override

        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            super.setContentView(R.layout.main);

        }

    }

  • 相关阅读:
    UML各种图总结
    信息系统安全等级保护基本要求
    MySQL InnoDB表空间加密
    服务器常见操作问题
    公众号-接口配置信息 接口实现 netcore
    某些时候命令绑定可能会存在刷新不及时,往往需要点击一次程序才能激活,特此记录下解决方案
    c#几种场景获取程序运行目录
    wpf 控件绑定鼠标命令、键盘命令
    并发特别高的时候,随机数的种子生成
    mongodb占用大量内存
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3301732.html
Copyright © 2011-2022 走看看