多点触控

//字体的放大缩小

package com.xiaoyu.multi_touch;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 1.类的用途:
 * 2.作者:段玉
 * 3.时间:2017/2/10 11
 * 4.邮箱:[email protected]
 */
public class ScaleTextview extends TextView {

    //获取两个指头之间的距离
    private double olddistance;
    private float textSize;

    public ScaleTextview(Context context) {
        super(context);
    }

    public ScaleTextview(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ScaleTextview(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getActionMasked()){
            case MotionEvent.ACTION_POINTER_DOWN:
            {
                //获取字体大小
                if (textSize == 0){
                    textSize = this.getTextSize();
                    //吐司
                    Toast.makeText(getContext(),textSize+"",Toast.LENGTH_SHORT).show();
                }

                //获取两指之间的距离
                olddistance = getdistance(event);
            }
                break;
            case MotionEvent.ACTION_MOVE:
            {
                //判断当前有几个手指
                if (event.getPointerCount() >= 2){
                    //获取滑动时两指之间的距离
                    double distance = getdistance(event);
                    //获取按下时 和滑动时  两指之间距离的比例
                    Zoom(distance / olddistance);

                    //重置下按下的距离
                    olddistance = distance;
                }
            }
                break;
        }


        return true;
    }

    /**
     * 重新设置字体大小
     * @param v
     */
    private void Zoom(double v) {
        //textview控件重新设置字体大小
        textSize = (float) (textSize * v);
        //重新设置textview
        this.setTextSize(px2sp(getContext(),textSize));

    }

    /**
     * 获取两指之间距离
     * @param event
     * @return
     */
    public double getdistance(MotionEvent event){
        float x = event.getX(0);
        float x1 = event.getX(1);
        float y = event.getY(0);
        float y1 = event.getY(1);

        //根据勾股定理 取平方根
        double sqrt = Math.sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1));

        return sqrt;

    }

    /**
     * px值转换为sp,保证文字大小不变
     * @param context
     * @param pxValue
     * @return
     */
    public int px2sp(Context context,float pxValue){
        float scaledDensity = context.getResources().getDisplayMetrics().scaledDensity;

        return (int) (pxValue / scaledDensity + 0.5f);
    }

}

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

    <com.baway.multi_touch.ScaleTextview
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="30dp"
        android:text="浅宝宝_筱玉"/>

</LinearLayout>

//图片的放大缩小

package com.xiaoyu.multi_touch;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * 1.类的用途:
 * 2.作者:段玉
 * 3.时间:2017/2/10 14
 * 4.邮箱:[email protected]
 */
public class ScaleImageview extends View {

    private Bitmap bitmap;
    Matrix matrix = new Matrix();
    private double olddistance;

    public ScaleImageview(Context context) {
        super(context);
        init();
    }


    public ScaleImageview(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.nuan_bao);
    }

    public ScaleImageview(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(bitmap,matrix,null);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getActionMasked()){
            case MotionEvent.ACTION_POINTER_DOWN:
            {
                //获取两指之间距离
                olddistance = getdistance(event);
            }
                break;
            case MotionEvent.ACTION_MOVE:
            {
                if (event.getPointerCount() >= 2){
                    //获取移动中两指之间的距离
                    double distance = getdistance(event);
                    //获取按下时 和滑动时  两指之间距离的比例
                    zoom((float) (distance / olddistance));

                    olddistance = distance;
                }
            }
                break;
        }
        return true;
    }

    /**
     * 重新绘制图片
     * @param v
     */
    private void zoom(float v) {
        //矩阵缩放
        matrix.postScale(1.0f * v, 1.0f * v);
        //重新绘制
        postInvalidate();
    }

    //获取两指之间的距离
    public double getdistance(MotionEvent event){
        float x = event.getX(0);
        float x1 = event.getX(1);
        float y = event.getY(0);
        float y1 = event.getY(1);

        //取平方根
        double sqrt = Math.sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1));

        return sqrt;
    }
}


猜你喜欢

转载自blog.csdn.net/qaz18610474875/article/details/54970019