package com.practice.imageviewpic;
import android.app.Activity;
import android.content.Context;
import android.graphics.*;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
public class ImageViewPic extends Activity {
/*
* 利用多點觸控來控制ImageView中圖像的放大與縮小
* 手指控制圖片移動
*/
private MyImageView imageView;
private Bitmap bitmap;
//兩點觸屏後之間的長度
private float beforeLenght;
private float afterLenght;
//單點移動的先後座標值
private float afterX,afterY;
private float beforeX,beforeY;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
findView();
setContentView(imageView);
config();
}
private void findView() {
imageView = new MyImageView(this);
//得到圖片
bitmap = ((BitmapDrawable)getResources().getDrawable(R.drawable.xing)).getBitmap();
}
private void config() {
//設置imageView的顯示圖片
imageView.setImageBitmap(bitmap);
//設置圖片填充ImageView
imageView.setScaleType(ScaleType.FIT_XY);
}
//建立一個本身的ImageView類
class MyImageView extends ImageView {
private float scale = 0.1f;
public MyImageView(Context context) {
super(context);
}
//用來設置ImageView的位置
private void setLocation(int x,int y) {
this.setFrame(this.getLeft()+x, this.getTop()+y, this.getRight()+x, this.getBottom()+y);
}
/*
* 用來放大縮小ImageView
* 由於圖片是填充ImageView的,因此也就有放大縮小圖片的效果
* flag爲0是放大圖片,爲1是小於圖片
*/
private void setScale(float temp,int flag) {
if(flag==0) {
this.setFrame(this.getLeft()-(int)(temp*this.getWidth()),
this.getTop()-(int)(temp*this.getHeight()),
this.getRight()+(int)(temp*this.getWidth()),
this.getBottom()+(int)(temp*this.getHeight()));
}else {
this.setFrame(this.getLeft()+(int)(temp*this.getWidth()),
this.getTop()+(int)(temp*this.getHeight()),
this.getRight()-(int)(temp*this.getWidth()),
this.getBottom()-(int)(temp*this.getHeight()));
}
}
//繪製邊框
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Rect rec=canvas.getClipBounds();
rec.bottom--;
rec.right--;
Paint paint=new Paint();
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
canvas.drawRect(rec, paint);
}
/* 讓圖片跟隨手指觸屏的位置移動
* beforeX、Y是用來保存前一位置的座標
* afterX、Y是用來保存當前位置的座標
* 它們的差值就是ImageView各座標的增長或減小值
*/
public void moveWithFinger(MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
beforeX = event.getX();
beforeY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
afterX = event.getX();
afterY = event.getY();
this.setLocation((int)(afterX-beforeX),(int)(afterY-beforeY));
beforeX = afterX;
beforeY = afterY;
break;
case MotionEvent.ACTION_UP:
break;
}
}
/*
* 經過多點觸屏放大或縮小圖像
* beforeLenght用來保存前一時間兩點之間的距離
* afterLenght用來保存當前時間兩點之間的距離
*/
public void scaleWithFinger(MotionEvent event) {
float moveX = event.getX(1) - event.getX(0);
float moveY = event.getY(1) - event.getY(0);
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
beforeLenght = (float) Math.sqrt( (moveX*moveX) + (moveY*moveY) );
break;
case MotionEvent.ACTION_MOVE:
//獲得兩個點之間的長度
afterLenght = (float) Math.sqrt( (moveX*moveX) + (moveY*moveY) );
float gapLenght = afterLenght - beforeLenght;
if(gapLenght == 0) {
break;
}
//若是當前時間兩點距離大於前一時間兩點距離,則傳0,不然傳1
if(gapLenght>0) {
this.setScale(scale,0);
}else {
this.setScale(scale,1);
}
beforeLenght = afterLenght;
break;
}
}
}
//這裏來監聽屏幕觸控時間
@Override
public boolean onTouchEvent(MotionEvent event) {
/*
* 斷定用戶是否觸摸到了圖片
* 若是是單點觸摸則調用控制圖片移動的方法
* 若是是2點觸控則調用控制圖片大小的方法
*/
if(event.getY() > imageView.getTop() && event.getY() < imageView.getBottom()
&& event.getX() > imageView.getLeft() && event.getX() < imageView.getRight()) {
if(event.getPointerCount() == 2) {
imageView.scaleWithFinger(event);
}else if(event.getPointerCount() == 1) {
imageView.moveWithFinger(event);
}
}
return true;
}
}android