MainActivity:
package com.example.day2_one; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.RelativeLayout.LayoutParams; public class MainActivity extends Activity { private ImageView image_view; float lastDistance = -1; float x0 = 0; float y0 = 0; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); image_view=(ImageView) findViewById(R.id.image_view); image_view.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub int action=event.getAction(); switch (action) { //触摸开始 case MotionEvent.ACTION_UP: image_view.setX(event.getX()); image_view.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 between=(float) Math.sqrt(distanceX*distanceY+distanceX*distanceY); if(between<1){ lastDistance=between; }else if((between-lastDistance)>5){ //放大 lastDistance=between; //获得布局参数 RelativeLayout.LayoutParams parmars=(LayoutParams) image_view.getLayoutParams(); parmars.width=(int) (image_view.getWidth()*1.1f); parmars.height=(int) (image_view.getHeight()*1.1f); image_view.setLayoutParams(parmars); }else if((lastDistance-between)>5){ lastDistance=between; //获得布局参数 RelativeLayout.LayoutParams params=(LayoutParams) image_view.getLayoutParams(); params.width=(int) (image_view.getWidth()*0.9f); params.height=(int) (image_view.getHeight()*0.9f); image_view.setLayoutParams(params); } }else if(count==1){ image_view.setX(event.getX()); image_view.setY(event.getY()); } break; default: break; } return true; } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
布局:
<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/image_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:src="@drawable/ic_launcher" /> </RelativeLayout>
menu文件:activity_main:
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/menu_settings" android:orderInCategory="100" android:showAsAction="never" android:title="@string/menu_settings"/> </menu>
时间: 2024-12-12 16:37:29