Multi-touch in Android can enable image enlargement, reduction and rotation, etc. for your reference. The specific content is as follows
The user's touch events are mainly monitored through the setOnTouchListener method, and the x-axis (or y-axis) coordinates of the first touch point and the second touch point are obtained through (0) and (1). Next, the obtained x-axis or y-axis are processed in MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE, MotionEvent.ACTION_UP, and the obtained x-axis or y-axis can be achieved.
The following small demo implements the processing of enlarging and reducing the image:
package ; import ; import .; import ; import ; import ; import ; import ; import ; public class MainActivity extends ActionBarActivity { private RelativeLayout layout; private ImageView iv; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); layout = (RelativeLayout) findViewById(); iv = (ImageView) findViewById(); (new () { float currentDistance; float lastDistance = -1; @Override public boolean onTouch(View v, MotionEvent event) { switch (()) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_MOVE: //Judge several touch points if (() >= 2) { //The coordinate difference of x between two points float offsetX = (0) - (1); //The difference in coordinates of y between two points float offsetY = (0) - (1); //The distance between two points currentDistance = (float) (offsetX * offsetX + offsetY * offsetY); if (lastDistance < 0) { //No zoom lastDistance = currentDistance; } else { if (currentDistance - lastDistance > 5) {//enlarge lp = () (); = (int) (1.1f * ()); = (int) (1.1f * ()); (lp); lastDistance = currentDistance; } else if (currentDistance - lastDistance < -5) {//Shrink int currentIvWidth = (); int currentIvHeight = (); if (currentIvWidth > 50 && currentIvHeight >50) { lp = () (); = (int) (0.9f * ()); = (int) (0.9f * ()); (lp); lastDistance = currentDistance; } } } } break; case MotionEvent.ACTION_UP: break; } return true; } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(.menu_main, menu); 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 . int id = (); //noinspection SimplifiableIfStatement if (id == .action_settings) { return true; } return (item); } }
xml code:
<RelativeLayout xmlns:andro android: xmlns:tools="/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <ImageView android: android:layout_width="wrap_content" android:src="@mipmap/a" android:layout_height="wrap_content" /> </RelativeLayout>
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.