本文所需要实现的就是这样一种有逼格的效果:
右上角加了个图片框,按下确定可以裁剪正方形区域里的图片并显示在右上角。
实现思路:
1:首先需要自定义一个ZoomImageView来显示我们需要的图片,这个View需要让图片能够以合适的位置展现在当前布局的图片展示区域内(合适的位置值的是:如果图片长度大于屏幕,则压缩图片长度至屏幕宽度,高度等比压缩并居中显示,如果图片高度大于屏幕,则压缩图片高度至屏幕高度,长度等比压缩并居中显示。);
2:然后需要实现这个拖动的框框,该框框实现的功能有四点:拖动、扩大缩小、触摸时显示基准线、截图。
首先是布局设计image_details.xml:
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="#323441">
android:id="@+id/certification_returnbtn"
android:layout_width="55dp"
android:layout_height="55dp"
android:background="@android:color/transparent"
android:padding="15dp"
android:scaleType="fitCenter"
android:src="@drawable/topbar_returnbtn"/>
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@id/certification_returnbtn"
android:gravity="center_vertical"
android:text="裁剪图片"
android:textColor="@android:color/white"
android:textSize="20sp"/>
android:id="@+id/testimg"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_width="55dp"
android:layout_height="55dp"/>
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
android:id="@+id/zoom_image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#303030"/>
android:id="@+id/zoom_choiceborder_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
android:id="@+id/image_details_subbtn"
android:text="确定"
android:background="@drawable/image_details_submitbtn_shape"
android:layout_marginBottom="20dp"
android:layout_width="180dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
布局比较简单,两个View互相层叠。
自定义图片大小控制视图:ZoomImageView.java
代码注释很详细就不解释了。
package com.whale.nangua.pubuliuzhaopianqiang;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
/**
* Created by nangua on 2016/7/20.
*/
public class ZoomImageView extends View {
/**
* 初始化状态常量
*/
public static final int STATUS_INIT = 1;
/**
* 用于对图片进行移动和缩放变换的矩阵
*/
private Matrix matrix = new Matrix();
/**
* 待展示的Bitmap对象
*/
private Bitmap sourceBitmap;
/**
* 记录当前操作的状态,可选值为STATUS_INIT、STATUS_ZOOM_OUT、STATUS_ZOOM_IN和STATUS_MOVE
*/
private int currentStatus;
/**
* ZoomImageView控件的宽度
*/
private int width;
/**
* ZoomImageView控件的高度
*/
private int height;
/**
* ZoomImageView构造函数,将当前操作状态设为STATUS_INIT。
*
* @param context
* @param attrs
*/
public ZoomImageView(Context context,AttributeSet attrs) {
super(context,attrs);
currentStatus = STATUS_INIT;
}
/**
* 将待展示的图片设置进来。
*
* @param bitmap 待展示的Bitmap对象
*/
public void setImageBitmap(Bitmap bitmap) {
sourceBitmap = bitmap;
invalidate();
}
@Override
protected void onLayout(boolean changed,int left,int top,int right,int bottom) {
super.onLayout(changed,left,top,right,bottom);
if (changed) {
// 分别获取到ZoomImageView的宽度和高度
width = getWidth();
height = getHeight();
}
}
/**
* 根据currentStatus的值来决定对图片进行什么样的绘制操作。
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
initBitmap(canvas);
canvas.drawBitmap(sourceBitmap,matrix,null);
}
float translateY;//Y轴偏移量
float translateX;//X轴偏移量
/**
* @param canvas
* @autohr nangua
* 对图片进行初始化操作,包括让图片居中,以及当图片大于屏幕宽高时对图片进行压缩。
*
* 1.当图片宽度大于显示宽度、图片高度小于显示宽度:
* 设置图片宽度为显示宽度,高度缩放*(图片宽度/显示宽度)
*
* 2.当图片宽度小于显示宽度、图片高度大于显示宽度&