下面列出了android.widget.ImageView#measure ( ) 实例代码,或者点击链接到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);
}
/**
* 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;
}
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);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
mScrollView = (ScrollView) findViewById(R.id.scroll_view);
mContent = (CellLayout) findViewById(R.id.folder_content);
int measureSpec = MeasureSpec.UNSPECIFIED;
LauncherAppState app = LauncherAppState.getInstance();
DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
mContent.setCellDimensions(grid.folderCellWidthPx, grid.folderCellHeightPx);
mContent.setGridSize(0, 0);
mContent.getShortcutsAndWidgets().setMotionEventSplittingEnabled(false);
mContent.setInvertIfRtl(true);
mFolderName = (FolderEditText) findViewById(R.id.folder_name);
mFolderName.setFolder(this);
mFolderName.setOnFocusChangeListener(this);
// We find out how tall the text view wants to be (it is set to wrap_content), so that
// we can allocate the appropriate amount of space for it.
mFolderName.measure(measureSpec, measureSpec);
mFolderNameHeight = mFolderName.getMeasuredHeight();
// We disable action mode for now since it messes up the view on phones
mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
mFolderName.setOnEditorActionListener(this);
mFolderName.setSelectAllOnFocus(true);
mFolderName.setInputType(mFolderName.getInputType() |
InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
mAutoScrollHelper = new FolderAutoScrollHelper(mScrollView);
if (SettingsProvider.getBoolean(mLauncher,
SettingsProvider.SETTINGS_UI_HOMESCREEN_HIDE_ICON_LABELS,
R.bool.preferences_interface_homescreen_hide_icon_labels_default)) {
mFolderName.setVisibility(View.GONE);
mFolderNameHeight = getPaddingBottom();
}
mFolderLock = (ImageView) findViewById(R.id.folder_lock);
mFolderTitleSection = (RelativeLayout) findViewById(R.id.folder_title_section);
mFolderLock.measure(measureSpec, measureSpec);
mFolderLock.setOnClickListener(this);
mFolderTitleSection.measure(measureSpec, measureSpec);
}
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);
}