图片缩放
来源:互联网 发布:淘宝女装店铺推荐日韩 编辑:程序博客网 时间:2024/06/13 23:48
package baway.com.cj.baway1502h2.activity;
import android.app.Activity;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import com.google.gson.Gson;
import org.greenrobot.eventbus.EventBus;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;
import java.util.List;
import baway.com.cj.baway1502h2.R;
import baway.com.cj.baway1502h2.application.ApplicationConstants;
import baway.com.cj.baway1502h2.model.bean.JokeBean;
import baway.com.cj.baway1502h2.model.event.Message;
/**
* Created by cj on 2017/2/14.
*/
public class SecondActivity extends Activity {
private final int MODE_DRAG = 0;
private final int MODE_ZOOM = 1;
private final int MODE_NONE = 3;
private int currentMode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Button button = (Button) findViewById(R.id.sendMessage);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EventBus.getDefault().post(new Message("ok"));
}
});
final ImageView imageView = (ImageView) findViewById(R.id.dog);
imageView.setOnTouchListener(new View.OnTouchListener() {
private double beginSpacing;
private float y;
private float x;
Matrix matrix = new Matrix();
Matrix newMatrix = new Matrix();
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
x = motionEvent.getX();
y = motionEvent.getY();
matrix.set(imageView.getImageMatrix());
currentMode = MODE_DRAG;
break;
case MotionEvent.ACTION_POINTER_DOWN:
if (spacing(motionEvent) > 10) {
currentMode = MODE_ZOOM;
beginSpacing = spacing(motionEvent);
matrix.set(imageView.getImageMatrix());
}
break;
case MotionEvent.ACTION_MOVE:
if (currentMode == MODE_DRAG) {
newMatrix.set(matrix);
float newX = motionEvent.getX();
float newY = motionEvent.getY();
float lastX = newX - x;
float lastY = newY - y;
newMatrix.postTranslate(lastX, lastY);
} else if (currentMode == MODE_ZOOM) {
//计算缩放比例
if (spacing(motionEvent) > 10) {
newMatrix.set(matrix);
double newSpacing = spacing(motionEvent);
float scale = (float) newSpacing / (float) beginSpacing;
PointF pointF = midPoint(motionEvent);
newMatrix.postScale(scale, scale, pointF.x, pointF.y);
}
}
break;
case MotionEvent.ACTION_UP:
currentMode = MODE_NONE;
break;
}
imageView.setImageMatrix(newMatrix);
return true;
}
});
}
//计算两点之间的距离
private double spacing(MotionEvent event) {
float x = 0;
float y = 0;
try {
x = event.getX(0) - event.getX(1);
y = event.getY(0) - event.getY(1);
} catch (IllegalArgumentException e) {}
return Math.sqrt(x * x + y * y);
}
//计算中点位置
private PointF midPoint(MotionEvent event) {
PointF pointF = new PointF();
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
pointF.set(x / 2, y / 2);
return pointF;
}
}
import android.app.Activity;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import com.google.gson.Gson;
import org.greenrobot.eventbus.EventBus;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;
import java.util.List;
import baway.com.cj.baway1502h2.R;
import baway.com.cj.baway1502h2.application.ApplicationConstants;
import baway.com.cj.baway1502h2.model.bean.JokeBean;
import baway.com.cj.baway1502h2.model.event.Message;
/**
* Created by cj on 2017/2/14.
*/
public class SecondActivity extends Activity {
private final int MODE_DRAG = 0;
private final int MODE_ZOOM = 1;
private final int MODE_NONE = 3;
private int currentMode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Button button = (Button) findViewById(R.id.sendMessage);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EventBus.getDefault().post(new Message("ok"));
}
});
final ImageView imageView = (ImageView) findViewById(R.id.dog);
imageView.setOnTouchListener(new View.OnTouchListener() {
private double beginSpacing;
private float y;
private float x;
Matrix matrix = new Matrix();
Matrix newMatrix = new Matrix();
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
x = motionEvent.getX();
y = motionEvent.getY();
matrix.set(imageView.getImageMatrix());
currentMode = MODE_DRAG;
break;
case MotionEvent.ACTION_POINTER_DOWN:
if (spacing(motionEvent) > 10) {
currentMode = MODE_ZOOM;
beginSpacing = spacing(motionEvent);
matrix.set(imageView.getImageMatrix());
}
break;
case MotionEvent.ACTION_MOVE:
if (currentMode == MODE_DRAG) {
newMatrix.set(matrix);
float newX = motionEvent.getX();
float newY = motionEvent.getY();
float lastX = newX - x;
float lastY = newY - y;
newMatrix.postTranslate(lastX, lastY);
} else if (currentMode == MODE_ZOOM) {
//计算缩放比例
if (spacing(motionEvent) > 10) {
newMatrix.set(matrix);
double newSpacing = spacing(motionEvent);
float scale = (float) newSpacing / (float) beginSpacing;
PointF pointF = midPoint(motionEvent);
newMatrix.postScale(scale, scale, pointF.x, pointF.y);
}
}
break;
case MotionEvent.ACTION_UP:
currentMode = MODE_NONE;
break;
}
imageView.setImageMatrix(newMatrix);
return true;
}
});
}
//计算两点之间的距离
private double spacing(MotionEvent event) {
float x = 0;
float y = 0;
try {
x = event.getX(0) - event.getX(1);
y = event.getY(0) - event.getY(1);
} catch (IllegalArgumentException e) {}
return Math.sqrt(x * x + y * y);
}
//计算中点位置
private PointF midPoint(MotionEvent event) {
PointF pointF = new PointF();
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
pointF.set(x / 2, y / 2);
return pointF;
}
}
0 0
- 图片缩放
- 图片缩放
- 图片缩放
- 图片缩放
- 图片缩放
- 缩放图片
- 缩放图片
- 图片缩放
- 图片缩放
- 图片缩放
- 缩放图片
- 缩放图片
- 图片缩放
- 图片缩放
- 图片缩放
- 缩放图片
- 图片缩放
- 图片缩放
- hotspot垃圾收集器简介
- 扩展模板库的新方法
- Python中的字符串序列拼接方法
- centos7下安装配置pptp
- 题目1105:字符串的反码
- 图片缩放
- 基础算法(一)---抽象数据类型之队列(先进先出)
- leetcode 301. Remove Invalid Parentheses
- [BZOJ 1007][HNOI2008]水平可见直线:单调栈
- hdu 1584 蜘蛛牌(bfs+set)
- 规律的作息
- 六、Intent和Activity详解
- 二叉树的前中后序非递归遍历算法实现
- 七、广播详解