事先申明,此例子有个缺陷,图片放缩速度较慢,因为每次放缩都会自动画一张图片,而不是现在流行了设置ImageView的Matrix放缩控件大小。不过因人而异,也许您会有所需求的。
public static ImageView ImageTouchSet(final ImageView imageview,RelativeLayout Imgbg){
ImageView image = null;
matrix.postScale(2.0f, 2.0f,50,50);
Imgbg.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()&MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
savedMatrix.set(matrix);
System.out.println("测试");
start.set(event.getX(), event.getY());
mode = MOVE;
rotate = NONE;
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
break;
case MotionEvent.ACTION_POINTER_DOWN:
//System.out.println("测试");
oldDistance = (float)Math.sqrt((event.getX(0)-event.getX(1))*(event.getX(0)-event.getX(1))+(event.getY(0)-event.getY(1))*(event.getY(0)-event.getY(1)));
if (oldDistance > 10f) {
savedMatrix.set(matrix);
mid.set((event.getX(0)+event.getX(1))/2, (event.getY(0)+event.getY(1))/2);
mode = ZOOM;
}
case MotionEvent.ACTION_MOVE:
//System.out.println("Move");
if (mode == MOVE)
{
System.out.println("move");
if(rotate == NONE) {
savedMatrix.set(matrix);
System.out.println("moveNone");
mid.set(event.getX(), event.getY());
rotate = ROTATION;
}
else {
System.out.println("move移动");
matrix.set(savedMatrix);
double a = Math.atan((mid.y-start.y)/(mid.x-start.x));
double b = Math.atan((event.getY()-mid.y)/(event.getX()-mid.x));
if ((b - a < Math.PI/2 && b - a > Math.PI / 18)||((b + Math.PI) % Math.PI - a < Math.PI/2 && (b + Math.PI) % Math.PI - a > Math.PI / 18)) {
matrix.postScale((float)0.9, (float)0.9);
//matrix.setScale(0.9, 0.9);
System.out.println("多点触控1");
}
else if ((a - b < Math.PI / 2 && a - b > Math.PI / 18)||((a + Math.PI) % Math.PI - b < Math.PI/2 && (a + Math.PI) % Math.PI - b > Math.PI / 18)) {
matrix.postScale((float)1.1, (float)1.1);
System.out.println("多点触控2");
}
//System.out.println(event.getX()+"*"+event.getY());
System.out.println(a-b);
start.set(event.getX(), event.getY());
rotate = NONE;
}
}
else if(mode == ZOOM)
{
float newDistance;
newDistance = (float)Math.sqrt((event.getX(0)-event.getX(1))*(event.getX(0)-event.getX(1))+(event.getY(0)-event.getY(1))*(event.getY(0)-event.getY(1)));
if(newDistance > 10f) {
matrix.set(savedMatrix);
matrix.postScale(newDistance/oldDistance, newDistance/oldDistance, mid.x, mid.y);
oldDistance = newDistance;
savedMatrix.set(matrix);
//System.out.println("多点触控3");
}
}
break;
}
imageview.setScaleType(ScaleType.MATRIX);
//imageview.setScaleType(ScaleType.CENTER_INSIDE);
System.out.println(matrix);
imageview.setImageMatrix(matrix);
//System.out.println(matrix);
//ImageTouch.drawImage(imageview,matrix);
return true;
}
});
return imageview;
}
/**
* 绘制一个Bitmap
* @param canvas 画布
* @param bitmap 图片
* @param x 屏幕上的x坐标
* @param y 屏幕上的y坐标
*/
public static void drawImage(ImageView imageview,Matrix matrix)
{
/* 绘制图像 */
//imageview.setDrawingCacheEnabled(true);
//BitMap = Bitmap.createBitmap(imageview.getDrawingCache());
if(BitMap == null){
//BitMap=BitmapFactory.decodeFile("/sdcard/FlyBird/girl/1.jpg");
BitMap = getThumbnails(400,1000);
}
Bitwidth = BitMap.getWidth();
Bitheight = BitMap.getHeight();
//System.out.println(Bitwidth + "*" +displayWidth + "***" + Bitheight +"*"+displayHeight);
float[] array = {0,0,0,0,0,0,0,0,0};
matrix.getValues(array);
System.out.println(array[0]+"*"+array[1]+"*"+array[2]+"***"+matrix);
if(array[0] <= 1.5){
//imageview.setDrawingCacheEnabled(false);
Bitmap resizedBitmap = Bitmap.createBitmap(BitMap, 0, 0,Bitwidth, Bitheight, matrix, true);
imageview.setImageBitmap(resizedBitmap);
}else{
matrix.setValues(max_matrix);
}
//return imageview;
}
/**
* 得到所有照片的缩略图
* @param minSideLength
* @param maxNumOfPixels
* @return
*/
public static Bitmap getThumbnails(int minSideLength, int maxNumOfPixels){
//Bitmap mBitmap = new Bitmap[;
//int i=0;
//Map<String,String> map = (Map<String, String>);
String photo_url = "/sdcard/FlyBird/girl/1.jpg";
//mBitmap[i++] = BitmapFactory.decodeFile(photo_url);
try{
FileDescriptor fd = new FileInputStream(photo_url).getFD();
Options opts = new Options ();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeFileDescriptor(fd, null, opts);
opts.inSampleSize = computeSampleSize(opts, minSideLength, maxNumOfPixels);
try{
opts.inJustDecodeBounds = false;
BitMap = BitmapFactory.decodeFile(photo_url,opts);
}catch(Exception e){
e.printStackTrace();
}
}catch(Exception e){
e.printStackTrace();
}
return BitMap;
}
/**
* 动态优化bitmap
* @param opts
* @param minSideLength
* @param maxNumOfPixels
* @return
*/
private static int computeSampleSize(Options opts, int minSideLength, int maxNumOfPixels){
int initialSize = computeInitialSize(opts, minSideLength, maxNumOfPixels);
int roundedSize;
if(initialSize <= 8){
roundedSize = 1;
while(roundedSize < initialSize){
roundedSize <<= 1;
}
}else{
roundedSize = (initialSize+7)/(8*8);
}
return roundedSize;
}
private static int computeInitialSize(Options opts, int minSideLength, int maxNumOfPixels){
double width = opts.outWidth;
double height = opts.outHeight;
int lowerBound = (maxNumOfPixels == -1)? 1 : (int)Math.ceil(Math.sqrt(width*height/maxNumOfPixels));
int upperBound = (minSideLength == -1) ? 128 : (int)Math.min(Math.floor(width/minSideLength), Math.floor(height/minSideLength));
if(lowerBound<upperBound){
return lowerBound;
}
if((minSideLength==-1) && (maxNumOfPixels==-1)){
return 1;
}else if(minSideLength == -1){
return lowerBound;
}else{
return upperBound;
}
}