下面列出了怎么用android.widget.ImageView.ScaleType的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 初始化数据
*/
private void initData(){
//定义一个布局并设置参数
LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
//初始化引导图片列表
ImageView iv0 = new ImageView(this);
iv0.setScaleType(ScaleType.FIT_CENTER);
iv0.setLayoutParams(mParams);
iv0.setImageResource(pics[0]);
views.add(iv0);
ImageView iv1 = new ImageView(this);
iv1.setScaleType(ScaleType.FIT_CENTER);
iv1.setLayoutParams(mParams);
iv1.setImageResource(pics[1]);
views.add(iv1);
//设置数据
viewPager.setAdapter(vpAdapter);
//设置监听
viewPager.setOnPageChangeListener(this);
//初始化底部小点
initPoint();
}
@Override
protected void onLoadingDrawableSet(Drawable imageDrawable) {
if (null != imageDrawable) {
final int dHeight = imageDrawable.getIntrinsicHeight();
final int dWidth = imageDrawable.getIntrinsicWidth();
/**
* We need to set the width/height of the ImageView so that it is
* square with each side the size of the largest drawable dimension.
* This is so that it doesn't clip when rotated.
*/
ViewGroup.LayoutParams lp = mHeaderImage.getLayoutParams();
lp.width = lp.height = Math.max(dHeight, dWidth);
mHeaderImage.requestLayout();
/**
* We now rotate the Drawable so that is at the correct rotation,
* and is centered.
*/
mHeaderImage.setScaleType(ScaleType.MATRIX);
Matrix matrix = new Matrix();
matrix.postTranslate((lp.width - dWidth) / 2f, (lp.height - dHeight) / 2f);
matrix.postRotate(getDrawableRotationAngle(), lp.width / 2f, lp.height / 2f);
mHeaderImage.setImageMatrix(matrix);
}
}
public RotateLoadingLayout(Context context, Mode mode, Orientation scrollDirection, TypedArray attrs) {
super(context, mode, scrollDirection, attrs);
mRotateDrawableWhilePulling = attrs.getBoolean(R.styleable.PullToRefresh_ptrRotateDrawableWhilePulling, true);
mHeaderImage.setScaleType(ScaleType.MATRIX);
mHeaderImageMatrix = new Matrix();
mHeaderImage.setImageMatrix(mHeaderImageMatrix);
mRotateAnimation = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
mRotateAnimation.setInterpolator(ANIMATION_INTERPOLATOR);
mRotateAnimation.setDuration(ROTATION_ANIMATION_DURATION);
mRotateAnimation.setRepeatCount(Animation.INFINITE);
mRotateAnimation.setRepeatMode(Animation.RESTART);
}
/**
* Creates a new image request, decoding to a maximum specified width and height. If both width
* and height are zero, the image will be decoded to its natural size. If one of the two is
* nonzero, that dimension will be clamped and the other one will be set to preserve the image's
* aspect ratio. If both width and height are nonzero, the image will be decoded to be fit in
* the rectangle of dimensions width x height while keeping its aspect ratio.
*
* @param url URL of the image
* @param listener Listener to receive the decoded bitmap
* @param maxWidth Maximum width to decode this bitmap to, or zero for none
* @param maxHeight Maximum height to decode this bitmap to, or zero for none
* @param scaleType The ImageViews ScaleType used to calculate the needed image size.
* @param decodeConfig Format to decode the bitmap to
* @param errorListener Error listener, or null to ignore errors
*/
public ImageRequest(
String url,
Response.Listener<Bitmap> listener,
int maxWidth,
int maxHeight,
ScaleType scaleType,
Config decodeConfig,
@Nullable Response.ErrorListener errorListener) {
super(Method.GET, url, errorListener);
setRetryPolicy(
new DefaultRetryPolicy(
DEFAULT_IMAGE_TIMEOUT_MS,
DEFAULT_IMAGE_MAX_RETRIES,
DEFAULT_IMAGE_BACKOFF_MULT));
mListener = listener;
mDecodeConfig = decodeConfig;
mMaxWidth = maxWidth;
mMaxHeight = maxHeight;
mScaleType = scaleType;
}
@Test
public void publicMethods() throws Exception {
// Catch-all test to find API-breaking changes.
assertNotNull(
ImageRequest.class.getConstructor(
String.class,
Response.Listener.class,
int.class,
int.class,
Bitmap.Config.class,
Response.ErrorListener.class));
assertNotNull(
ImageRequest.class.getConstructor(
String.class,
Response.Listener.class,
int.class,
int.class,
ImageView.ScaleType.class,
Bitmap.Config.class,
Response.ErrorListener.class));
assertEquals(ImageRequest.DEFAULT_IMAGE_TIMEOUT_MS, 1000);
assertEquals(ImageRequest.DEFAULT_IMAGE_MAX_RETRIES, 2);
assertEquals(ImageRequest.DEFAULT_IMAGE_BACKOFF_MULT, 2f, 0);
}
public ButtonFloat(Context context, AttributeSet attrs) {
super(context, attrs);
setBackgroundResource(R.drawable.background_button_float);
setBackgroundColor(backgroundColor);
sizeRadius = 28;
setDefaultProperties();
icon = new ImageView(context);
icon.setAdjustViewBounds(true);
icon.setScaleType(ScaleType.CENTER_CROP);
if (drawableIcon != null) {
icon.setImageDrawable(drawableIcon);
}
RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(Utils.dpToPx(sizeIcon, getResources()),
Utils.dpToPx(sizeIcon, getResources()));
params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
icon.setLayoutParams(params);
addView(icon);
}
public RotateLoadingLayout(Context context, Mode mode, Orientation scrollDirection, TypedArray attrs) {
super(context, mode, scrollDirection, attrs);
mRotateDrawableWhilePulling = attrs.getBoolean(R.styleable.PullToRefresh_ptrRotateDrawableWhilePulling, true);
mHeaderImage.setScaleType(ScaleType.MATRIX);
mHeaderImageMatrix = new Matrix();
mHeaderImage.setImageMatrix(mHeaderImageMatrix);
mRotateAnimation = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
mRotateAnimation.setInterpolator(ANIMATION_INTERPOLATOR);
mRotateAnimation.setDuration(ROTATION_ANIMATION_DURATION);
mRotateAnimation.setRepeatCount(Animation.INFINITE);
mRotateAnimation.setRepeatMode(Animation.RESTART);
}
public RotateLoadingLayout(Context context, Mode mode, Orientation scrollDirection, TypedArray attrs) {
super(context, mode, scrollDirection, attrs);
mRotateDrawableWhilePulling = attrs.getBoolean(R.styleable.PullToRefresh_ptrRotateDrawableWhilePulling, true);
mHeaderImage.setScaleType(ScaleType.MATRIX);
mHeaderImageMatrix = new Matrix();
mHeaderImage.setImageMatrix(mHeaderImageMatrix);
mRotateAnimation = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
mRotateAnimation.setInterpolator(ANIMATION_INTERPOLATOR);
mRotateAnimation.setDuration(ROTATION_ANIMATION_DURATION);
mRotateAnimation.setRepeatCount(Animation.INFINITE);
mRotateAnimation.setRepeatMode(Animation.RESTART);
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
LinearLayout widgetFrameView = ((LinearLayout) view.findViewById(android.R.id.widget_frame));
mBtnAppIcon = new ImageButton(mContext);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(mAppIconPreviewSizePx, mAppIconPreviewSizePx);
lp.gravity = Gravity.CENTER;
mBtnAppIcon.setLayoutParams(lp);
mBtnAppIcon.setScaleType(ScaleType.CENTER_CROP);
mBtnAppIcon.setImageDrawable(mAppInfo.icon);
mBtnAppIcon.setFocusable(false);
if (mIconPickerEnabled) {
mBtnAppIcon.setOnClickListener(this);
mBtnAppIcon.setOnLongClickListener(this);
} else {
mBtnAppIcon.setEnabled(false);
}
widgetFrameView.addView(mBtnAppIcon);
widgetFrameView.setVisibility(View.VISIBLE);
}
/**
* Set's the ImageView's ScaleType to Matrix.
*/
private static void setImageViewScaleTypeMatrix(ImageView imageView) {
/**
* PhotoView sets it's own ScaleType to Matrix, then diverts all calls
* setScaleType to this.setScaleType automatically.
*/
if (null != imageView && !(imageView instanceof IPhotoView)) {
if (!ScaleType.MATRIX.equals(imageView.getScaleType())) {
imageView.setScaleType(ScaleType.MATRIX);
}
}
}
/**
* Set's the ImageView's ScaleType to Matrix.
*/
private static void setImageViewScaleTypeMatrix(ImageView imageView) {
if (null != imageView) {
if (imageView instanceof EasePhotoView) {
/**
* PhotoView sets it's own ScaleType to Matrix, then diverts all
* calls setScaleType to this.setScaleType. Basically we don't
* need to do anything here
*/
} else {
imageView.setScaleType(ScaleType.MATRIX);
}
}
}
@Override
public final void setScaleType(ScaleType scaleType) {
if (isSupportedScaleType(scaleType) && scaleType != mScaleType) {
mScaleType = scaleType;
// Finally update
update();
}
}
/**
* Set's the ImageView's ScaleType to Matrix.
*/
private static void setImageViewScaleTypeMatrix(ImageView imageView) {
if (null != imageView) {
if (imageView instanceof PhotoView) {
/**
* PhotoView sets it's own ScaleType to Matrix, then diverts all
* calls setScaleType to this.setScaleType. Basically we don't
* need to do anything here
*/
} else {
imageView.setScaleType(ScaleType.MATRIX);
}
}
}
private void checkImageViewScaleType() {
ImageView imageView = getImageView();
/**
* PhotoView's getScaleType() will just divert to this.getScaleType() so
* only call if we're not attached to a PhotoView.
*/
if (null != imageView && !(imageView instanceof EasePhotoView)) {
if (imageView.getScaleType() != ScaleType.MATRIX) {
throw new IllegalStateException(
"The ImageView's ScaleType has been changed since attaching a PhotoViewAttacher");
}
}
}
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();
}
}
@Override
public void setScaleType(ScaleType scaleType) {
if (isSupportedScaleType(scaleType) && scaleType != mScaleType) {
mScaleType = scaleType;
// Finally update
update();
}
}
/**
* Set's the ImageView's ScaleType to Matrix.
*/
private static void setImageViewScaleTypeMatrix(ImageView imageView) {
/**
* PhotoView sets it's own ScaleType to Matrix, then diverts all calls
* setScaleType to this.setScaleType automatically.
*/
if (null != imageView && !(imageView instanceof ScaleImageView)) {
if (!ScaleType.MATRIX.equals(imageView.getScaleType())) {
imageView.setScaleType(ScaleType.MATRIX);
}
}
}
/**
* Set's the ImageView's ScaleType to Matrix.
*/
private static void setImageViewScaleTypeMatrix(ImageView imageView) {
/**
* PhotoView sets it's own ScaleType to Matrix, then diverts all calls
* setScaleType to this.setScaleType automatically.
*/
if (null != imageView && !(imageView instanceof PhotoView)) {
if (!ScaleType.MATRIX.equals(imageView.getScaleType())) {
imageView.setScaleType(ScaleType.MATRIX);
}
}
}
/**
* @return true if the ScaleType is supported.
*/
private static boolean isSupportedScaleType(final ScaleType scaleType) {
if (null == scaleType) {
return false;
}
switch (scaleType) {
case MATRIX:
throw new IllegalArgumentException(scaleType.name()
+ " is not supported in PhotoView");
default:
return true;
}
}
/**
* Equivalent to calling {@link #get(String, ImageListener, int, int, ScaleType)} with
* {@code Scaletype == ScaleType.CENTER_INSIDE}.
*/
/*
* 默认 scaleType: ScaleType.CENTER_INSIDE
*/
public ImageContainer get(String requestUrl, ImageListener imageListener, int maxWidth, int maxHeight) {
return get(requestUrl, imageListener, maxWidth, maxHeight, ScaleType.CENTER_INSIDE);
}
/**
* Set's the ImageView's ScaleType to Matrix.
*/
private static void setImageViewScaleTypeMatrix(ImageView imageView) {
/**
* PhotoView sets it's own ScaleType to Matrix, then diverts all calls
* setScaleType to this.setScaleType automatically.
*/
if (null != imageView && !(imageView instanceof IPhotoView)) {
if (!ScaleType.MATRIX.equals(imageView.getScaleType())) {
imageView.setScaleType(ScaleType.MATRIX);
}
}
}
private void checkImageViewScaleType() {
ImageView imageView = getImageView();
/**
* PhotoView's getScaleType() will just divert to this.getScaleType() so
* only call if we're not attached to a PhotoView.
*/
if (null != imageView && !(imageView instanceof IPhotoView)) {
if (!ScaleType.MATRIX.equals(imageView.getScaleType())) {
throw new IllegalStateException(
"The ImageView's ScaleType has been changed since attaching a PhotoViewAttacher");
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_matrix);
mImageView = (ImageView) findViewById(R.id.image);
mImageView.setScaleType(ScaleType.MATRIX);
updateMatrix(getIntent().getIntExtra(KEY_ACTION, -1));
}
public RoundedDrawable setScaleType(ScaleType scaleType) {
if (scaleType == null) {
scaleType = ScaleType.FIT_CENTER;
}
if (mScaleType != scaleType) {
mScaleType = scaleType;
updateShaderMatrix();
}
return this;
}
public void onCreate() {
activity.getWindow().setBackgroundDrawable(new ColorDrawable(0x4c000000));
sivViewer = new ScaledImageView(activity);
sivViewer.setScaleType(ScaleType.MATRIX);
activity.setContentView(sivViewer);
if (pic != null) {
sivViewer.getViewTreeObserver().addOnGlobalLayoutListener(this);
}
}
/**
* @return true if the ScaleType is supported.
*/
private static boolean isSupportedScaleType(final ScaleType scaleType) {
if (null == scaleType) {
return false;
}
switch (scaleType) {
case MATRIX:
throw new IllegalArgumentException(scaleType.name()
+ " is not supported in PhotoView");
default:
return true;
}
}
protected Request<Bitmap> makeImageRequest(String requestUrl, int maxWidth, int maxHeight, ScaleType scaleType, final String cacheKey) {
return new ImageRequest(requestUrl, new Listener<Bitmap>() {
@Override public void onResponse(Bitmap response) {
onGetImageSuccess(cacheKey, response);
}
}, maxWidth, maxHeight, scaleType, Config.RGB_565, new ErrorListener() {
@Override public void onErrorResponse(VolleyError error) {
onGetImageError(cacheKey, error);
}
});
}
protected Request<Bitmap> makeImageRequest(String requestUrl, int maxWidth, int maxHeight,
ScaleType scaleType, final String cacheKey) {
return new ImageRequest(requestUrl, new Listener<Bitmap>() {
@Override
public void onResponse(Bitmap response) {
onGetImageSuccess(cacheKey, response);
}
}, maxWidth, maxHeight, scaleType, Config.RGB_565, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
onGetImageError(cacheKey, error);
}
});
}
@Override
public void setScaleType(ScaleType scaleType) {
if (isSupportedScaleType(scaleType) && scaleType != mScaleType) {
mScaleType = scaleType;
// Finally update
update();
}
}
/**
* 画面を更新します.
* @param drawObj 更新する画像データ
*/
private void showDrawObject(final CanvasDrawImageObject drawObj) {
switch (drawObj.getMode()) {
default:
case NON_SCALE_MODE:
Matrix matrix = new Matrix();
matrix.postTranslate((float) drawObj.getX(), (float) drawObj.getY());
mCanvasView.setImageBitmap(mBitmap);
mCanvasView.setScaleType(ScaleType.MATRIX);
mCanvasView.setImageMatrix(matrix);
break;
case SCALE_MODE:
mCanvasView.setImageBitmap(mBitmap);
mCanvasView.setScaleType(ScaleType.FIT_CENTER);
mCanvasView.setTranslationX((int) drawObj.getX());
mCanvasView.setTranslationY((int) drawObj.getY());
break;
case FILL_MODE:
BitmapDrawable bd = new BitmapDrawable(getResources(), mBitmap);
bd.setTileModeX(Shader.TileMode.REPEAT);
bd.setTileModeY(Shader.TileMode.REPEAT);
mCanvasView.setImageDrawable(bd);
mCanvasView.setScaleType(ScaleType.FIT_XY);
mCanvasView.setTranslationX((int) drawObj.getX());
mCanvasView.setTranslationY((int) drawObj.getY());
break;
}
}