下面列出了android.widget.ImageView#layout ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Creates a View using the bitmap copy of <code>view</code>. If <code>view</code> is large,
* the copy will use a scaled bitmap of the given view.
*
* @param sceneRoot The ViewGroup in which the view copy will be displayed.
* @param view The view to create a copy of.
* @param parent The parent of view.
*/
public static View copyViewImage(ViewGroup sceneRoot, View view, View parent) {
Matrix matrix = new Matrix();
matrix.setTranslate(-parent.getScrollX(), -parent.getScrollY());
view.transformMatrixToGlobal(matrix);
sceneRoot.transformMatrixToLocal(matrix);
RectF bounds = new RectF(0, 0, view.getWidth(), view.getHeight());
matrix.mapRect(bounds);
int left = Math.round(bounds.left);
int top = Math.round(bounds.top);
int right = Math.round(bounds.right);
int bottom = Math.round(bounds.bottom);
ImageView copy = new ImageView(view.getContext());
copy.setScaleType(ImageView.ScaleType.CENTER_CROP);
Bitmap bitmap = createViewBitmap(view, matrix, bounds, sceneRoot);
if (bitmap != null) {
copy.setImageBitmap(bitmap);
}
int widthSpec = View.MeasureSpec.makeMeasureSpec(right - left, View.MeasureSpec.EXACTLY);
int heightSpec = View.MeasureSpec.makeMeasureSpec(bottom - top, View.MeasureSpec.EXACTLY);
copy.measure(widthSpec, heightSpec);
copy.layout(left, top, right, bottom);
return copy;
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout contentView = new LinearLayout(this);
ImageView imageView0 = new ImageView(this);
imageView0.setImageBitmap(ShadowBitmapFactory.create("AndroidGodEye-Bitmap", null, new Point(200, 100)));
ImageView imageView1 = new ImageView(this);
imageView1.setImageBitmap(ShadowBitmapFactory.create("AndroidGodEye-Bitmap", null, new Point(200, 100)));
ImageView imageView2 = new ImageView(this);
imageView2.setImageBitmap(ShadowBitmapFactory.create("AndroidGodEye-Bitmap", null, new Point(200, 100)));
imageView3 = new ImageView(this);
imageView3.setImageBitmap(ShadowBitmapFactory.create("AndroidGodEye-Bitmap", null, new Point(200, 100)));
contentView.addView(imageView0);
contentView.addView(imageView1);
contentView.addView(imageView2);
contentView.addView(imageView3);
setContentView(contentView);
imageView0.measure(50, 50);
imageView0.layout(0, 0, 50, 50);
imageView1.measure(500, 500);
imageView1.layout(0, 0, 500, 500);
imageView2.measure(210, 95);
imageView2.layout(0, 0, 210, 95);
imageView3.measure(50, 50);
imageView3.layout(0, 0, 50, 50);
}
private void layoutForNoSpanChildrenView(int childrenCount) {
if (childrenCount <= 0) return;
int row, column, left, top, right, bottom;
for (int i = 0; i < childrenCount; i++) {
ImageView childrenView = (ImageView) getChildAt(i);
row = i / mColumnCount;
column = i % mColumnCount;
left = (mGridSize + mGap) * column + getPaddingLeft();
top = (mGridSize + mGap) * row + getPaddingTop();
right = left + mGridSize;
bottom = top + mGridSize;
childrenView.layout(left, top, right, bottom);
if (mAdapter != null) {
mAdapter.onDisplayImage(getContext(), childrenView, mImgDataList.get(i));
}
}
}
/**
* Drag the text out of current view.
* @param text The dragged text.
* @param shadowImage The shadow image for the dragged text.
*/
@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.N)
@CalledByNative
private boolean startDragAndDrop(String text, Bitmap shadowImage) {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) return false;
ViewGroup containerView = getContainerView();
if (containerView == null) return false;
ImageView imageView = new ImageView(containerView.getContext());
imageView.setImageBitmap(shadowImage);
imageView.layout(0, 0, shadowImage.getWidth(), shadowImage.getHeight());
return containerView.startDragAndDrop(ClipData.newPlainText(null, text),
new View.DragShadowBuilder(imageView), null, View.DRAG_FLAG_GLOBAL);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
ImageView imageView = (ImageView) getChildAt(0);
// Resize the image view to be at least mZoomLength pixels larger in both
// dimensions than the containing view.
int imageWidth = imageView.getDrawable().getIntrinsicWidth();
int imageHeight = imageView.getDrawable().getIntrinsicHeight();
int minSize = Math.max(right - left, bottom - top) + mZoomLength;
if (imageWidth > imageHeight) {
imageWidth = minSize * imageWidth / imageHeight;
imageHeight = minSize;
} else {
imageHeight = minSize * imageHeight / imageWidth;
imageWidth = minSize;
}
imageView.layout(left, top, left + imageWidth, top + imageHeight);
}
/**
* Creates a View using the bitmap copy of <code>view</code>. If <code>view</code> is large,
* the copy will use a scaled bitmap of the given view.
*
* @param sceneRoot The ViewGroup in which the view copy will be displayed.
* @param view The view to create a copy of.
* @param parent The parent of view
*/
@NonNull
public static View copyViewImage(@NonNull ViewGroup sceneRoot, @NonNull View view, @NonNull View parent) {
Matrix matrix = new Matrix();
matrix.setTranslate(-parent.getScrollX(), -parent.getScrollY());
ViewUtils.transformMatrixToGlobal(view, matrix);
ViewUtils.transformMatrixToLocal(sceneRoot, matrix);
RectF bounds = new RectF(0, 0, view.getWidth(), view.getHeight());
matrix.mapRect(bounds);
int left = Math.round(bounds.left);
int top = Math.round(bounds.top);
int right = Math.round(bounds.right);
int bottom = Math.round(bounds.bottom);
ImageView copy = new ImageView(view.getContext());
copy.setScaleType(ImageView.ScaleType.CENTER_CROP);
Bitmap bitmap = createViewBitmap(view, matrix, bounds);
if (bitmap != null) {
copy.setImageBitmap(bitmap);
}
int widthSpec = View.MeasureSpec.makeMeasureSpec(right - left, View.MeasureSpec.EXACTLY);
int heightSpec = View.MeasureSpec.makeMeasureSpec(bottom - top, View.MeasureSpec.EXACTLY);
copy.measure(widthSpec, heightSpec);
copy.layout(left, top, right, bottom);
return copy;
}
void layoutImage(int i, int left, int top, int right, int bottom) {
final ImageView view = imageViews[i];
if (view.getLeft() == left && view.getTop() == top && view.getRight() == right
&& view.getBottom() == bottom) {
return;
}
view.layout(left, top, right, bottom);
}
void layoutImage(int i, int left, int top, int right, int bottom) {
final ImageView view = imageViews[i];
if (view.getLeft() == left && view.getTop() == top && view.getRight() == right
&& view.getBottom() == bottom) {
return;
}
view.layout(left, top, right, bottom);
}
protected void workGetImage(ImageView imageView) {
imageView.setDrawingCacheEnabled(true);
imageView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
imageView.layout(0, 0, imageView.getMeasuredWidth(), imageView.getMeasuredHeight());
imageView.buildDrawingCache(true);
output_bitmap = Bitmap.createBitmap(imageView.getDrawingCache());
imageView.setDrawingCacheEnabled(false);
}
private void layoutForThreeChildrenView(int childrenCount) {
int left, top, right, bottom;
for (int i = 0; i < childrenCount; i++) {
ImageView childrenView = (ImageView) getChildAt(i);
switch (mSpanType) {
case TOPCOLSPAN: //2行2列,首行跨列
if (i == 0) {
left = getPaddingLeft();
top = getPaddingTop();
right = left + mGridSize * 2 + mGap;
bottom = top + mGridSize;
} else if (i == 1) {
left = getPaddingLeft();
top = getPaddingTop() + mGridSize + mGap;
right = left + mGridSize;
bottom = top + mGridSize;
} else {
left = getPaddingLeft() + mGridSize + mGap;
top = getPaddingTop() + mGridSize + mGap;
right = left + mGridSize;
bottom = top + mGridSize;
}
childrenView.layout(left, top, right, bottom);
break;
case BOTTOMCOLSPAN: //2行2列,末行跨列
if (i == 0) {
left = getPaddingLeft();
top = getPaddingTop();
right = left + mGridSize;
bottom = top + mGridSize;
} else if (i == 1) {
left = getPaddingLeft() + mGridSize + mGap;
top = getPaddingTop();
right = left + mGridSize;
bottom = top + mGridSize;
} else {
left = getPaddingLeft();
top = getPaddingTop() + mGridSize + mGap;
right = left + mGridSize * 2 + mGap;
bottom = top + mGridSize;
}
childrenView.layout(left, top, right, bottom);
break;
case LEFTROWSPAN: //2行2列,首列跨行
if (i == 0) {
left = getPaddingLeft();
top = getPaddingTop();
right = left + mGridSize;
bottom = top + mGridSize * 2 + mGap;
} else if (i == 1) {
left = getPaddingLeft() + mGridSize + mGap;
top = getPaddingTop();
right = left + mGridSize;
bottom = top + mGridSize;
} else {
left = getPaddingLeft() + mGridSize + mGap;
top = getPaddingTop() + mGridSize + mGap;
right = left + mGridSize;
bottom = top + mGridSize;
}
childrenView.layout(left, top, right, bottom);
break;
default:
break;
}
if (mAdapter != null) {
mAdapter.onDisplayImage(getContext(), childrenView, mImgDataList.get(i));
}
}
}
private void layoutForFourChildrenView(int childrenCount) {
int left, top, right, bottom;
for (int i = 0; i < childrenCount; i++) {
ImageView childrenView = (ImageView) getChildAt(i);
switch (mSpanType) {
case TOPCOLSPAN: //3行3列,首行跨2行3列
if (i == 0) {
left = getPaddingLeft();
top = getPaddingTop();
right = left + mGridSize * 3 + mGap * 2;
bottom = top + mGridSize * 2 + mGap;
} else if (i == 1) {
left = getPaddingLeft();
top = getPaddingTop() + mGridSize * 2 + mGap * 2;
right = left + mGridSize;
bottom = top + mGridSize;
} else if (i == 2) {
left = getPaddingLeft() + mGridSize + mGap;
top = getPaddingTop() + mGridSize * 2 + mGap * 2;
right = left + mGridSize;
bottom = top + mGridSize;
} else {
left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
top = getPaddingTop() + mGridSize * 2 + mGap * 2;
right = left + mGridSize;
bottom = top + mGridSize;
}
childrenView.layout(left, top, right, bottom);
break;
case BOTTOMCOLSPAN: //3行3列,末行跨2行3列
if (i == 0) {
left = getPaddingLeft();
top = getPaddingTop();
right = left + mGridSize;
bottom = top + mGridSize;
} else if (i == 1) {
left = getPaddingLeft() + mGridSize + mGap;
top = getPaddingTop();
right = left + mGridSize;
bottom = top + mGridSize;
} else if (i == 2) {
left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
top = getPaddingTop();
right = left + mGridSize;
bottom = top + mGridSize;
} else {
left = getPaddingLeft();
top = getPaddingTop() + mGridSize + mGap;
right = left + mGridSize * 3 + mGap * 2;
bottom = top + mGridSize * 2 + mGap;
}
childrenView.layout(left, top, right, bottom);
break;
case LEFTROWSPAN: //3行3列,首列跨3行2列
if (i == 0) {
left = getPaddingLeft();
top = getPaddingTop();
right = left + mGridSize * 2 + mGap;
bottom = top + mGridSize * 3 + mGap * 2;
} else if (i == 1) {
left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
top = getPaddingTop();
right = left + mGridSize;
bottom = top + mGridSize;
} else if (i == 2) {
left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
top = getPaddingTop() + mGridSize + mGap;
right = left + mGridSize;
bottom = top + mGridSize;
} else {
left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
top = getPaddingTop() + mGridSize * 2 + mGap * 2;
right = left + mGridSize;
bottom = top + mGridSize;
}
childrenView.layout(left, top, right, bottom);
break;
default:
break;
}
if (mAdapter != null) {
mAdapter.onDisplayImage(getContext(), childrenView, mImgDataList.get(i));
}
}
}
private void layoutForFiveChildrenView(int childrenCount) {
int left, top, right, bottom;
for (int i = 0; i < childrenCount; i++) {
ImageView childrenView = (ImageView) getChildAt(i);
switch (mSpanType) {
case TOPCOLSPAN: //3行3列,首行跨2行,2列跨3列
if (i == 0) {
left = getPaddingLeft();
top = getPaddingTop();
right = left + (mGridSize * 3 + mGap) / 2;
bottom = top + mGridSize * 2 + mGap;
} else if (i == 1) {
left = getPaddingLeft() + (mGridSize * 3 + mGap) / 2 + mGap;
top = getPaddingTop();
right = left + (mGridSize * 3 + mGap) / 2;
bottom = top + mGridSize * 2 + mGap;
} else if (i == 2) {
left = getPaddingLeft();
top = getPaddingTop() + mGridSize * 2 + mGap * 2;
right = left + mGridSize;
bottom = top + mGridSize;
} else if (i == 3) {
left = getPaddingLeft() + mGridSize + mGap;
top = getPaddingTop() + mGridSize * 2 + mGap * 2;
right = left + mGridSize;
bottom = top + mGridSize;
} else {
left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
top = getPaddingTop() + mGridSize * 2 + mGap * 2;
right = left + mGridSize;
bottom = top + mGridSize;
}
childrenView.layout(left, top, right, bottom);
break;
case BOTTOMCOLSPAN: //3行3列,末行跨2行,2列跨3列
if (i == 0) {
left = getPaddingLeft();
top = getPaddingTop();
right = left + mGridSize;
bottom = top + mGridSize;
} else if (i == 1) {
left = getPaddingLeft() + mGridSize + mGap;
top = getPaddingTop();
right = left + mGridSize;
bottom = top + mGridSize;
} else if (i == 2) {
left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
top = getPaddingTop();
right = left + mGridSize;
bottom = top + mGridSize;
} else if (i == 3) {
left = getPaddingLeft();
top = getPaddingTop() + mGridSize + mGap;
right = left + (mGridSize * 3 + mGap) / 2;
bottom = top + mGridSize * 2 + mGap;
} else {
left = getPaddingLeft() + (mGridSize * 3 + mGap) / 2 + mGap;
top = getPaddingTop() + mGridSize + mGap;
right = left + (mGridSize * 3 + mGap) / 2;
bottom = top + mGridSize * 2 + mGap;
}
childrenView.layout(left, top, right, bottom);
break;
case LEFTROWSPAN: //3行3列,2行跨3行,1列
if (i == 0) {
left = getPaddingLeft();
top = getPaddingTop();
right = left + mGridSize * 2 + mGap;
bottom = top + (mGridSize * 3 + mGap) / 2;
} else if (i == 1) {
left = getPaddingLeft();
top = getPaddingTop() + (mGridSize * 3 + mGap) / 2 + mGap;
right = left + mGridSize * 2 + mGap;
bottom = top + (mGridSize * 3 + mGap) / 2;
} else if (i == 2) {
left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
top = getPaddingTop();
right = left + mGridSize;
bottom = top + mGridSize;
} else if (i == 3) {
left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
top = getPaddingTop() + mGridSize + mGap;
right = left + mGridSize;
bottom = top + mGridSize;
} else {
left = getPaddingLeft() + mGridSize * 2 + mGap * 2;
top = getPaddingTop() + mGridSize * 2 + mGap * 2;
right = left + mGridSize;
bottom = top + mGridSize;
}
childrenView.layout(left, top, right, bottom);
break;
default:
break;
}
if (mAdapter != null) {
mAdapter.onDisplayImage(getContext(), childrenView, mImgDataList.get(i));
}
}
}
private void addShadow(ImageView imageView, int l, int t, int r, int b) {
imageView.layout(l, t, r, b);
addView(imageView);
}
private void addShadow(ImageView imageView, int l, int t, int r, int b) {
imageView.layout(l, t, r, b);
addView(imageView);
}
private void createRoundedBitmap()
{
if (mBitmap == null)
return;
int targetWidth = getWidth();
int targetHeight = getHeight();
if (targetWidth == 0 || targetHeight == 0)
return;
Bitmap targetBitmap = Bitmap.createBitmap(
targetWidth,
targetHeight,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(targetBitmap);
// Adapted from
// http://stackoverflow.com/questions/1705239/how-should-i-give-images-rounded-corners-in-android
Bitmap rounder = Bitmap.createBitmap(targetWidth, targetHeight,
Bitmap.Config.ARGB_8888);
Canvas canvasRounder = new Canvas(rounder);
Paint xferPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
xferPaint.setColor(Color.RED);
canvasRounder.drawRoundRect(new RectF(0, 0, targetWidth, targetHeight),
mRadius, mRadius, xferPaint);
xferPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
// Create a private image view to do scaling stuff. Saves us a lot of tricky calculations
// to achieve "CenterCrop" scale type... =)
ImageView ivCopy = new ImageView(this.getContext());
ivCopy.setScaleType(ScaleType.CENTER_CROP);
ivCopy.setImageBitmap(mBitmap);
ivCopy.measure(MeasureSpec.makeMeasureSpec(targetWidth, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(targetHeight, MeasureSpec.EXACTLY));
ivCopy.layout(0, 0, targetWidth, targetHeight);
canvas.save();
ivCopy.draw(canvas);
canvas.restore();
canvas.drawBitmap(rounder, 0, 0, xferPaint);
super.setImageBitmap(targetBitmap);
}