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

        }

    }

  • 相关阅读:
    检查网上下载“学习资料”的完整性,用这招就够了
    MP4 视频“只闻其声,不见其人”,是怎么回事呢?
    大型网课翻车现场!原因竟是……
    看图说话,P2P 分享率 90% 以上的 P2P-CDN 服务,来了!
    Linux删除除了某些文件之外的所有文件或文件夹
    git分支管理三
    git远程仓库(二)
    git基础(一)
    ntp时间服务器
    oracle-3-Linux-11g安装-图形安装
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3301732.html
Copyright © 2011-2022 走看看