当我的手指在textview周围时(我记得没有放在textview上)我可以放大/缩小文本视图但问题是当我尝试放大或缩小同时保持双手放在文本上时认为它不起作用。
这是我执行放大/缩小并在运行时在屏幕上移动文本视图的活动
public class MainActivity extends AppCompatActivity {
TextView tv;
EditText et;
Button ok;
float dX, dY;
ScaleGestureDetector scaleGestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tv = (TextView) findViewById(R.id.tvtoshow);
tv.setVisibility(View.INVISIBLE);
ok = (Button) findViewById(R.id.btnok);
et = (EditText) findViewById(R.id.ettext);
String abc = et.getText().toString();
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
// TODO: handle exception
}
String abc = et.getText().toString();
et.setVisibility(View.INVISIBLE);
ok.setVisibility(View.INVISIBLE);
tv.setText(abc);
tv.setVisibility(View.VISIBLE);
tv.setRotation(progress);
}
});
tv.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if(motionEvent.getAction()== MotionEvent.ACTION_MOVE)
{
// view.setX(motionEvent.getRawX() - view.getWidth());
// view.setY(motionEvent.getRawY() - view.getHeight());
view.animate()
.x(motionEvent.getRawX() + dX)
.y(motionEvent.getRawY() + dY)
.setDuration(0)
.start();
}
else if (motionEvent.getAction()== MotionEvent.ACTION_DOWN)
{
dX = view.getX() - motionEvent.getRawX();
dY = view.getY() - motionEvent.getRawY();
}
else if (motionEvent.getAction() == MotionEvent.ACTION_UP)
{
return true;
}
return true;
}
});
scaleGestureDetector = new ScaleGestureDetector(this, new simpleOnScaleGestureListener());
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
scaleGestureDetector.onTouchEvent(event);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public class simpleOnScaleGestureListener extends
ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
// TODO Auto-generated method stub
float size = tv.getTextSize();
Log.d("TextSizeStart", String.valueOf(size));
float factor = detector.getScaleFactor();
Log.d("Factor", String.valueOf(factor));
float product = size*factor;
Log.d("TextSize", String.valueOf(product));
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, product);
size = tv.getTextSize();
Log.d("TextSizeEnd", String.valueOf(size));
return true;
}
}
}
答案 0 :(得分:0)
我认为不是覆盖Touch上的活动 你可以这样做
tv.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
event.setLocation(event.getRawX(), event.getRawY());
scaleGestureDetector.onTouchEvent(event);
return true;
}
});'
使用上方缩放将在文本视图内部开始工作。 对于文本视图外部的触摸,将setOnTouch侦听器发送到textView的父级,并按上述步骤进行操作