啦啦啦啦,我来啦!手指缩放米老鼠给各位客官奉上
好可惜不能放文件夹,不然直接把素材都可以给你们啦,我使用的是模拟机,没使用真机,缩放的时候直接可以按住Ctrl键同时拖动鼠标就可以啦
xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="@mipmap/background"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_mickey"
android:layout_width="100dp"
android:layout_height="120dp"
android:scaleType="fitXY"
android:src="@mipmap/mickey" />
</LinearLayout>
Java代码
package net.hw.a0502_scalemickeybytouches;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
/**
* 米老鼠图像控件
*/
private ImageView ivMickey;
/**
* 线性根布局
*/
private LinearLayout root;
/**
* 布局参数
*/
private LinearLayout.LayoutParams layoutParams;
/**
* 第一个触点的坐标
*/
float x1, y1;
/**
* 第二个触点的坐标
*/
float x2, y2;
/**
* 第一个触点下一次的坐标
*/
float next_x1, next_y1;
/**
* 第二个触点下一次的坐标
*/
float next_x2, next_y2;
/**
* 两个触点之间的距离
*/
float distance;
/**
* 两个触点下一次的距离
*/
float next_distance;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 利用布局资源文件设置用户界面
setContentView(R.layout.activity_main);
// 通过资源索引获得控件实例
ivMickey = (ImageView) findViewById(R.id.iv_mickey);
root = (LinearLayout) findViewById(R.id.root);
// 设置根布局可以获得焦点
root.setFocusable(true);
// 让根布局获得焦点
root.requestFocus();
// 获取图像控件的布局参数
layoutParams = (LinearLayout.LayoutParams) ivMickey.getLayoutParams();
// 给根布局注册触摸监听器
root.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// 根据触点个数执行不同操作(两个触点缩放图像,单个触点移动图像)
if (event.getPointerCount() == 2) { // 两个触点
// 根据触摸动作执行不同的操作
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: // 触点按下
// 获取第一个触点的坐标
x1 = event.getX(0);
y1 = event.getY(0);
// 获取第二个触点的坐标
x2 = event.getX(1);
y2 = event.getY(1);
// 两个触点的距离
distance = (float) Math
.sqrt((x2 - x1)
* (x2 - x1)
+ (y2 - y1)
* (y2 - y1));
break;
case MotionEvent.ACTION_MOVE: // 触点移动
// 获取第一个触点下一次的坐标
next_x1 = event.getX(0);
next_y1 = event.getY(0);
// 获取第二个触点下一次的坐标
next_x2 = event.getX(1);
next_y2 = event.getY(1);
// 两个触点下一次的距离
next_distance = (float) Math.sqrt((next_x2 - next_x1)
* (next_x2 - next_x1) + (next_y2 - next_y1)
* (next_y2 - next_y1));
break;
case MotionEvent.ACTION_UP: // 触点放开
break;
}
// 修改图像控件的布局参数
if (next_distance > distance) {
layoutParams.width = (int) (layoutParams.width * 1.05);
layoutParams.height = (int) (layoutParams.height * 1.05);
} else {
layoutParams.width = (int) (layoutParams.width * 0.95);
layoutParams.height = (int) (layoutParams.height * 0.95);
}
// 坐标迭代
x1 = next_x1;
y1 = next_y1;
x2 = next_x2;
y2 = next_y2;
// 两个触点下一次的距离
distance = (float) Math.sqrt((x2 - x1)
* (x2 - x1) + (y2 - y1)
* (y2 - y1));
} else if (event.getPointerCount() == 1) { // 单点触摸
if (event.getAction() == MotionEvent.ACTION_MOVE) {
// 修改图像控件的布局参数(因为线性布局gravity设置为center,所以要减去屏幕一半的尺寸)
layoutParams.leftMargin = (int) event.getX() - getWindowManager().getDefaultDisplay().getWidth() / 2;
layoutParams.topMargin = (int) event.getY() - getWindowManager().getDefaultDisplay().getHeight() / 2;
}
}
// 重新设置图像控件的布局参数
ivMickey.setLayoutParams(layoutParams);
return true; // 设置为真,三个事件才会依次执行:DOWN->MOVE->UP
}
});
}
}
我只能给你们看效果图,缩放的过程你们可以自己试一下
1.)原图
2.)放大效果图
3.)缩小效果图