|
[li]/**[/li][li]* 图片浏览、缩放、拖动、自动居中[/li][li]*/[/li][li]public class Touch extends Activity implements OnTouchListener {[/li][li][/li][li] Matrix matrix = new Matrix();[/li][li] Matrix savedMatrix = new Matrix();[/li][li] DisplayMetrics dm;[/li][li] ImageView imgView;[/li][li] Bitmap bitmap;[/li][li][/li][li] float minScaleR;// 最小缩放比例[/li][li] static final float MAX_SCALE = 4f;// 最大缩放比例[/li][li][/li][li] static final int NONE = 0;// 初始状态[/li][li] static final int DRAG = 1;// 拖动[/li][li] static final int ZOOM = 2;// 缩放[/li][li] int mode = NONE;[/li][li][/li][li] PointF prev = new PointF();[/li][li] PointF mid = new PointF();[/li][li] float dist = 1f;[/li][li][/li][li] @Override[/li][li] public void onCreate(Bundle savedInstanceState) {[/li][li] super.onCreate(savedInstanceState);[/li][li] setContentView(R.layout.scale);[/li][li] imgView = (ImageView) findViewById(R.id.imag);// 获取控件[/li][li] bitmap = BitmapFactory.decodeResource(getResources(), this.getIntent()[/li][li] .getExtras().getInt("IMG"));// 获取图片资源[/li][li] imgView.setImageBitmap(bitmap);// 填充控件[/li][li] imgView.setOnTouchListener(this);// 设置触屏监听[/li][li] dm = new DisplayMetrics();[/li][li] getWindowManager().getDefaultDisplay().getMetrics(dm);// 获取分辨率[/li][li] minZoom();[/li][li] center();[/li][li] imgView.setImageMatrix(matrix);[/li][li] }[/li][li][/li][li] /**[/li][li] * 触屏监听[/li][li] */[/li][li] public boolean onTouch(View v, MotionEvent event) {[/li][li][/li][li] switch (event.getAction() & MotionEvent.ACTION_MASK) {[/li][li] // 主点按下[/li][li] case MotionEvent.ACTION_DOWN:[/li][li] savedMatrix.set(matrix);[/li][li] prev.set(event.getX(), event.getY());[/li][li] mode = DRAG;[/li][li] break;[/li][li] // 副点按下[/li][li] case MotionEvent.ACTION_POINTER_DOWN:[/li][li] dist = spacing(event);[/li][li] // 如果连续两点距离大于10,则判定为多点模式[/li][li] if (spacing(event) > 10f) {[/li][li] savedMatrix.set(matrix);[/li][li] midPoint(mid, event);[/li][li] mode = ZOOM;[/li][li] }[/li][li] break;[/li][li] case MotionEvent.ACTION_UP:[/li][li] case MotionEvent.ACTION_POINTER_UP:[/li][li] mode = NONE;[/li][li] break;[/li][li] case MotionEvent.ACTION_MOVE:[/li][li] if (mode == DRAG) {[/li][li] matrix.set(savedMatrix);[/li][li] matrix.postTranslate(event.getX() - prev.x, event.getY()[/li][li] - prev.y);[/li][li] } else if (mode == ZOOM) {[/li][li] float newDist = spacing(event);[/li][li] if (newDist > 10f) {[/li][li] matrix.set(savedMatrix);[/li][li] float tScale = newDist / dist;[/li][li] matrix.postScale(tScale, tScale, mid.x, mid.y);[/li][li] }[/li][li] }[/li][li] break;[/li][li] }[/li][li] imgView.setImageMatrix(matrix);[/li][li] CheckView();[/li][li] return true;[/li][li] }[/li][li][/li][li] /**[/li][li] * 限制最大最小缩放比例,自动居中[/li][li] */[/li][li] private void CheckView() {[/li][li] float p[] = new float[9];[/li][li] matrix.getValues(p);[/li][li] if (mode == ZOOM) {[/li][li] if (p[0] < minScaleR) {[/li][li] matrix.setScale(minScaleR, minScaleR);[/li][li] }[/li][li] if (p[0] > MAX_SCALE) {[/li][li] matrix.set(savedMatrix);[/li][li] }[/li][li] }[/li][li] center();[/li][li] }[/li][li][/li][li] /**[/li][li] * 最小缩放比例,最大为100%[/li][li] */[/li][li] private void minZoom() {[/li][li] minScaleR = Math.min([/li][li] (float) dm.widthPixels / (float) bitmap.getWidth(),[/li][li] (float) dm.heightPixels / (float) bitmap.getHeight());[/li][li] if (minScaleR < 1.0) {[/li][li] matrix.postScale(minScaleR, minScaleR);[/li][li] }[/li][li] }[/li][li][/li][li] private void center() {[/li][li] center(true, true);[/li][li] }[/li][li][/li][li] /**[/li][li] * 横向、纵向居中[/li][li] */[/li][li] protected void center(boolean horizontal, boolean vertical) {[/li][li][/li][li] Matrix m = new Matrix();[/li][li] m.set(matrix);[/li][li] RectF rect = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());[/li][li] m.mapRect(rect);[/li][li][/li][li] float height = rect.height();[/li][li] float width = rect.width();[/li][li][/li][li] float deltaX = 0, deltaY = 0;[/li][li][/li][li] if (vertical) {[/li][li] // 图片小于屏幕大小,则居中显示。大于屏幕,上方留空则往上移,下放留空则往下移[/li][li] int screenHeight = dm.heightPixels;[/li][li] if (height < screenHeight) {[/li][li] deltaY = (screenHeight - height) / 2 - rect.top;[/li][li] } else if (rect.top > 0) {[/li][li] deltaY = -rect.top;[/li][li] } else if (rect.bottom < screenHeight) {[/li][li] deltaY = imgView.getHeight() - rect.bottom;[/li][li] }[/li][li] }[/li][li][/li][li] if (horizontal) {[/li][li] int screenWidth = dm.widthPixels;[/li][li] if (width < screenWidth) {[/li][li] deltaX = (screenWidth - width) / 2 - rect.left;[/li][li] } else if (rect.left > 0) {[/li][li] deltaX = -rect.left;[/li][li] } else if (rect.right < screenWidth) {[/li][li] deltaX = screenWidth - rect.right;[/li][li] }[/li][li] }[/li][li] matrix.postTranslate(deltaX, deltaY);[/li][li] }[/li][li][/li][li] /**[/li][li] * 两点的距离[/li][li] */[/li][li] private float spacing(MotionEvent event) {[/li][li] float x = event.getX(0) - event.getX(1);[/li][li] float y = event.getY(0) - event.getY(1);[/li][li] return FloatMath.sqrt(x * x + y * y);[/li][li] }[/li][li][/li][li] /**[/li][li] * 两点的中点[/li][li] */[/li][li] private void midPoint(PointF point, MotionEvent event) {[/li][li] float x = event.getX(0) + event.getX(1);[/li][li] float y = event.getY(0) + event.getY(1);[/li][li] point.set(x / 2, y / 2);[/li][li] }[/li][li]}
[/li]
复制代码
scale.xml
[li]<?xml version="1.0" encoding="utf-8"?>[/li][li]<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"[/li][li] android:layout_width="fill_parent"[/li][li] android:layout_height="fill_parent"[/li][li] android:layout_gravity="center" >[/li][li][/li][li] <ImageView[/li][li] android:id="@+id/imag"[/li][li] android:layout_width="fill_parent"[/li][li] android:layout_height="fill_parent"[/li][li] android:layout_gravity="center"[/li][li] android:scaleType="matrix" >[/li][li] </ImageView>[/li][li][/li][li]</FrameLayout>
[/li] |
|