public class ClipImageBorder extends View {
//水平,垂直方向与View的边距
private int mHorizontalPadding = 20,mVerticalpadding;
//绘制的矩形的宽度
private int mWidth;
//边框的颜色,默认为白色
private int mBorderColor = Color.parseColor("#ffffff");
//边框的宽度,单位dp
private int mBorderWidth = 1;
private Paint mPaint;
public ClipImageBorder(Context context) {
this(context,null,0);
}
public ClipImageBorder(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public ClipImageBorder(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//计算padding的px
mHorizontalPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mHorizontalPadding, getResources().getDisplayMetrics());
mBorderWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mBorderWidth, getResources().getDisplayMetrics());
mPaint = new Paint();
mPaint.setAntiAlias(true);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//计算矩形区域的宽度
mWidth = getWidth() - 2*mHorizontalPadding;
//计算距离屏幕垂直边界的边距
mVerticalpadding = (getHeight() - mWidth)/2;
mPaint.setColor(Color.parseColor("#aa000000"));
mPaint.setStyle(Paint.Style.FILL);
//绘制左边
canvas.drawRect(0, 0, mHorizontalPadding, getHeight(), mPaint);
//绘制右边
canvas.drawRect(getWidth() - mHorizontalPadding,0,getWidth(),getHeight(),mPaint);
//绘制上边
canvas.drawRect(mHorizontalPadding,0,getWidth() - mHorizontalPadding,mVerticalpadding,mPaint);
//绘制下边
canvas.drawRect(mHorizontalPadding,getHeight() - mVerticalpadding,getWidth() - mHorizontalPadding,getHeight(),mPaint);
//绘制外边框
mPaint.setColor(mBorderColor);
mPaint.setStrokeWidth(mBorderWidth);
mPaint.setStyle(Paint.Style.STROKE);
canvas.drawRect(mHorizontalPadding,mVerticalpadding,getWidth()-mHorizontalPadding,getHeight()-mVerticalpadding,mPaint);
}
public void setHorizontalPadding(int mHorizontalPadding) {
this.mHorizontalPadding = mHorizontalPadding;
}
/**
* 剪切图片,返回剪切后的bitmap对象
*
* @return
*/
public Bitmap clip() {
Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
draw(canvas);
return Bitmap.createBitmap(bitmap, mHorizontalPadding,
mVerticalpadding, getWidth() - 2 * mHorizontalPadding,
getWidth() - 2 * mHorizontalPadding);
}
}
two
public class zoomImageView extends ImageView implements
OnScaleGestureListener, OnTouchListener,
ViewTreeObserver.OnGlobalLayoutListener {
public static float SCALE_MAX = 4.0f;
private static float SCALE_MID = 2.0f;
/**
* 初始化时的缩放比例,如果图片宽或高大于屏幕,此值将小于0
*/
private float initScale = 1.0f;
private boolean once = true;
/**
* 用于存放矩阵的9个值
*/
private final float[] matrixValues = new float[9];
/**
* 缩放的手势检测
*/
private ScaleGestureDetector mScaleGestureDetector = null;
private final Matrix mScaleMatrix = new Matrix();
/**
* 用于双击检测
*/
private GestureDetector mGestureDetector;
private boolean isAutoScale;
private int mTouchSlop;
private float mLastX;
private float mLastY;
private boolean isCanDrag;
private int lastPointerCount;
public zoomImageView(Context context) {
this(context, null);
}
public zoomImageView(Context context, AttributeSet attrs) {
super(context, attrs);
setScaleType(ScaleType.MATRIX);
mGestureDetector = new GestureDetector(context,
new SimpleOnGestureListener() {
@Override
public boolean onDoubleTap(MotionEvent e) {
if (isAutoScale == true)
return true;
float x = e.getX();
float y = e.getY();
if (getScale() < SCALE_MID) {
zoomImageView.this.postDelayed(
new AutoScaleRunnable(SCALE_MID, x, y), 16);
isAutoScale = true;
} else {
zoomImageView.this.postDelayed(
new AutoScaleRunnable(initScale, x, y), 16);
isAutoScale = true;
}
return true;
}
});
mScaleGestureDetector = new ScaleGestureDetector(context, this);
this.setOnTouchListener(this);
}
/**
* 自动缩放的任务
*
* @author zhy
*/
private class AutoScaleRunnable implements Runnable {
static final float BIGGER = 1.07f;
static final float SMALLER = 0.93f;
private float mTargetScale;
private float tmpScale;
/**
* 缩放的中心
*/
private float x;
private float y;
/**
* 传入目标缩放值,根据目标值与当前值,判断应该放大还是缩小
*
* @param targetScale
*/
public AutoScaleRunnable(float targetScale, float x, float y) {
this.mTargetScale = targetScale;
this.x = x;
this.y = y;
if (getScale() < mTargetScale) {
tmpScale = BIGGER;
} else {
tmpScale = SMALLER;
}
}
@Override
public void run() {
// 进行缩放
mScaleMatrix.postScale(tmpScale, tmpScale, x, y);
checkBorder();
setImageMatrix(mScaleMatrix);
final float currentScale = getScale();
// 如果值在合法范围内,继续缩放
if (((tmpScale > 1f) && (currentScale < mTargetScale))
|| ((tmpScale < 1f) && (mTargetScale < currentScale))) {
zoomImageView.this.postDelayed(this, 16);
} else
// 设置为目标的缩放比例
{
final float deltaScale = mTargetScale / currentScale;
mScaleMatrix.postScale(deltaScale, deltaScale, x, y);
checkBorder();
setImageMatrix(mScaleMatrix);
isAutoScale = false;
}
}
}
three
public class ClipImageLayout extends RelativeLayout {
private zoomImageView mZoomImageView;
private ClipImageBorder mClipImageView;
private int mHorizontalPadding = 20;
public ClipImageLayout(Context context, AttributeSet attrs) {
super(context, attrs);
mZoomImageView = new zoomImageView(context);
mClipImageView = new ClipImageBorder(context);
LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
mZoomImageView.setImageDrawable(getResources().getDrawable(R.mipmap.a));
this.addView(mZoomImageView, lp);
this.addView(mClipImageView,lp);
//计算padding的px
mHorizontalPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mHorizontalPadding, getResources().getDisplayMetrics());
mClipImageView.setHorizontalPadding(mHorizontalPadding);
mZoomImageView.setHorizontalPadding(mHorizontalPadding);
}
//对外公布设置边距的方法,单位dp
public void setHorizontalPadding(int mHorizontalPadding){
this.mHorizontalPadding = mHorizontalPadding;
}
//裁切图片
public Bitmap clip(){
return mZoomImageView.clip();
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.testapplication.Main2Activity"
>
<com.example.administrator.testapplication.ClipImageLayout
android:id="@+id/clipImgeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</com.example.administrator.testapplication.ClipImageLayout>
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
/>
</RelativeLayout>
menu布局
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/action_clip"
android:icon="@mipmap/ic_action_github"
android:showAsAction="always|withText"
android:title="裁剪"
/>
</menu>
public class Main2Activity extends Activity {
private ClipImageLayout clipImageBorder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main2);
clipImageBorder = (ClipImageLayout) findViewById(R.id.clipImgeLayout);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.action_clip:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Bitmap bitmap = clipImageBorder.clip();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100,baos);
byte[] datas = baos.toByteArray();
setContentView(R.layout.show_image);
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Bitmap bitmap1 = BitmapFactory.decodeByteArray(datas, 0, datas.length);
if(bitmap1 != null){
imageView.setImageBitmap(bitmap1);
imageView.setVisibility(View.VISIBLE);
}
break;
}
return super.onOptionsItemSelected(item);
}
}