多大触控(图片的放大缩小)

来源:互联网 发布:linux 建立目录 编辑:程序博客网 时间:2024/05/02 01:42

<-------------------------Activity----------------------------------->

public class MainActivity extends Activity {



private ImageView imageView;
float lastDistance=-1;
float x0=0;
float y0=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}


private void init() {
imageView = (ImageView) findViewById(R.id.imageView);

imageView.setOnTouchListener(new OnTouchListener() {

public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
Log.i("TAG", "开始触摸了。。");
switch (action) {
case MotionEvent.ACTION_UP://手指抬起
/*imageView.setX(event.getX());
imageView.setY(event.getY());*/
break;

case MotionEvent.ACTION_MOVE://手指移动
//得到触摸点的个数
int count = event.getPointerCount();
if(count>1){
//获得两点的坐标差
float distanceX=event.getX(0)-event.getX(1);
float distanceY=event.getY(0)-event.getY(1);
//两点之间的距离
float betweenDistance=(float) Math.sqrt(distanceX*distanceX+distanceY*distanceY);
if(betweenDistance<1){
lastDistance=betweenDistance;
}else if(betweenDistance-lastDistance > 5){
lastDistance=betweenDistance;
//放大
RelativeLayout.LayoutParams params= (android.widget.RelativeLayout.LayoutParams) imageView.getLayoutParams();
params.width=(int) (imageView.getWidth()*1.1f);
params.height=(int) (imageView.getHeight()*1.1f);
imageView.setLayoutParams(params);
}else if(lastDistance-betweenDistance > 5){
lastDistance=betweenDistance;
//缩小
RelativeLayout.LayoutParams params= (android.widget.RelativeLayout.LayoutParams) imageView.getLayoutParams();
params.width=(int) (imageView.getWidth()*0.9f);
params.height=(int) (imageView.getHeight()*0.9f);
imageView.setLayoutParams(params);
}
}else if(count==1){
/*imageView.setX(event.getX());
imageView.setY(event.getY());*/
}
break;


}
return true;
}
});
}


}


<-------------------------xml文件----------------------------------->

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >


    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@drawable/xk" />


</RelativeLayout>

0 0
原创粉丝点击