android触摸 图片的缩放和放大 带着图片在屏幕上游走

package com.example.multouch;


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.Toast;


public class MainActivity extends Activity {
   private FrameLayout root;
   private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv=(ImageView) findViewById(R.id.iv);
root=(FrameLayout) findViewById(R.id.container);
root.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//Toast.makeText(MainActivity.this, "down", Toast.LENGTH_SHORT).show();
break;
case MotionEvent.ACTION_MOVE:
//Toast.makeText(MainActivity.this, String.format("x:%f,y:%f",event.getX(),event.getY()), Toast.LENGTH_SHORT).show();
FrameLayout.LayoutParams lp=(LayoutParams) iv.getLayoutParams();
lp.leftMargin=(int) event.getX();
lp.topMargin=(int) event.getY();

iv.setLayoutParams(lp);
break;
case MotionEvent.ACTION_UP:
//Toast.makeText(MainActivity.this, "up", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return true;
}
});
}

}

上面代码达到的效果:一张图片可以在屏幕上随意拖动。


package com.example.multouch;


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.Toast;


public class MainActivity extends Activity {
   private FrameLayout root;
   private ImageView iv;
   private float currentDistance;
   private float lastDistance=-1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv=(ImageView) findViewById(R.id.iv);
root=(FrameLayout) findViewById(R.id.container);
root.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//Toast.makeText(MainActivity.this, "down", Toast.LENGTH_SHORT).show();
break;
case MotionEvent.ACTION_MOVE:
float offsetx=event.getX(0)-event.getX(1);
float offsety=event.getY(0)-event.getY(1);
currentDistance=(float) Math.sqrt(offsetx*offsetx+offsety*offsety);
if(lastDistance<0){
lastDistance=currentDistance;
}else{
if(currentDistance-lastDistance>5){
FrameLayout.LayoutParams lp=(LayoutParams) iv.getLayoutParams();
   lp.width*=1.1;
   lp.height*=1.1;
   iv.setLayoutParams(lp);
   lastDistance=currentDistance;
   
}else if(lastDistance-currentDistance>5){
FrameLayout.LayoutParams lp=(LayoutParams) iv.getLayoutParams();
   lp.width*=0.6;
   lp.height*=0.6;
   iv.setLayoutParams(lp);
   lastDistance=currentDistance;
}
}
//Toast.makeText(MainActivity.this, String.format("x:%f,y:%f",event.getX(),event.getY()), Toast.LENGTH_SHORT).show();
//Toast.makeText(MainActivity.this,"point:"+event.getPointerCount() , Toast.LENGTH_SHORT).show();
// FrameLayout.LayoutParams lp=(LayoutParams) iv.getLayoutParams();
// lp.leftMargin=(int) event.getX();
// lp.topMargin=(int) event.getY();
// iv.setLayoutParams(lp);
break;
case MotionEvent.ACTION_UP:
//Toast.makeText(MainActivity.this, "up", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return true;
}
});
}
}

完成图片的放大和缩放

你可能感兴趣的:(android触摸 图片的缩放和放大 带着图片在屏幕上游走)