package com.example.administrator.text.widget;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.media.ThumbnailUtils;
import android.os.Environment;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Timer;
import java.util.TimerTask;
/**
* Created by Administrator on 2016/9/28.
*/
public class ScaleImageView extends android.support.v7.widget.AppCompatImageView {
public static final int SCALE = 2;
public static final int DRAG = 1;
public static final int NONE = 0;
public int mode = NONE;
//縮放比率
public static final float MAX = 3f;
public static final int MIN = 1;
Bitmap bmp;
//屏幕寬度
int width;
//
PointF moveFinger = new PointF();
PointF scaleCenter = new PointF();
Matrix martix;
float twoDis = 0;
//畫筆
Paint paint;
//java代碼創建
public ScaleImageView(Context context) {
super(context);
init();
}
//xml中創建
public ScaleImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}
public void init() {
setBackgroundColor(Color.rgb(0x31, 0x33, 0x35));
//很重要的方法
setScaleType(ScaleType.MATRIX);
width = getResources().getDisplayMetrics().widthPixels;
//設置畫筆
paint = new Paint();
paint.setAntiAlias(true);
paint.setDither(true);
paint.setColor(Color.argb(0x80, 0XFF, 0XFF, 0XFF));
paint.setStrokeWidth(2);
}
boolean isShowLine = false;
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setARGB(0X80, 0XFF, 0XFF, 0XFF);
if (isShowLine) {
for (int i = 0; i < 2; i++) {
canvas.drawLine(width / 3 * (i + 1), 0, width / 3 * (i + 1), width, paint);
canvas.drawLine(0, width / 3 * (i + 1), width, width / 3 * (i + 1), paint);
}
}
}
//獲取圖片
public String getScaleBitmap() {
Bitmap bmp = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
paint.setARGB(0xFF, 0XFF, 0XFF, 0XFF);
canvas.drawBitmap(this.bmp, martix, paint);
//上傳 文件
String name = "";
try {
name = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), System.currentTimeMillis() + ".png").getAbsolutePath();
FileOutputStream fio = new FileOutputStream(name);
bmp.compress(Bitmap.CompressFormat.PNG, 100, fio);
fio.flush();
fio.close();
} catch (Exception e) {
e.printStackTrace();
}
return name;
/* //獲取當前圖片
//Bitmap src = ((BitmapDrawable) getDrawable()).getBitmap();
//獲取圖片的位置
RectF rectf = new RectF(0, 0, bmp.getWidth(), bmp.getHeight());
martix.mapRect(rectf);
//最終的取的圖片部分
Rect rectsrc = new Rect(-(int) rectf.left, - (int) rectf.top, -(int) rectf.left+width, - (int) rectf.top+width);
Log.e("TAG", rectsrc.toString());
Log.e("TAG", "-----------" + (int) rectf.left + " " + (int) rectf.top + " " + (int) rectf.right + " " + (int) rectf.bottom);
//目標的圖片部分
Rect rectdst = new Rect(0, 0, width, width);
canvas.drawBitmap(this.bmp, rectsrc, rectdst, paint);
canvas.save();
// bmp.recycle();
try {
bmp.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath() + "/temp.png"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}*/
}
//設置圖片
@Override
public void setImageBitmap(Bitmap bm) {
if (bm == null)
return;
//圖片矩陣運算
martix = new Matrix();
//放大到鋪滿全屏
//判斷圖片的高寬是否大於width
if (bm.getWidth() < width || bm.getHeight() < width) {
//取最小值
int min = Math.min(bm.getWidth(), bm.getHeight());
//放大多少呢?
float rate = width * 1.0f / min;
// 放大
this.bmp = ThumbnailUtils.extractThumbnail(bm, (int) (bm.getWidth() * rate), (int) (bm.getHeight() * rate));
} else
this.bmp = bm;
setImageMatrix(martix);
super.setImageBitmap(bmp);
}
Timer timer;
@Override
public boolean onTouchEvent(MotionEvent event) {
isShowLine = true;
if (timer != null)
timer.cancel();
//如果需要拿多指觸碰
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
//按下的時候,記住你的手指頭
moveFinger.set(event.getX(), event.getY());
mode = DRAG;
break;
case MotionEvent.ACTION_POINTER_DOWN:
//記錄放大的中心點
//設置中心店
scaleCenter = centerPoint(event);
mode = SCALE;
twoDis = twoMove(event);
break;
case MotionEvent.ACTION_UP:
mode = NONE;
//位置復位
reSet();
//關閉線條
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
isShowLine = false;
//子線程重繪
postInvalidate();
}
}, 1000);
break;
case MotionEvent.ACTION_POINTER_UP:
mode = DRAG;
//縮放復位
reScaleSet();
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
//拖拽
PointF pointf = calcDistance(event);
//x y 移動的距離
martix.postTranslate(pointf.x, pointf.y);
//記錄移動後的點
moveFinger.set(event.getX(), event.getY());
} else if (mode == SCALE) {
//當前的距離
float dis = twoMove(event) / twoDis;
martix.postScale(dis, dis, scaleCenter.x, scaleCenter.y);
}
break;
}
setImageMatrix(martix);
return true;
}
private void reScaleSet() {
float values[] = new float[9];
martix.getValues(values);
Log.e("TAG", "------縮放" + values[0]);
if (values[0] < MIN) {
values[0] = MIN;
values[4] = MIN;
}
if (values[0] > MAX) {
values[0] = MAX;
values[4] = MAX;
}
martix.setValues(values);
Bitmap src = ((BitmapDrawable) getDrawable()).getBitmap();
Log.e("TAG", src.getWidth() + " " + src.getHeight());
}
//復位
public void reSet() {
RectF rectf = new RectF(0, 0, bmp.getWidth(), bmp.getHeight());
//計算當前圖片在矩陣的位置
martix.mapRect(rectf);
Log.e("TAG", rectf.left + " " + rectf.right + " " + rectf.top + " " + rectf.bottom);
float x = 0;
float y = 0;
//水平方向
if (rectf.left > 0) {
x = -rectf.left;
} else if (rectf.right < width) {
x = width - rectf.right;
}
//垂直方向
if (rectf.top > 0)
y = -rectf.top;
else if (rectf.bottom < width)
y = width - rectf.bottom;
martix.postTranslate(x, y);
}
//計算兩點之間的距離
public PointF calcDistance(MotionEvent event) {
float x = event.getX();
float y = event.getY();
PointF pointf = new PointF(x - moveFinger.x, y - moveFinger.y);
return pointf;
}
public PointF centerPoint(MotionEvent event) {
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
return new PointF(x / 2, y / 2);
}
//兩點移動距離
public float twoMove(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return x * x + y * y;
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
getParent().requestDisallowInterceptTouchEvent(true);
return super.dispatchTouchEvent(event);
}
}
用法:
1.複製代碼到工程目錄下
2.xml中直接加入下面代碼就好
<com.example.administrator.text.widget.ScaleImageView
android:id="@+id/siv_img"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
3.