Bitmap 裁剪图片周边透明区域
//方法1:
public static Bitmap trimBitmap(Bitmap bitmap) {
int top = 0
int left = 0
int right = bitmap.getWidth() - 1
int bottom = 0
for (int w = bitmap.getWidth() / 2
int h = 0
for (h = 0
int color = bitmap.getPixel(w, h)
if (color != 0) {
if (top == 0) {
top = h
}
top = Math.min(top, h)
break
}
}
if (h >= bitmap.getHeight() - 1) {
right = w
break
}
}
for (int w = bitmap.getWidth() / 2 - 1
int h = 0
for (h = 0
int color = bitmap.getPixel(w, h)
if (color != 0) {
if (top == 0) {
top = h
}
top = Math.min(top, h)
break
}
}
if (h >= bitmap.getHeight() - 1) {
left = w
break
}
}
for (int w = left
for (int h = bitmap.getHeight() - 1
int color = bitmap.getPixel(w, h)
if (color != 0) {
bottom = Math.max(bottom, h)
break
}
}
}
//对边界值做一次判断,保证严谨
int x = (int) Math.max(left , 0f)
int y = (int) Math.max(top , 0f)
int w = (int) Math.min(right - left , bitmap.getWidth() - x)
int h = (int) Math.min(bottom - top , bitmap.getHeight() - y)
if (x + w > bitmap.getWidth()) {
x = 0
w = bitmap.getWidth()
}
if (y + h > bitmap.getHeight()) {
y = 0
h = bitmap.getHeight()
}
return Bitmap.createBitmap(bitmap, x, y, w, h)
}
//方法2:
public static Bitmap TrimBitmap(Bitmap bmp) {
int imgHeight = bmp.getHeight()
int imgWidth = bmp.getWidth()
//TRIM WIDTH - LEFT
int startWidth = 0
for (int x = 0
if (startWidth == 0) {
for (int y = 0
if (bmp.getPixel(x, y) != Color.TRANSPARENT) {
startWidth = x
break
}
}
} else break
}
//TRIM WIDTH - RIGHT
int endWidth = 0
for (int x = imgWidth - 1
if (endWidth == 0) {
for (int y = 0
if (bmp.getPixel(x, y) != Color.TRANSPARENT) {
endWidth = x
break
}
}
} else break
}
//TRIM HEIGHT - TOP
int startHeight = 0
for (int y = 0
if (startHeight == 0) {
for (int x = 0
if (bmp.getPixel(x, y) != Color.TRANSPARENT) {
startHeight = y
break
}
}
} else break
}
//TRIM HEIGHT - BOTTOM
int endHeight = 0
for (int y = imgHeight - 1
if (endHeight == 0) {
for (int x = 0
if (bmp.getPixel(x, y) != Color.TRANSPARENT) {
endHeight = y
break
}
}
} else break
}
return Bitmap.createBitmap(bmp, startWidth, startHeight, endWidth - startWidth, endHeight - startHeight)
}
Matrix 获取当前旋转角度值和当前缩放值
private float[] matrixValues = new float[9];
private float getMatrixValue(@NonNull Matrix matrix, @IntRange(from = 0, to = 9) int valueIndex) {
matrix.getValues(matrixValues);
return matrixValues[valueIndex];
}
private float getMatrixAngle(@NonNull Matrix matrix) {
return (float) -(Math.atan2(getMatrixValue(matrix, Matrix.MSKEW_X),
getMatrixValue(matrix, Matrix.MSCALE_X)) * (180 / Math.PI));
}
private float getMatrixScale(@NonNull Matrix matrix) {
return (float) Math.sqrt(Math.pow(getMatrixValue(matrix, Matrix.MSCALE_X), 2) + Math.pow(
getMatrixValue(matrix, Matrix.MSKEW_Y), 2));
}
AndroidBug5497Workaround
import android.app.Activity
import android.graphics.Rect
import android.os.Build
import android.view.View
import android.view.ViewTreeObserver
import android.widget.FrameLayout
public class AndroidBug5497Workaround {
public static void assistActivity(Activity activity) {
new AndroidBug5497Workaround(activity)
}
private View mChildOfContent
private int usableHeightPrevious
private FrameLayout.LayoutParams frameLayoutParams
private int contentHeight
private boolean isfirst = true
private Activity activity
private int statusBarHeight
private AndroidBug5497Workaround(Activity activity) {
//获取状态栏的高度
int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android")
statusBarHeight = activity.getResources().getDimensionPixelSize(resourceId)+5
this.activity = activity
FrameLayout content = (FrameLayout)activity.findViewById(android.R.id.content)
mChildOfContent = content.getChildAt(0)
//界面出现变动都会调用这个监听事件
mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
if (isfirst) {
contentHeight = mChildOfContent.getHeight()
isfirst = false
}
possiblyResizeChildOfContent()
}
})
frameLayoutParams = (FrameLayout.LayoutParams)
mChildOfContent.getLayoutParams()
}
//重新调整跟布局的高度
private void possiblyResizeChildOfContent() {
int usableHeightNow = computeUsableHeight()
//当前可见高度和上一次可见高度不一致 布局变动
if (usableHeightNow != usableHeightPrevious) {
//int usableHeightSansKeyboard2 = mChildOfContent.getHeight()
int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight()
int heightDifference = usableHeightSansKeyboard - usableHeightNow
if (heightDifference > (usableHeightSansKeyboard / 4)) {
// keyboard probably just became visible
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
//frameLayoutParams.height = usableHeightSansKeyboard - heightDifference
frameLayoutParams.height = usableHeightSansKeyboard - heightDifference + statusBarHeight
} else {
frameLayoutParams.height = usableHeightSansKeyboard -heightDifference
}
} else {
frameLayoutParams.height = contentHeight
}
mChildOfContent.requestLayout()
usableHeightPrevious = usableHeightNow
}
}
/**
* 计算mChildOfContent可见高度
* @return
*/
private int computeUsableHeight() {
Rect r = new Rect()
mChildOfContent.getWindowVisibleDisplayFrame(r)
return (r.bottom - r.top)
}
}
开发收集...