zoukankan      html  css  js  c++  java
  • 实现:TextView自由复制功能


    源代码已经上传,链接地址:http://download.csdn.net/detail/huangyabin001/7556825


    package com.example.copyfree;
    
    import android.app.ActionBar;
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.app.AlertDialog.Builder;
    import android.content.DialogInterface;
    import android.content.DialogInterface.OnClickListener;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.text.TextUtils;
    import android.text.method.HideReturnsTransformationMethod;
    import android.util.TypedValue;
    import android.view.GestureDetector;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.MotionEvent;
    import android.view.ScaleGestureDetector;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class CopyFreeActivity extends Activity {
    	private static final String TAG = "SelectTextToCopyActivity";
    	private float mScaleFactor = 1;
    	private ScaleGestureDetector mScaleDetector;
    	private GestureDetector mGestureDetector;
    	private TextView text;
    	private EditText edit;
    
    	private static final int ZOOM_IN = 4;
    	private static final int ZOOM_OUT = 5;
    	private final int MAX_ZOOM_IN_SIZE = 60;
    	private final int MAX_ZOOM_OUT_SIZE = 20;
    	private final int THE_SIZE_OF_PER_ZOOM = 9;
    	private float mTextSize = 27;
    	private int mZoomMsg = -1;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_copy_free);
    		initUi();
    	}
    
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu) {
    		return true;
    	}
    
    	public void button(View view) {
    		if (TextUtils.isEmpty(edit.getText())) {
    			Toast.makeText(this, "请输入测试内容!", 0).show();
    		} else
    			text.setText(edit.getText());
    	}
    
    	@Override
    	public boolean onSearchRequested() {
    		return false;
    	}
    
    	@Override
    	public boolean onOptionsItemSelected(MenuItem item) {
    		switch (item.getItemId()) {
    		case android.R.id.home:
    			AlertDialog.Builder dialog = new AlertDialog.Builder(this);
    			dialog.setTitle("Exit").setIcon(android.R.drawable.btn_star)
    					.setMessage("立即退出程序?")
    					.setPositiveButton("确定", new OnClickListener() {
    
    						@Override
    						public void onClick(DialogInterface dialog, int which) {
    							CopyFreeActivity.this.finish();
    						}
    					}).setNegativeButton("取消", new OnClickListener() {
    
    						@Override
    						public void onClick(DialogInterface dialog, int which) {
    							dialog.cancel();
    						}
    					}).show();
    			break;
    
    		}
    		return super.onOptionsItemSelected(item);
    	}
    
    	private void initUi() {
    		text = (TextView) findViewById(R.id.text);
    		edit = (EditText) findViewById(R.id.edit);
    		text.setTransformationMethod(HideReturnsTransformationMethod
    				.getInstance());
    		text.setTextIsSelectable(true);
    		mScaleDetector = new ScaleGestureDetector(this, new MyScaleListener());
    		mGestureDetector = new GestureDetector(this,
    				new GestureDetector.SimpleOnGestureListener() {
    				});
    		mGestureDetector.setOnDoubleTapListener(null);
    		ActionBar actionBar = getActionBar();
    		actionBar.setDisplayHomeAsUpEnabled(true);
    	}
    
    	private Handler mUiHandler = new Handler() {
    		@Override
    		public void handleMessage(Message msg) {
    			switch (msg.what) {
    			case ZOOM_IN:
    				zoomIn();
    				text.invalidate();
    				break;
    			case ZOOM_OUT:
    				zoomOut();
    				text.invalidate();//修改TextView后,调用该方法刷新TextView,这样才能看到重新绘制的界面。
    				break;
    			default:
    				break;
    			}
    		}
    	};
    
    	private void zoomIn() {
    		mTextSize = mTextSize + THE_SIZE_OF_PER_ZOOM <= MAX_ZOOM_IN_SIZE ? mTextSize
    				+ THE_SIZE_OF_PER_ZOOM
    				: MAX_ZOOM_IN_SIZE;
    		if (mTextSize >= MAX_ZOOM_IN_SIZE) {
    			mTextSize = MAX_ZOOM_IN_SIZE;
    		}
    		text.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
    	}
    
    	private void zoomOut() {
    		mTextSize = mTextSize - THE_SIZE_OF_PER_ZOOM < MAX_ZOOM_OUT_SIZE ? MAX_ZOOM_OUT_SIZE
    				: mTextSize - THE_SIZE_OF_PER_ZOOM;
    		if (mTextSize <= MAX_ZOOM_OUT_SIZE) {
    			mTextSize = MAX_ZOOM_OUT_SIZE;
    		}
    		text.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
    	}
    
    	private class MyScaleListener extends
    			ScaleGestureDetector.SimpleOnScaleGestureListener {
    		@Override
    		public boolean onScale(ScaleGestureDetector detector) {
    			float scale = detector.getScaleFactor();
    			if (scale < 0.999999 || scale > 1.00001) {
    				mScaleFactor = scale;
    			}
    			return true;
    		}
    
    		@Override
    		public boolean onScaleBegin(ScaleGestureDetector detector) {
    			return true;
    		}
    
    		@Override
    		public void onScaleEnd(ScaleGestureDetector detector) {
    			float scale = detector.getScaleFactor();
    			if (mScaleFactor > 1.0) {
    				mZoomMsg = ZOOM_IN;
    			} else if (mScaleFactor < 1.0) {
    				mZoomMsg = ZOOM_OUT;
    			}
    		}
    	}
    
    	public boolean onInterceptTouchEvent(MotionEvent ev) {
    		mScaleDetector.onTouchEvent(ev);
    		final int action = ev.getAction();
    		switch (action) {
    		case MotionEvent.ACTION_DOWN:
    			mGestureDetector.onTouchEvent(ev);
    			return false;
    
    		case MotionEvent.ACTION_MOVE:
    			mGestureDetector.onTouchEvent(ev);
    			return false;
    
    		case MotionEvent.ACTION_UP:
    			mGestureDetector.onTouchEvent(ev);
    			Message msg = Message.obtain();
    			msg.what = mZoomMsg;
    			mUiHandler.sendMessage(msg);
    			mZoomMsg = -1;
    			return false;
    		}
    		return true;
    	}
    
    	public boolean onTouchEvent(MotionEvent ev) {
    		mScaleDetector.onTouchEvent(ev);
    		final int action = ev.getAction();
    
    		switch (action) {
    		case MotionEvent.ACTION_DOWN:
    			mGestureDetector.onTouchEvent(ev);
    			return true;
    
    		case MotionEvent.ACTION_MOVE:
    			mGestureDetector.onTouchEvent(ev);
    			return true;
    
    		case MotionEvent.ACTION_UP:
    			mGestureDetector.onTouchEvent(ev);
    			Message msg = Message.obtain();
    			msg.what = mZoomMsg;
    			mUiHandler.sendMessage(msg);
    			mZoomMsg = -1;
    			return true;
    
    		case MotionEvent.ACTION_CANCEL:
    			mGestureDetector.onTouchEvent(ev);
    			return true;
    
    		default:
    			if (mGestureDetector.onTouchEvent(ev)) {
    				return true;
    			}
    
    			return true;
    		}
    	}
    
    	@Override
    	protected void onDestroy() {
    		super.onDestroy();
    		mUiHandler.removeCallbacksAndMessages(null);
    	}
    }




  • 相关阅读:
    20180420首旅酒店的操作记录
    股票的站上均线和反弹购买的学习
    linux里面的fork函数创建一个新进程
    allegro导入网表过程中出现的错误信息
    海思NB-IOT的SDK添加AT指令方法
    如何在ubuntun中安装pycharm并将图标显示在桌面上
    流畅python学习笔记:第十九章:动态属性和特性
    一起来学linux:目录与路径
    一起来学linux:用户与用户组
    python自动化运维五:pexpect
  • 原文地址:https://www.cnblogs.com/bill-technology/p/4130864.html
Copyright © 2011-2022 走看看