我做过同样的事情,就像在单点触摸文本上是复制,共享和选择所有文本,在双指上触摸文本是在Activity类中进行放大和缩小一样。但是,当我在片段类文本视图中执行相同的操作时,它只会放大和缩小文本,而无法显示复制,共享和全部选择的功能,仅通过单指触摸即可完成。
public class MainActivity extends Activity implements View.OnTouchListener {
final static float STEP = 200;
TextView mytv;
float mRatio = 1.0f;
int mBaseDist;
float mBaseRatio;
float fontsize = 13;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mytv = (TextView) findViewById(R.id.mytv);
mytv.setTextSize(mRatio + 13);
}
public boolean onTouchEvent(MotionEvent event) {
if (event.getPointerCount() == 2) {
int action = event.getAction();
int pureaction = action & MotionEvent.ACTION_MASK;
if (pureaction == MotionEvent.ACTION_POINTER_DOWN) {
mBaseDist = getDistance(event);
mBaseRatio = mRatio;
} else {
float delta = (getDistance(event) - mBaseDist) / STEP;
float multi = (float) Math.pow(2, delta);
mRatio = Math.min(1024.0f, Math.max(0.1f, mBaseRatio * multi));
mytv.setTextSize(mRatio + 13);
}
}
else if(event.getPointerCount()==1){
mytv.setTextIsSelectable(true);
}
return true;
}
int getDistance(MotionEvent event) {
int dx = (int) (event.getX(0) - event.getX(1));
int dy = (int) (event.getY(0) - event.getY(1));
return (int) (Math.sqrt(dx * dx + dy * dy));
}
public boolean onTouch(View v, MotionEvent event) {
return false;
}
}
和片段类代码
public class DetailContentFragment extends Fragment implements View.OnTouchListener {
private OnDetailedContentFragmentInteractionListener mListener;
FragmentDetailContentBinding binding;
final static float STEP = 200;
private Toast toast;
float mRatio = 1.0f;
int mBaseDist;
float mBaseRatio;
float fontsize = 50;
public static DetailContentFragment newInstance(String param1, String param2) {
DetailContentFragment fragment = new DetailContentFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
binding = DataBindingUtil.inflate(LayoutInflater.from(getContext()),R.layout.fragment_detail_content, container, false);
setHasOptionsMenu(true);
binding.descriptionText.setTextSize(mRatio + 13);
binding.descriptionText.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent event) {
if (event.getPointerCount() == 2) {
int action = event.getAction();
Log.e("hello " ,binding.descriptionView.toString());
int pureaction = action & MotionEvent.ACTION_MASK;
if (pureaction == MotionEvent.ACTION_POINTER_DOWN) {
mBaseDist =getDistance(event);
mBaseRatio = mRatio;
} else {
float delta = (getDistance(event) - mBaseDist) / STEP;
float multi = (float) Math.pow(2, delta);
mRatio = Math.min(1024.0f, Math.max(0.1f, mBaseRatio * multi));
if(mRatio>47.504753) {
mRatio = (float) 47.504753;
if (toast != null) {
toast.cancel();
}
toast = Toast.makeText(getContext(),"Text is zoom enough to see",Toast.LENGTH_SHORT);
toast.show();
}
binding.descriptionText.setTextSize(mRatio + 13);
}
}
else if(event.getPointerCount() == 1){
binding.descriptionText.setTextIsSelectable(true);
}
return true;
}
});
return binding.getRoot();
}
int getDistance(MotionEvent event) {
int dx = (int) (event.getX(0) - event.getX(1));
int dy = (int) (event.getY(0) - event.getY(1));
return (int) (Math.sqrt(dx * dx + dy * dy));
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return false;
}
}