下面列出了android.widget.ImageView#getWidth ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void displayImage(ImageView view, String url) {
if(url == null) {
view.setImageResource(R.mipmap.ic_launcher);
return;
}
else if (TextUtils.isEmpty(url)) {//空图片显示
view.setImageResource(R.mipmap.ic_launcher);
return;
}
RequestCreator creator = imageLoader
.load(url)
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.config(Bitmap.Config.RGB_565);//不透明的图片使用减少内存
if (view.getWidth() == 0 && view.getHeight() == 0){
}
else {
creator.centerCrop()
.resize(view.getWidth(), view.getHeight());
}
creator.into(view);
}
@Override public boolean onPreDraw() {
ImageView target = this.target;
ViewTreeObserver vto = target.getViewTreeObserver();
if (!vto.isAlive()) {
return true;
}
int width = target.getWidth();
int height = target.getHeight();
if (width <= 0 || height <= 0) {
return true;
}
target.removeOnAttachStateChangeListener(this);
vto.removeOnPreDrawListener(this);
this.creator.unfit().resize(width, height).into(target, callback);
return true;
}
/**
* 根据当前的状态来旋转箭头。
*/
public static void rotateArrow(ImageView arrow, boolean flag) {
float pivotX = arrow.getWidth() / 2f;
float pivotY = arrow.getHeight() / 2f;
float fromDegrees = 0f;
float toDegrees = 0f;
// flag为true则向下
if (flag) {
fromDegrees = 0f;
toDegrees = 180f;
} else {
fromDegrees = 180f;
toDegrees = 0f;
}
//旋转动画效果 参数值 旋转的开始角度 旋转的结束角度 pivotX x轴伸缩值
RotateAnimation animation = new RotateAnimation(fromDegrees, toDegrees,
pivotX, pivotY);
//该方法用于设置动画的持续时间,以毫秒为单位
animation.setDuration(200);
//动画终止时停留在最后一帧
animation.setFillAfter(true);
//启动动画
arrow.startAnimation(animation);
}
/**
* {@inheritDoc}
* <p/>
* Width is defined by target {@link ImageView view} parameters, configuration
* parameters or device display dimensions.<br />
* Size computing algorithm:<br />
* 1) Get the actual drawn <b>getWidth()</b> of the View. If view haven't drawn yet then go
* to step #2.<br />
* 2) Get <b>layout_width</b>. If it hasn't exact value then go to step #3.<br />
* 3) Get <b>maxWidth</b>.
*/
@Override
public int getWidth() {
ImageView imageView = imageViewRef.get();
if (imageView != null) {
final ViewGroup.LayoutParams params = imageView.getLayoutParams();
int width = (params != null && params.width == ViewGroup.LayoutParams.WRAP_CONTENT) ? 0 :
imageView.getWidth(); // Get actual image width
if (width <= 0 && params != null) width = params.width; // Get layout width parameter
if (width <= 0) width = getImageViewFieldValue(imageView, "mMaxWidth"); // Check maxWidth parameter
//L.w("width = " + width);
return width;
}
return 0;
}
/**
* 根据ImageView获适当的压缩的宽和高
* @param imageView
* @return
*/
public static ImageSize getImageViewSize(ImageView imageView){
ViewGroup.LayoutParams lp=imageView.getLayoutParams();
DisplayMetrics displayMetrics= imageView.getContext().getResources().getDisplayMetrics();
int width=imageView.getWidth();
if(width<=0){
width=lp.width;
}
if(width<=0){
width=getImageViewFieltValue(imageView,"mMaxWidth");
}
if(width<=0){
width=displayMetrics.widthPixels;
}
int height=imageView.getHeight();
if(height<=0){
height=lp.height;
}
if(height<=0){
height=getImageViewFieltValue(imageView,"mMaxHeight");
}
if(height<=0){
height=displayMetrics.heightPixels;
}
ImageSize imageSize=new ImageSize();
imageSize.width=width;
imageSize.height=height;
return imageSize;
}
private void movePin(double x, double y, double z) {
// todo this is currently hard coded for 2D setups
final ImageView ImageView_BitmapView = findViewById(R.id.map);
final ImageView ImageView_Pin = findViewById(R.id.pin);
ImageView_Pin.setVisibility(View.VISIBLE);
// 0,0 point is 82.4427% down the image and 2.336% from the left of the image
float x_image_offset = (ImageView_BitmapView.getWidth() * 0.02336f);
float y_image_offset = (ImageView_BitmapView.getHeight() * 0.824427f);
x_image_offset = ImageView_BitmapView.getX() + x_image_offset;
y_image_offset = ImageView_BitmapView.getY() + y_image_offset;
float pin_width = ImageView_Pin.getWidth();
float pin_height = ImageView_Pin.getHeight();
float x_pin_offset = (pin_width / 2.0f);
float y_pin_offset = (pin_height) - (5.0f / 72.0f * pin_height); // There are a few pixels at the bottom of the pin
// Account for the fact that the Pin is pointing to the lower middle of the image view
float pinOriginX = x_image_offset - x_pin_offset;
float pinOriginY = y_image_offset - y_pin_offset;
//ImageView_Pin.setX(pinOriginX);
//ImageView_Pin.setY(pinOriginY);
float floorWidth = ImageView_BitmapView.getWidth() * (1772.0f / 3982.0f);
float floorHeight = ImageView_BitmapView.getHeight() * (1488.0f / 2096.0f);
float scaledX = (float) (x / 8370.0f * floorWidth);
float scaledY = (float) (y / 7000.0f * floorHeight);
ImageView_Pin.setX(pinOriginX + scaledX);
ImageView_Pin.setY(pinOriginY - scaledY);
}
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();
}
}
/**
* 根据ImageView获适当的压缩的宽和高
* @param imageView
* @return
*/
public static ImageSize getImageViewSize(ImageView imageView){
ViewGroup.LayoutParams lp=imageView.getLayoutParams();
DisplayMetrics displayMetrics= imageView.getContext().getResources().getDisplayMetrics();
int width=imageView.getWidth();
if(width<=0){
width=lp.width;
}
if(width<=0){
width=getImageViewFieltValue(imageView,"mMaxWidth");
}
if(width<=0){
width=displayMetrics.widthPixels;
}
int height=imageView.getHeight();
if(height<=0){
height=lp.height;
}
if(height<=0){
height=getImageViewFieltValue(imageView,"mMaxHeight");
}
if(height<=0){
height=displayMetrics.heightPixels;
}
ImageSize imageSize=new ImageSize();
imageSize.width=width;
imageSize.height=height;
return imageSize;
}
private int getImageViewWidth(ImageView imageView) {
if (null == imageView)
return 0;
return imageView.getWidth() - imageView.getPaddingLeft() - imageView.getPaddingRight();
}
private int getImageViewWidth(ImageView imageView) {
if (null == imageView)
return 0;
return imageView.getWidth() - imageView.getPaddingLeft() - imageView.getPaddingRight();
}
/**
* Asynchronously fulfills the request into the specified {@link ImageView} and invokes the
* target {@link Callback} if it's not {@code null}.
* <p>
* <em>Note:</em> The {@link Callback} param is a strong reference and will prevent your
* {@link android.app.Activity} or {@link android.app.Fragment} from being garbage collected. If
* you use this method, it is <b>strongly</b> recommended you invoke an adjacent
* {@link DokitPicasso#cancelRequest(android.widget.ImageView)} call to prevent temporary leaking.
*/
public void into(ImageView target, Callback callback) {
long started = System.nanoTime();
checkMain();
if (target == null) {
throw new IllegalArgumentException("Target must not be null.");
}
if (!data.hasImage()) {
picasso.cancelRequest(target);
if (setPlaceholder) {
setPlaceholder(target, getPlaceholderDrawable());
}
return;
}
if (deferred) {
if (data.hasSize()) {
throw new IllegalStateException("Fit cannot be used with resize.");
}
int width = target.getWidth();
int height = target.getHeight();
if (width == 0 || height == 0) {
if (setPlaceholder) {
setPlaceholder(target, getPlaceholderDrawable());
}
picasso.defer(target, new DeferredRequestCreator(this, target, callback));
return;
}
data.resize(width, height);
}
Request request = createRequest(started);
String requestKey = createKey(request);
if (shouldReadFromMemoryCache(memoryPolicy)) {
Bitmap bitmap = picasso.quickMemoryCacheCheck(requestKey);
if (bitmap != null) {
picasso.cancelRequest(target);
setBitmap(target, picasso.context, bitmap, MEMORY, noFade, picasso.indicatorsEnabled);
if (picasso.loggingEnabled) {
log(OWNER_MAIN, VERB_COMPLETED, request.plainId(), "from " + MEMORY);
}
if (callback != null) {
callback.onSuccess();
}
return;
}
}
if (setPlaceholder) {
setPlaceholder(target, getPlaceholderDrawable());
}
Action action =
new ImageViewAction(picasso, target, request, memoryPolicy, networkPolicy, errorResId,
errorDrawable, requestKey, tag, callback, noFade);
picasso.enqueueAndSubmit(action);
}
private void checkMatrixBounds() {
final ImageView imageView = getImageView();
if (null == imageView) {
return;
}
final RectF rect = getDisplayRect(getDisplayMatrix());
if (null == rect) {
return;
}
final float height = rect.height(), width = rect.width();
float deltaX = 0, deltaY = 0;
final int viewHeight = imageView.getHeight();
if (height <= viewHeight) {
switch (mScaleType) {
case FIT_START:
deltaY = -rect.top;
break;
case FIT_END:
deltaY = viewHeight - height - rect.top;
break;
default:
deltaY = (viewHeight - height) / 2 - rect.top;
break;
}
} else if (rect.top > 0) {
deltaY = -rect.top;
} else if (rect.bottom < viewHeight) {
deltaY = viewHeight - rect.bottom;
}
final int viewWidth = imageView.getWidth();
if (width <= viewWidth) {
switch (mScaleType) {
case FIT_START:
deltaX = -rect.left;
break;
case FIT_END:
deltaX = viewWidth - width - rect.left;
break;
default:
deltaX = (viewWidth - width) / 2 - rect.left;
break;
}
mScrollEdge = EDGE_BOTH;
} else if (rect.left > 0) {
mScrollEdge = EDGE_LEFT;
deltaX = -rect.left;
} else if (rect.right < viewWidth) {
deltaX = viewWidth - rect.right;
mScrollEdge = EDGE_RIGHT;
} else {
mScrollEdge = EDGE_NONE;
}
// Finally actually translate the matrix
mSuppMatrix.postTranslate(deltaX, deltaY);
}
private int getImageViewWidth(ImageView imageView) {
if (null == imageView)
return 0;
return imageView.getWidth() - imageView.getPaddingLeft() - imageView.getPaddingRight();
}
private int getImageViewWidth(ImageView imageView) {
if (null == imageView)
return 0;
return imageView.getWidth() - imageView.getPaddingLeft() - imageView.getPaddingRight();
}
private void checkMatrixBounds() {
final ImageView imageView = getImageView();
if (null == imageView) {
return;
}
final RectF rect = getDisplayRect(getDisplayMatrix());
if (null == rect) {
return;
}
final float height = rect.height(), width = rect.width();
float deltaX = 0, deltaY = 0;
final int viewHeight = imageView.getHeight();
if (height <= viewHeight) {
switch (mScaleType) {
case FIT_START:
deltaY = -rect.top;
break;
case FIT_END:
deltaY = viewHeight - height - rect.top;
break;
default:
deltaY = (viewHeight - height) / 2 - rect.top;
break;
}
} else if (rect.top > 0) {
deltaY = -rect.top;
} else if (rect.bottom < viewHeight) {
deltaY = viewHeight - rect.bottom;
}
final int viewWidth = imageView.getWidth();
if (width <= viewWidth) {
switch (mScaleType) {
case FIT_START:
deltaX = -rect.left;
break;
case FIT_END:
deltaX = viewWidth - width - rect.left;
break;
default:
deltaX = (viewWidth - width) / 2 - rect.left;
break;
}
mScrollEdge = EDGE_BOTH;
} else if (rect.left > 0) {
mScrollEdge = EDGE_LEFT;
deltaX = -rect.left;
} else if (rect.right < viewWidth) {
deltaX = viewWidth - rect.right;
mScrollEdge = EDGE_RIGHT;
} else {
mScrollEdge = EDGE_NONE;
}
// Finally actually translate the matrix
mSuppMatrix.postTranslate(deltaX, deltaY);
}
private int getImageViewWidth(ImageView imageView) {
return imageView.getWidth() - imageView.getPaddingLeft() - imageView.getPaddingRight();
}
private void checkMatrixBounds() {
final ImageView imageView = getImageView();
if (null == imageView) {
return;
}
final RectF rect = getDisplayRect(getDisplayMatrix());
if (null == rect) {
return;
}
final float height = rect.height(), width = rect.width();
float deltaX = 0, deltaY = 0;
final int viewHeight = imageView.getHeight();
if (height <= viewHeight) {
switch (mScaleType) {
case FIT_START:
deltaY = -rect.top;
break;
case FIT_END:
deltaY = viewHeight - height - rect.top;
break;
default:
deltaY = (viewHeight - height) / 2 - rect.top;
break;
}
} else if (rect.top > 0) {
deltaY = -rect.top;
} else if (rect.bottom < viewHeight) {
deltaY = viewHeight - rect.bottom;
}
final int viewWidth = imageView.getWidth();
if (width <= viewWidth) {
switch (mScaleType) {
case FIT_START:
deltaX = -rect.left;
break;
case FIT_END:
deltaX = viewWidth - width - rect.left;
break;
default:
deltaX = (viewWidth - width) / 2 - rect.left;
break;
}
mScrollEdge = EDGE_BOTH;
} else if (rect.left > 0) {
mScrollEdge = EDGE_LEFT;
deltaX = -rect.left;
} else if (rect.right < viewWidth) {
deltaX = viewWidth - rect.right;
mScrollEdge = EDGE_RIGHT;
} else {
mScrollEdge = EDGE_NONE;
}
// Finally actually translate the matrix
mSuppMatrix.postTranslate(deltaX, deltaY);
}
private int getImageViewWidth(ImageView imageView) {
if (null == imageView) return 0;
return imageView.getWidth() - imageView.getPaddingLeft() - imageView.getPaddingRight();
}
/**
* 根据ImageView获适当的压缩的宽和高
*
* @param imageView
* @return
*/
public static ImageSize getImageViewSize(ImageView imageView)
{
ImageSize imageSize = new ImageSize();
DisplayMetrics displayMetrics = imageView.getContext().getResources()
.getDisplayMetrics();
LayoutParams lp = imageView.getLayoutParams();
int width = imageView.getWidth();// 获取imageview的实际宽度
if (width <= 0)
{
width = lp.width;// 获取imageview在layout中声明的宽度
}
if (width <= 0)
{
//width = imageView.getMaxWidth();// 检查最大值
width = getImageViewFieldValue(imageView, "mMaxWidth");
}
if (width <= 0)
{
width = displayMetrics.widthPixels;
}
int height = imageView.getHeight();// 获取imageview的实际高度
if (height <= 0)
{
height = lp.height;// 获取imageview在layout中声明的宽度
}
if (height <= 0)
{
height = getImageViewFieldValue(imageView, "mMaxHeight");// 检查最大值
}
if (height <= 0)
{
height = displayMetrics.heightPixels;
}
imageSize.width = width;
imageSize.height = height;
return imageSize;
}
private int getImageViewWidth(ImageView imageView) {
if (null == imageView)
return 0;
return imageView.getWidth() - imageView.getPaddingLeft() - imageView.getPaddingRight();
}