zoukankan      html  css  js  c++  java
  • 实现画图板

    package com.pingyijinren.helloworld;
    
    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;
    
    import java.util.ArrayList;
    import java.util.Iterator;
    
    /**
     * Created by Administrator on 2016/6/2 0002.
     */
    public class MyPaintView extends View {
        private ArrayList<Point> allPoints=new ArrayList<Point>();
        public MyPaintView(Context context, AttributeSet attrs) {
            super(context, attrs);
            setBackgroundColor(Color.WHITE);
            setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    Point point=new Point((int)event.getX(),(int)event.getY());
                    if(event.getAction()==MotionEvent.ACTION_DOWN){
                        allPoints=new ArrayList<Point>();
                        allPoints.add(point);
                    }
                    else if(event.getAction()==MotionEvent.ACTION_UP){
                        allPoints.add(point);
                    }
                    else if(event.getAction()==MotionEvent.ACTION_MOVE){
                        allPoints.add(point);
                        MyPaintView.this.postInvalidate();
                    }
                    return true;
                }
            });
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            Paint paint=new Paint();
            paint.setColor(Color.RED);
            if(allPoints.size()>1){
                Iterator<Point> iterator=allPoints.iterator();
                Point firstPoint=null;
                Point lastPoint=null;
                while(iterator.hasNext()){
                    if(firstPoint==null){
                        firstPoint=iterator.next();
                    }
                    else{
                        if(lastPoint!=null){
                            firstPoint=lastPoint;
                        }
                        lastPoint=iterator.next();
                        canvas.drawLine(firstPoint.x,firstPoint.y,lastPoint.x,lastPoint.y,paint);
                    }
                }
            }
        }
    }
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.pingyijinren.helloworld.MainActivity">
    
        <com.pingyijinren.helloworld.MyPaintView
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </LinearLayout>
    package com.pingyijinren.helloworld;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    
    public class MainActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    }
  • 相关阅读:
    第9章 线程编程(7)_线程同步4:信号量
    第9章 线程编程(6)_线程同步3:条件变量
    第9章 线程编程(5)_线程同步2:读写锁
    第9章 线程编程(4)_线程同步1:互斥锁
    第9章 线程编程(3)_线程清理和控制函数
    第9章 线程编程(2)_线程创建和终止
    第9章 线程编程(1)_线程概念和线程标识
    第8章 信号(6)_贯穿案例2:mini shell(3)
    第8章 信号(5)_信号集和信号屏蔽
    第8章 信号(4)_中断的系统调用和函数可重入性
  • 原文地址:https://www.cnblogs.com/zqxLonely/p/5552563.html
Copyright © 2011-2022 走看看