下面列出了android.widget.ImageView#getMeasuredHeight ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 恢复
*
* @param img
*/
private void resumeRotation(ImageView img, boolean useAnim) {
int w = AndroidUtils.getWindowWidth(getActivity()), h = AndroidUtils.getWindowHeight(getActivity());
int iw = img.getMeasuredWidth(), ih = img.getMeasuredHeight();
if (useAnim) {
ObjectAnimator move = ObjectAnimator.ofFloat(img, "translationY", (h - ih) / 2f, 0);
move.setDuration(400);
ObjectAnimator scaleX = ObjectAnimator.ofFloat(img, "scaleX", (float) h / iw, 1.0f);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(img, "scaleY", (float) w / ih, 1.0f);
ObjectAnimator rotation = ObjectAnimator.ofFloat(img, "rotation", 90f, 0f);
AnimatorSet set = new AnimatorSet();
set.play(scaleX).with(scaleY).with(rotation).with(move);
set.setDuration(600);
set.start();
} else {
img.setTranslationY(0f);
img.setScaleX(1.0f);
img.setScaleY(1.0f);
img.setRotation(0f);
}
}
/**
* 通过jni进行图片模糊,一维高斯算法
* @param bkg 需要模糊的bitmap
* @param radius 模糊半径
* @param view 显示模糊图片的ImageView
* @return 消耗时间,单位毫秒(ms)
*/
public static long blurByJni_Gaussian(Bitmap bkg, int radius, ImageView view) {
long startMs = System.currentTimeMillis();
float scaleFactor = 8;
int width = (int)(view.getMeasuredWidth()/scaleFactor);
int height = (int)(view.getMeasuredHeight()/scaleFactor);
Bitmap overlay = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(overlay);
canvas.scale(1 / scaleFactor, 1 / scaleFactor);
Paint paint = new Paint();
paint.setFlags(Paint.FILTER_BITMAP_FLAG);
canvas.drawBitmap(bkg, 0, 0, paint);
int[] pixels = new int[width * height];
overlay.getPixels(pixels, 0, width, 0, 0, width, height);
int[] newPixels = toGaussianBlur(pixels,radius,width,height);
overlay.setPixels(newPixels, 0, width, 0, 0, width, height);
view.setImageBitmap(overlay);
return System.currentTimeMillis() - startMs;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
w = imageView.getMeasuredWidth();
h = imageView.getMeasuredHeight();
}
}
/**
* 初始化 singleLine 模式需要的视图
*
* @param widthMeasureSpec
* @param heightMeasureSpec
*/
private void initSingleLineView(int widthMeasureSpec, int heightMeasureSpec) {
if (!mSingleLine) {
return;
}
if (mShowRightImage) {
imageView = new ImageView(getContext());
imageView.setImageResource(mRightImageResId);
imageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
measureChild(imageView, widthMeasureSpec, heightMeasureSpec);
imageWidth = imageView.getMeasuredWidth();
imageHeight = imageView.getMeasuredHeight();
addView(imageView);
}
if (mShowEndText) {
endText = (TextView) mInflater.inflate(mTagResId, null);
if (mTagResId == DEFAULT_TAG_RESID) {
endText.setBackgroundResource(mBackground);
endText.setTextSize(TypedValue.COMPLEX_UNIT_SP, mTagSize);
endText.setTextColor(mTagColor);
}
@SuppressLint("DrawAllocation") LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
endText.setLayoutParams(layoutParams);
endText.setText(endTextString == null || endTextString.equals("") ? DEFAULT_END_TEXT_STRING : endTextString);
measureChild(endText, widthMeasureSpec, heightMeasureSpec);
endTextHeight = endText.getMeasuredHeight();
endTextWidth = endText.getMeasuredWidth();
addView(endText);
endText.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (onTagClickListener != null) {
onTagClickListener.onTagClick(-1);
}
}
});
}
}
public static boolean isBitmapTooLarge(float bw,float bh,ImageView imageView){
float bitmapArea = bw * bh;
float ivArea = imageView.getMeasuredWidth() * imageView.getMeasuredHeight();
if(ivArea ==0){
return false;
}
return (bitmapArea / ivArea) > 1.25f;
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
ImageView child = (ImageView) getChildAt(0);
if (child != null) {
float cl;
float cr;
if (mType == CircleIndicatorType.CIRTORECT) {
int offset = (mRectWidth - mSize) / 2;
cl = child.getLeft() - offset;
cr = cl + mRectWidth;
} else {
cl = child.getLeft();
cr = cl + child.getMeasuredWidth();
}
float ct = child.getTop();
float cb = ct + child.getMeasuredHeight();
mRect.set(cl, ct, cr, cb);
mMoveSize = mMargin + mSize;
int currentItem = mViewPager.getCurrentItem();
if (mType == CircleIndicatorType.SCALE) {
if (currentItem % mCount == 0) {
doScaleAnim(child, ANIM_OUT);
}
}
moveToPosition(mViewPager.getCurrentItem());
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
ImageView child = (ImageView) getChildAt(0);
if (child != null) {
float cl = child.getLeft();
float ct = child.getTop();
float cr = cl + child.getMeasuredWidth();
float cb = ct + child.getMeasuredHeight();
mRect.set(cl, ct, cr, cb);
mMoveSize = mMargin + mRectWidth;
moveToPosition(mViewPager.getCurrentItem());
}
}
static public void setPic(ImageView mImageView, String pPath) {
try {
if (pPath == null) return;
File f = new File(pPath);
if (!f.exists() || f.isDirectory()) return;
// Get the dimensions of the View
int targetW = mImageView.getWidth();
if (targetW == 0) targetW = mImageView.getMeasuredWidth();
int targetH = mImageView.getHeight();
if (targetH == 0) targetH = mImageView.getMeasuredHeight();
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(pPath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
// Determine how much to scale down the image
int scaleFactor = photoW / targetW; //Math.min(photoW/targetW, photoH/targetH);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(pPath, bmOptions);
Bitmap orientedBitmap = ExifUtil.rotateBitmap(pPath, bitmap);
mImageView.setImageBitmap(orientedBitmap);
//mImageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
mImageView.setAdjustViewBounds(true);
mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 通过jni进行图片模糊,均值模糊算法
* @param bkg 需要模糊的bitmap
* @param radius 模糊半径
* @param view 显示模糊图片的ImageView
* @return 消耗时间,单位毫秒(ms)
*/
public static long blurByJni_box(Bitmap bkg, int radius, ImageView view) {
long startMs = System.currentTimeMillis();
float scaleFactor = 8;
int width = (int)(view.getMeasuredWidth()/scaleFactor);
int height = (int)(view.getMeasuredHeight()/scaleFactor);
Bitmap overlay = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(overlay);
canvas.scale(1 / scaleFactor, 1 / scaleFactor);
Paint paint = new Paint();
paint.setFlags(Paint.FILTER_BITMAP_FLAG);
canvas.drawBitmap(bkg, 0, 0, paint);
int[] pixels = new int[width*height];
overlay.getPixels(pixels, 0, width, 0, 0, width, height);
int[] newPixels = new int[width*height];
toBoxBlur(pixels, newPixels, radius, width, height);
overlay.setPixels(newPixels, 0, width, 0, 0, width, height);
view.setImageBitmap(overlay);
return System.currentTimeMillis() - startMs;
}
/**
* 通过RenderScript进行图片模糊
* @param bkg 需要模糊的bitmap
* @param radius 模糊半径,RenderScript规定范围为[1,25]
* @param view 显示模糊图片的ImageView
* @param context 上下文
* @return 消耗时间,单位毫秒(ms)
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static long blurByRenderScript(Bitmap bkg,int radius, ImageView view,Context context)
{
long startMs = System.currentTimeMillis();
float scaleFactor = 8;
int width = (int)(view.getMeasuredWidth()/scaleFactor);
int height = (int)(view.getMeasuredHeight()/scaleFactor);
Bitmap overlay = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(overlay);
canvas.scale(1 / scaleFactor, 1 / scaleFactor);
Paint paint = new Paint();
paint.setFlags(Paint.FILTER_BITMAP_FLAG);
canvas.drawBitmap(bkg, 0, 0, paint);
RenderScript rs = RenderScript.create(context);
Allocation overlayAlloc = Allocation.createFromBitmap(rs, overlay);
ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, overlayAlloc.getElement());
blur.setInput(overlayAlloc);
blur.setRadius(radius);
blur.forEach(overlayAlloc);
overlayAlloc.copyTo(overlay);
view.setImageBitmap(overlay);
rs.destroy();
return System.currentTimeMillis() - startMs;
}
@Override
public void onImageReady(final File bitmapSourceFile, final BoxRequest request, final Bitmap bitmap, final ImageView view) {
if (bitmap == null || bitmapSourceFile == null || view == null){
ViewData.getImageLoadListener(view).onError();
return;
}
if(TYPE_REPRESENTATION.equals(ViewData.getImageType(view))) {
// No resizing for representation images
mController.getThumbnailCache().put(bitmapSourceFile, bitmap);
} else {
if (view.getMeasuredWidth() > 0 && view.getMeasuredHeight() > 0) {
Bitmap resizedBitmap = ThumbnailUtils.extractThumbnail(bitmap, view.getMeasuredWidth(), view.getMeasuredHeight());
mController.getThumbnailCache().put(bitmapSourceFile, resizedBitmap);
if (resizedBitmap != bitmap) {
bitmap.recycle();
}
} else {
postLaterToView(bitmapSourceFile, request, bitmap, view);
return;
}
}
Bitmap scaledBitmap = mController.getThumbnailCache().get(bitmapSourceFile);
if (scaledBitmap != null && isRequestStillApplicable(request, view)){
loadThumbnail(scaledBitmap, view);
}
}
@Override
protected void onPreExecute() {
super.onPreExecute();
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
w = imageView.getMeasuredWidth();
h = imageView.getMeasuredHeight();
}
}
private void setImageDrawable(final ImageView imageView, Bitmap bitmap, final JobOptions options, final LoadedFrom loadedFrom) {
final int targetWidth = imageView.getMeasuredWidth();
final int targetHeight = imageView.getMeasuredHeight();
if (targetWidth != 0 && targetHeight != 0) {
options.requestedWidth = targetWidth;
options.requestedHeight = targetHeight;
}
// Process the transformed (smaller) image
final BitmapProcessor processor = new BitmapProcessor(imageManager.getContext());
Bitmap processedBitmap = null;
if (options.roundedCorners)
processedBitmap = processor.getRoundedCorners(bitmap, options.radius);
else if (options.circle)
processedBitmap = processor.getCircle(bitmap);
if (processedBitmap != null)
bitmap = processedBitmap;
final Bitmap finalBitmap = bitmap;
uiHandler.post(new Runnable() {
@Override
public void run() {
CacheableDrawable.setBitmap(imageView, imageManager.getContext(), finalBitmap, loadedFrom, !options.fadeIn, true);
final ImageViewCallback imageViewCallback = imageManager.getImageViewCallback();
if (imageViewCallback != null)
imageViewCallback.onImageLoaded(imageView, finalBitmap);
}
});
}
private static void findImageViewInViewTree(View curNode,List<ImageViewInfo> imageList) {
if (curNode.getVisibility() != View.VISIBLE) {
return ;
}
if (curNode instanceof ViewGroup) {
ViewGroup curNodeGroup = (ViewGroup) curNode;
for (int i = 0; i < curNodeGroup.getChildCount(); i++) {
findImageViewInViewTree(curNodeGroup.getChildAt(i),imageList);
}
} else {
if (curNode instanceof ImageView) {
ImageViewInfo imageViewInfo = new ImageViewInfo();
ImageView curImage = (ImageView) curNode;
Drawable drawable = curImage.getDrawable();
if (drawable instanceof BitmapDrawable) {
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
imageViewInfo.imageHeight = bitmap.getHeight();
imageViewInfo.imageWidth = bitmap.getWidth();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //API 19
imageViewInfo.imageSize = bitmap.getAllocationByteCount();
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {//API 12
imageViewInfo.imageSize = bitmap.getByteCount();
} else {
imageViewInfo.imageSize = bitmap.getRowBytes() * bitmap.getHeight();
}
if(bitmap.getHeight() * bitmap.getWidth() > curImage.getMeasuredHeight() * curImage.getMeasuredWidth()){
imageViewInfo.imgViewInfo = MyUtil.printImageView(curImage);
imageList.add(imageViewInfo);
}
}else {
imageViewInfo.imageHeight = drawable.getIntrinsicHeight();
imageViewInfo.imageWidth = drawable.getIntrinsicWidth();
if(imageViewInfo.imageHeight * imageViewInfo.imageWidth > curImage.getMeasuredHeight() * curImage.getMeasuredWidth()){
imageViewInfo.imgViewInfo = MyUtil.printImageView(curImage);
imageList.add(imageViewInfo);
}
}
}
}
return ;
}
/**
* Loads the thumbnail for the provided BoxItem (if available) into the target image view
*
* @param item the item
* @param targetImage the target image
*/
public void loadThumbnail(final BoxItem item, final ImageView targetImage) {
boolean isMediaType = TYPE_MEDIA.equals(ViewData.getImageType(targetImage));
targetImage.setScaleType(ImageView.ScaleType.FIT_CENTER);
if (item instanceof BoxFile
&& item.getPermissions() != null
&& item.getPermissions().contains(BoxItem.Permission.CAN_PREVIEW)
&& isThumbnailAvailable(item)) {
// Cancel pending task upon recycle.
BoxFutureTask task = mTargetToTask.remove(targetImage);
if (task != null) {
task.cancel(false);
}
File thumbnailFile = getThumbnailForBoxFile((BoxFile) item);
if (mController.getThumbnailCache() != null && mController.getThumbnailCache().get(thumbnailFile) != null){
Bitmap bm = mController.getThumbnailCache().get(thumbnailFile);
targetImage.setImageBitmap(mController.getThumbnailCache().get(thumbnailFile));
return;
}
Bitmap placeHolderBitmap = null;
if (! isMediaType) {
int iconResId = getDefaultIconResource(item);
if (mController.getIconResourceCache() != null) {
placeHolderBitmap = mController.getIconResourceCache().get(iconResId);
}
if (placeHolderBitmap == null) {
if (targetImage.getMeasuredWidth() > 0 && targetImage.getMeasuredHeight() > 0) {
placeHolderBitmap = SdkUtils.decodeSampledBitmapFromFile(targetImage.getResources(), iconResId, targetImage.getMeasuredWidth(), targetImage.getMeasuredHeight());
} else {
placeHolderBitmap = BitmapFactory.decodeResource(targetImage.getResources(), iconResId);
}
if (mController.getIconResourceCache() != null) {
mController.getIconResourceCache().put(iconResId, placeHolderBitmap);
}
}
}
// Set the drawable to our loader drawable, which will show a placeholder before loading the thumbnail into the view
BoxRequestsFile.DownloadThumbnail request = mController.getThumbnailRequest(item.getId(), thumbnailFile);
LoaderDrawable loaderDrawable = LoaderDrawable.create(request, item, targetImage, placeHolderBitmap, this);
targetImage.setImageDrawable(loaderDrawable);
BoxFutureTask thumbnailTask = loaderDrawable.getTask();
if (thumbnailTask != null) {
mTargetToTask.put(targetImage, thumbnailTask);
mController.getThumbnailExecutor().execute(thumbnailTask);
}
} else {
if (!isMediaType) {
targetImage.setImageResource(getDefaultIconResource(item));
}
}
}