`

android -> canvas学习3 (跟随手势画直线)

 
阅读更多

 

 DiyView.java

package com.test1;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

/**
 * 九宫格解锁控件
 * Created by Jerry on 2015/9/21.
 */
public class DiyView extends View {
    public DiyView(Context context) {
        this(context, null);
    }
    public DiyView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public DiyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        init();
    }

    //初始化
    Paint paint;
    private void init(){
        this.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg));//背景
        //初始画笔
        paint = new Paint();
        paint.setColor(Color.parseColor("#00B7EE"));
        paint.setAntiAlias(true);//消除锯齿
        paint.setStrokeWidth(33);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //画直线
        //drawLine(float startX, float startY, float stopX, float stopY,Paint)

        canvas.drawLine(startX, startY, endX, endY, paint);

    }

    private float startX, startY;
    private float endX,endY;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //获取手势当下坐标
        float crtX = event.getX();
        float crtY = event.getY();
        if(event.getAction() == MotionEvent.ACTION_DOWN){
            //按下时,开始  位置 和 结束位置 都是在一个点就是当前的点,
            startX = crtX;
            startY = crtY;
            //
            endX = crtX;
            endY = crtY;
        }
        if(event.getAction() == MotionEvent.ACTION_MOVE){
            endX = crtX;
            endY = crtY;

        }
        if(event.getAction() == MotionEvent.ACTION_UP){
            startX = 0;
            startY = 0;
            //
            endX = 0;
            endY = 0;
        }
        invalidate();//重新执行onDraw
        return true;
    }

    public void print(String str){
        Log.d("mft",str);
    }
}

 

 

布局文件  

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

 

    <com.test1.DiyView
        android:id="@+id/diyView"
        android:layout_width="match_parent"
        android:layout_height="300dip"
        android:background="#55ff00ff"
        />
</RelativeLayout>

 

入口文件 MainActivity.java  同样在这里面也可以做 画布的 监听 

package com.test1;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class MainActivity extends Activity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //

        DiyView lockView = (DiyView) findViewById(R.id.diyView);
        lockView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    print("onMain Down");
                }
                if (event.getAction() == MotionEvent.ACTION_MOVE) {
                    print("onMain Move");
                }
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    print("onMain Up");
                }
                return false;
            }
        });
        //\\

    }
    @Override
    public void onClick(View v) {
        String str = v.getTag().toString();
        print(str);

    }


    public void print(String con){
        Log.d("mft",con);
    }


}

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics