关闭

Android_双指缩放TextView

标签: androidtextview布局xml
296人阅读 评论(0) 收藏 举报
分类:

这里写图片描述

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

}
1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3317次
    • 积分:157
    • 等级:
    • 排名:千里之外
    • 原创:12篇
    • 转载:1篇
    • 译文:0篇
    • 评论:1条
    文章分类
    文章存档
    最新评论