三十功名尘与土 八千里路云和月

书生报国无他物 唯有手中笔如刀

Android_双指缩放TextView

这里写图片描述

xml

<?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"
    >

    <TextView 
        android:id="@+id/control_textView"
        android:text="字体可缩放哦"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"/>

</RelativeLayout>

activity

package com.lxf;

import android.app.Activity;
import android.os.Bundle;
import android.util.FloatMath;
import android.view.MotionEvent;
import android.widget.TextView;

/**
 * @address BeiJing
 * @author LiXufei
 * @function  TextViwe缩放
 */
public class cControlTextView extends Activity{

    private TextView view;
    private int textSize = 0;   
    private float oldDist = 0;
    private int mode = 0;

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

        view = (TextView) findViewById(R.id.control_textView);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (textSize == 0) {
            textSize = (int) view.getTextSize();
        }

        switch (event.getAction()&MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            mode = 1;
            break;

        case MotionEvent.ACTION_UP:
            mode = 0;
            break;

        case MotionEvent.ACTION_POINTER_DOWN:
            mode += 1;
            oldDist = spacing(event);
            break;

        case MotionEvent.ACTION_POINTER_UP:
            mode -= 1;
            break;

        case MotionEvent.ACTION_MOVE:
            if (mode == 2) {
                float newDist = spacing(event);

                if (newDist > oldDist+1) {  //放大
                    draw(newDist/oldDist);
                    oldDist = newDist;
                }
                if (newDist < oldDist-1) {      //缩小
                    draw(newDist/oldDist);
                    oldDist = newDist;
                }
            }
            break;
        }

        return true;
    }

    //绘制TextView
    private void draw(float f){
        view.setTextSize(textSize*=f);
    }

    //获取两指间的距离
    @SuppressWarnings("unused")
    private float spacing(MotionEvent event){

        float x=event.getX(0) - event.getX(1);
        float y=event.getY(0) - event.getY(1);

        return FloatMath.sqrt(x * x + y * y);
    }

}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33068255/article/details/51560291
个人分类: Android
上一篇Android_日期选择器DatePickerDialog、时间选择器TimePickerDialog
下一篇Android_自定义底部动画弹出pupopwindow
想对作者说点什么? 我来说一句

多点触摸可缩放TextView

2016年05月08日 970KB 下载

没有更多推荐了,返回首页

关闭
关闭