现在的位置: 首页 > 移动开发 > 正文

android 绘图

2019年07月21日 移动开发 ⁄ 共 2805字 ⁄ 字号 评论关闭

 

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

 

    }

}

抱歉!评论已关闭.