Java Code Examples for android.graphics.Matrix.preScale()

The following are Jave code examples for showing how to use preScale() of the android.graphics.Matrix class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: PlusGram   File: ZoomTouchView.java   Source Code and License Vote up 7 votes
private void doubleClick(MotionEvent event) {
    // this is still broken
    if (DOUBLE_TAP_FATBITS) {
        final float density = mSlate.getDrawingDensity();
        final float scale = 1f/density;
        final Matrix m = new Matrix();
        mTouchPointDoc[0] = mTouchPointDoc[1] = 0f;
        if (getScale(mSlate.getZoom()) == scale) {
            getCenter(event, mTouchPoint);
            mTouchPoint[0] *= density;
            mTouchPoint[1] *= density;
            mTouchPointDoc[0] = mTouchPoint[0] - mSlate.getZoomPosX();
            mTouchPointDoc[1] = mTouchPoint[1] - mSlate.getZoomPosY();
            mSlate.getZoomInv().mapPoints(mTouchPointDoc);
            m.preScale(scale*DOUBLE_TAP_ZOOM_LEVEL, scale*DOUBLE_TAP_ZOOM_LEVEL);

            mSlate.setZoomPosNoInval(mTouchPointDoc);
            mSlate.setZoom(m);
        } else {
            mSlate.resetZoom();
        }
    }
}
 
Example 2
Project: jackknife   File: ImageUtils.java   Source Code and License Vote up 6 votes
public static Bitmap makeReflectionImage(Bitmap bitmap) {
    int reflectionGap = 4;
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);
    Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height/2, width, height/2, matrix,
            false);
    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height/2),
            Bitmap.Config.ARGB_8888);
    Paint defaultPaint = new Paint();
    Canvas canvas = new Canvas(bitmapWithReflection);
    canvas.drawBitmap(bitmap, 0, 0, null);
    canvas.drawRect(0, height,width,height + reflectionGap, defaultPaint);
    canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
            bitmapWithReflection.getHeight()+reflectionGap, 0x70FFFFFF, 0x00FFFFFF,
            Shader.TileMode.CLAMP);
    paint.setShader(shader);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()+reflectionGap, paint);
    return bitmapWithReflection;
}
 
Example 3
Project: react-native-camera-face-detector   File: MutableImage.java   Source Code and License Vote up 6 votes
public void mirrorImage() throws ImageMutationFailedException {
    Matrix m = new Matrix();

    m.preScale(-1, 1);

    Bitmap bitmap = Bitmap.createBitmap(
            currentRepresentation,
            0,
            0,
            currentRepresentation.getWidth(),
            currentRepresentation.getHeight(),
            m,
            false
    );

    if (bitmap == null)
        throw new ImageMutationFailedException("failed to mirror");

    this.currentRepresentation = bitmap;
}
 
Example 4
Project: GitHub   File: PhoenixHeader.java   Source Code and License Vote up 5 votes
private void drawSun(Canvas canvas, int width, int height) {
        Matrix matrix = mMatrix;
        matrix.reset();


        float mSunLeftOffset = 0.3f * (float) width;
        float mSunTopOffset = (mHeaderHeight * 0.1f);

        float sunRadius = (float) mSunSize / 2.0f;
        float offsetX = mSunLeftOffset + sunRadius;
        float offsetY = mSunTopOffset + (mHeaderHeight / 2) * (1.0f - Math.min(mPercent, 1)); // Move the sun up

        int bWidth = mDrawableSun.width();
        float sunScale = 1f * mSunSize / bWidth;

        if (mPercent > 1) {
            sunScale = sunScale * (1.0f - 0.5f * (mPercent - 1));
            sunRadius = sunRadius * (1.0f - 0.5f * (mPercent - 1));
        }

        matrix.preScale(sunScale, sunScale);
        matrix.postRotate((isRefreshing ? -360 : 360) * mRotate * (isRefreshing ? 1 : SUN_INITIAL_ROTATE_GROWTH),
                sunRadius,
                sunRadius);

//        canvas.save();
//        canvas.translate(offsetX, offsetY);
//        canvas.drawBitmap(mSun, matrix, null);
//        canvas.restore();

        final int saveCount = canvas.getSaveCount();
        canvas.save();
        canvas.translate(offsetX, offsetY);
        canvas.concat(matrix);
        mDrawableSun.draw(canvas);
        canvas.restoreToCount(saveCount);
    }
 
Example 5
Project: Exoplayer2Radio   File: ImageHelper.java   Source Code and License Vote up 5 votes
private Matrix createTransformationMatrix (int size) {
    Matrix matrix = new Matrix();

    // get size of original image and calculate padding
    float inputImageHeight = (float)mInputImage.getHeight();
    float inputImageWidth = (float)mInputImage.getWidth();
    float padding = (float)size/4;

    // define variables needed for transformation matrix
    float aspectRatio = 0.0f;
    float xTranslation = 0.0f;
    float yTranslation = 0.0f;

    // landscape format and square
    if (inputImageWidth >= inputImageHeight) {
        aspectRatio = (size - padding*2) / inputImageWidth;
        xTranslation = 0.0f + padding;
        yTranslation = (size - inputImageHeight * aspectRatio)/2.0f;
    }
    // portrait format
    else if (inputImageHeight > inputImageWidth) {
        aspectRatio = (size - padding*2) / inputImageHeight;
        yTranslation = 0.0f + padding;
        xTranslation = (size - inputImageWidth * aspectRatio)/2.0f;
    }

    // construct transformation matrix
    matrix.postTranslate(xTranslation, yTranslation);
    matrix.preScale(aspectRatio, aspectRatio);

    return matrix;
}
 
Example 6
Project: GitHub   File: ImageUtils.java   Source Code and License Vote up 5 votes
/**
 * 添加倒影
 *
 * @param src              源图片的
 * @param reflectionHeight 倒影高度
 * @param recycle          是否回收
 * @return 带倒影图片
 */
public static Bitmap addReflection(final Bitmap src,
                                   final int reflectionHeight,
                                   final boolean recycle) {
    if (isEmptyBitmap(src)) return null;
    // 原图与倒影之间的间距
    final int REFLECTION_GAP = 0;
    int srcWidth = src.getWidth();
    int srcHeight = src.getHeight();
    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);
    Bitmap reflectionBitmap = Bitmap.createBitmap(src, 0, srcHeight - reflectionHeight,
            srcWidth, reflectionHeight, matrix, false);
    Bitmap ret = Bitmap.createBitmap(srcWidth, srcHeight + reflectionHeight, src.getConfig());
    Canvas canvas = new Canvas(ret);
    canvas.drawBitmap(src, 0, 0, null);
    canvas.drawBitmap(reflectionBitmap, 0, srcHeight + REFLECTION_GAP, null);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    LinearGradient shader = new LinearGradient(
            0, srcHeight,
            0, ret.getHeight() + REFLECTION_GAP,
            0x70FFFFFF,
            0x00FFFFFF,
            Shader.TileMode.MIRROR);
    paint.setShader(shader);
    paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.DST_IN));
    canvas.drawRect(0, srcHeight + REFLECTION_GAP, srcWidth, ret.getHeight(), paint);
    if (!reflectionBitmap.isRecycled()) reflectionBitmap.recycle();
    if (recycle && !src.isRecycled()) src.recycle();
    return ret;
}
 
Example 7
Project: MyCreditCardDemo   File: CmbChinaLoginActivity.java   Source Code and License Vote up 5 votes
private Bitmap scaleBitmap(Bitmap origin, float ratio) {
    if (origin == null) {
        return null;
    }
    int width = origin.getWidth();
    int height = origin.getHeight();
    Matrix matrix = new Matrix();
    matrix.preScale(ratio, ratio);
    Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);
    if (newBM.equals(origin)) {
        return newBM;
    }
    origin.recycle();
    return newBM;
}
 
Example 8
Project: TensorflowAndroidDemo   File: ObjectTracker.java   Source Code and License Vote up 5 votes
@SuppressWarnings("unused")
public synchronized void drawOverlay(final GL10 gl,
    final Size cameraViewSize, final Matrix matrix) {
  final Matrix tempMatrix = new Matrix(matrix);
  tempMatrix.preScale(DOWNSAMPLE_FACTOR, DOWNSAMPLE_FACTOR);
  tempMatrix.getValues(matrixValues);
  drawNative(cameraViewSize.width, cameraViewSize.height, matrixValues);
}
 
Example 9
Project: SmartRefreshLayout   File: PhoenixHeader.java   Source Code and License Vote up 5 votes
private void drawSun(Canvas canvas, int width, int height) {
        Matrix matrix = mMatrix;
        matrix.reset();


        float mSunLeftOffset = 0.3f * (float) width;
        float mSunTopOffset = (mHeaderHeight * 0.1f);

        float sunRadius = (float) mSunSize / 2.0f;
        float offsetX = mSunLeftOffset + sunRadius;
        float offsetY = mSunTopOffset + (mHeaderHeight / 2) * (1.0f - Math.min(mPercent, 1)); // Move the sun up

        int bWidth = mDrawableSun.width();
        float sunScale = 1f * mSunSize / bWidth;

        if (mPercent > 1) {
            sunScale = sunScale * (1.0f - 0.5f * (mPercent - 1));
            sunRadius = sunRadius * (1.0f - 0.5f * (mPercent - 1));
        }

        matrix.preScale(sunScale, sunScale);
        matrix.postRotate((isRefreshing ? -360 : 360) * mRotate * (isRefreshing ? 1 : SUN_INITIAL_ROTATE_GROWTH),
                sunRadius,
                sunRadius);

//        canvas.save();
//        canvas.translate(offsetX, offsetY);
//        canvas.drawBitmap(mSun, matrix, null);
//        canvas.restore();

        final int saveCount = canvas.getSaveCount();
        canvas.save();
        canvas.translate(offsetX, offsetY);
        canvas.concat(matrix);
        mDrawableSun.draw(canvas);
        canvas.restoreToCount(saveCount);
    }
 
Example 10
Project: Open-Quran-Radio   File: ImageHelper.java   Source Code and License Vote up 5 votes
private Matrix createTransformationMatrix (int size) {
    Matrix matrix = new Matrix();

    // get size of original image and calculate padding
    float inputImageHeight = (float)mInputImage.getHeight();
    float inputImageWidth = (float)mInputImage.getWidth();
    float padding = (float)size/32;

    // define variables needed for transformation matrix
    float aspectRatio = 0.0f;
    float xTranslation = 0.0f;
    float yTranslation = 0.0f;

    // landscape format and square
    if (inputImageWidth >= inputImageHeight) {
        aspectRatio = (size - padding*2) / inputImageWidth;
        xTranslation = 0.0f + padding;
        yTranslation = (size - inputImageHeight * aspectRatio)/2.0f;
    }
    // portrait format
    else if (inputImageHeight > inputImageWidth) {
        aspectRatio = (size - padding*2) / inputImageHeight;
        yTranslation = 0.0f + padding;
        xTranslation = (size - inputImageWidth * aspectRatio)/2.0f;
    }

    // construct transformation matrix
    matrix.postTranslate(xTranslation, yTranslation);
    matrix.preScale(aspectRatio, aspectRatio);

    return matrix;
}
 
Example 11
Project: mao-android   File: EglSurfaceBase.java   Source Code and License Vote up 5 votes
public Bitmap getFrameBitmap() {
    if (!mEglCore.isCurrent(mEGLSurface)) {
        throw new RuntimeException("Expected EGL context/surface is not current");
    }

    // glReadPixels fills in a "direct" ByteBuffer with what is essentially big-endian RGBA
    // data (i.e. a byte of red, followed by a byte of green...).  While the Bitmap
    // constructor that takes an int[] wants little-endian ARGB (blue/red swapped), the
    // Bitmap "copy pixels" method wants the same format GL provides.
    //
    // Ideally we'd have some way to re-use the ByteBuffer, especially if we're calling
    // here often.
    //
    // Making this even more interesting is the upside-down nature of GL, which means
    // our output will look upside down relative to what appears on screen if the
    // typical GL conventions are used.

    int width = getWidth();
    int height = getHeight();
    ByteBuffer buf = ByteBuffer.allocateDirect(width * height * 4);
    buf.order(ByteOrder.LITTLE_ENDIAN);
    GLES20.glReadPixels(0, 0, width, height,
            GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buf);
    //GlUtil.checkGlError("glReadPixels");
    buf.rewind();

    Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bmp.copyPixelsFromBuffer(buf);

    Matrix matrix = new Matrix();
    matrix.preScale(1f, -1f);
    Bitmap bmp2 = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, false);

    bmp.recycle();
    bmp = null;
    return bmp2;
}
 
Example 12
Project: XFrame   File: XBitmapUtils.java   Source Code and License Vote up 5 votes
/**
 * 水平翻转图片
 *
 * @param bitmap 原图
 * @return 水平翻转后的图片
 */
public static Bitmap reverseByHorizontal(Bitmap bitmap) {
    Matrix matrix = new Matrix();
    matrix.preScale(-1, 1);
    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
            bitmap.getHeight(), matrix, false);
}
 
Example 13
Project: ImageEraser   File: MainActivity.java   Source Code and License Vote up 5 votes
public Bitmap resizeBitmapByCanvas() {
    float width;
    float heigth;
    float orginalWidth = (float) originalBitmap.getWidth();
    float orginalHeight = (float) originalBitmap.getHeight();
    if (orginalWidth > orginalHeight) {
        width = (float) imageViewWidth;
        heigth = (((float) imageViewWidth) * orginalHeight) / orginalWidth;
    } else {
        heigth = (float) imageViewHeight;
        width = (((float) imageViewHeight) * orginalWidth) / orginalHeight;
    }
    if (width > orginalWidth || heigth > orginalHeight) {
        return originalBitmap;
    }
    Bitmap background = Bitmap.createBitmap((int) width, (int) heigth, Config.ARGB_8888);
    Canvas canvas = new Canvas(background);
    float scale = width / orginalWidth;
    float yTranslation = (heigth - (orginalHeight * scale)) / 2.0f;
    Matrix transformation = new Matrix();
    transformation.postTranslate(0.0f, yTranslation);
    transformation.preScale(scale, scale);
    Paint paint = new Paint();
    paint.setFilterBitmap(true);
    canvas.drawBitmap(originalBitmap, transformation, paint);
    this.isImageResized = true;
    return background;
}
 
Example 14
Project: ImageMagnifier   File: MainActivity.java   Source Code and License Vote up 5 votes
public Bitmap resizeBitmapByCanvas() {
    float width;
    float height;
    float originalWidth = (float) this.originalBitmap.getWidth();
    float originalHeight = (float) this.originalBitmap.getHeight();
    if (originalWidth > originalHeight) {
        width = (float) this.containerWidth;
        height = (((float) this.containerWidth) * originalHeight) / originalWidth;
    } else {
        height = (float) this.containerHeight;
        width = (((float) this.containerHeight) * originalWidth) / originalHeight;
    }
    if (width > originalWidth || height > originalHeight) {
        return this.originalBitmap;
    }
    Bitmap background = Bitmap.createBitmap((int) width, (int) height, Config.ARGB_8888);
    Canvas canvas = new Canvas(background);
    float scale = width / originalWidth;
    float yTranslation = (height - (originalHeight * scale)) / 2.0f;
    Matrix transformation = new Matrix();
    transformation.postTranslate(0.0f, yTranslation);
    transformation.preScale(scale, scale);
    Paint paint = new Paint();
    paint.setFilterBitmap(true);
    canvas.drawBitmap(this.originalBitmap, transformation, paint);
    return background;
}
 
Example 15
Project: BBSSDK-for-Android   File: ImageUtils.java   Source Code and License Vote up 5 votes
/**
 * 转为圆形图片
 *
 * @param src         源图片
 * @param recycle     是否回收
 * @param borderSize  边框尺寸
 * @param borderColor 边框颜色
 * @return 圆形图片
 */
public static Bitmap toRound(final Bitmap src, int borderSize, int borderColor, final boolean recycle) {
	if (isEmptyBitmap(src)) {
		return null;
	}
	int width = src.getWidth();
	int height = src.getHeight();
	int size = Math.min(width, height);
	Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
	Bitmap ret = Bitmap.createBitmap(width, height, src.getConfig());
	float center = size / 2f;
	RectF rectF = new RectF(0, 0, width, height);
	rectF.inset((width - size) / 2f, (height - size) / 2f);
	Matrix matrix = new Matrix();
	matrix.setTranslate(rectF.left, rectF.top);
	matrix.preScale((float) size / width, (float) size / height);
	BitmapShader shader = new BitmapShader(src, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
	shader.setLocalMatrix(matrix);
	paint.setShader(shader);
	Canvas canvas = new Canvas(ret);
	canvas.drawRoundRect(rectF, center, center, paint);
	if (borderSize > 0) {
		paint.setShader(null);
		paint.setColor(borderColor);
		paint.setStyle(Paint.Style.STROKE);
		paint.setStrokeWidth(borderSize);
		float radius = center - borderSize / 2f;
		canvas.drawCircle(width / 2f, height / 2f, radius, paint);
	}
	if (recycle && !src.isRecycled()) {
		src.recycle();
	}
	return ret;
}
 
Example 16
Project: SortingHatAndroid   File: ObjectTracker.java   Source Code and License Vote up 5 votes
@SuppressWarnings("unused")
public synchronized void drawOverlay(final GL10 gl,
    final Size cameraViewSize, final Matrix matrix) {
  final Matrix tempMatrix = new Matrix(matrix);
  tempMatrix.preScale(DOWNSAMPLE_FACTOR, DOWNSAMPLE_FACTOR);
  tempMatrix.getValues(matrixValues);
  drawNative(cameraViewSize.width, cameraViewSize.height, matrixValues);
}
 
Example 17
Project: BaseCore   File: ImageUtils.java   Source Code and License Vote up 5 votes
/**
 * 添加倒影
 *
 * @param src              源图片的
 * @param reflectionHeight 倒影高度
 * @param recycle          是否回收
 * @return 带倒影图片
 */
public static Bitmap addReflection(Bitmap src, int reflectionHeight, boolean recycle) {
    if (isEmptyBitmap(src)) return null;
    // 原图与倒影之间的间距
    final int REFLECTION_GAP = 0;
    int srcWidth = src.getWidth();
    int srcHeight = src.getHeight();
    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);
    Bitmap reflectionBitmap = Bitmap.createBitmap(src, 0, srcHeight - reflectionHeight,
            srcWidth, reflectionHeight, matrix, false);
    Bitmap ret = Bitmap.createBitmap(srcWidth, srcHeight + reflectionHeight, src.getConfig());
    Canvas canvas = new Canvas(ret);
    canvas.drawBitmap(src, 0, 0, null);
    canvas.drawBitmap(reflectionBitmap, 0, srcHeight + REFLECTION_GAP, null);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    LinearGradient shader = new LinearGradient(0, srcHeight,
            0, ret.getHeight() + REFLECTION_GAP,
            0x70FFFFFF, 0x00FFFFFF, Shader.TileMode.MIRROR);
    paint.setShader(shader);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    canvas.drawRect(0, srcHeight + REFLECTION_GAP,
            srcWidth, ret.getHeight(), paint);
    if (!reflectionBitmap.isRecycled()) reflectionBitmap.recycle();
    if (recycle && !src.isRecycled()) src.recycle();
    return ret;
}
 
Example 18
Project: microMathematics   File: SVGAndroidRenderer.java   Source Code and License Vote up 4 votes
private Matrix calculateViewBoxTransform(Box viewPort, Box viewBox,
		PreserveAspectRatio positioning) {
	Matrix m = new Matrix();

	if (positioning == null || positioning.getAlignment() == null)
		return m;

	float xScale = viewPort.width / viewBox.width;
	float yScale = viewPort.height / viewBox.height;
	float xOffset = -viewBox.minX;
	float yOffset = -viewBox.minY;

	// 'none' means scale both dimensions to fit the viewport
	if (positioning.equals(PreserveAspectRatio.STRETCH)) {
		m.preTranslate(viewPort.minX, viewPort.minY);
		m.preScale(xScale, yScale);
		m.preTranslate(xOffset, yOffset);
		return m;
	}

	// Otherwise, the aspect ratio of the image is kept.
	// What scale are we going to use?
	float scale = (positioning.getScale() == PreserveAspectRatio.Scale.Slice) ? Math
			.max(xScale, yScale) : Math.min(xScale, yScale);
	// What size will the image end up being?
	float imageW = viewPort.width / scale;
	float imageH = viewPort.height / scale;
	// Determine final X position
	switch (positioning.getAlignment()) {
	case XMidYMin:
	case XMidYMid:
	case XMidYMax:
		xOffset -= (viewBox.width - imageW) / 2;
		break;
	case XMaxYMin:
	case XMaxYMid:
	case XMaxYMax:
		xOffset -= (viewBox.width - imageW);
		break;
	default:
		// nothing to do
		break;
	}
	// Determine final Y position
	switch (positioning.getAlignment()) {
	case XMinYMid:
	case XMidYMid:
	case XMaxYMid:
		yOffset -= (viewBox.height - imageH) / 2;
		break;
	case XMinYMax:
	case XMidYMax:
	case XMaxYMax:
		yOffset -= (viewBox.height - imageH);
		break;
	default:
		// nothing to do
		break;
	}

	m.preTranslate(viewPort.minX, viewPort.minY);
	m.preScale(scale, scale);
	m.preTranslate(xOffset, yOffset);
	return m;
}
 
Example 19
Project: YiZhi   File: MovingImageView.java   Source Code and License Vote up 4 votes
/**
 * 设置最佳的运动类型
 * 计算缩放比例
 *
 * @return image scale.
 */
private float calculateTypeAndScale() {
    movementType = MovingViewAnimator.AUTO_MOVE;
    float scale = 1f;
    float scaleByImage = Math.max(imageWidth / canvasWidth, imageHeight / canvasHeight);
    Matrix matrix = new Matrix();

    if (offsetWidth == 0 && offsetHeight == 0) {//图片太小,无法动画,需要放大
        //画布宽度/图片宽度
        float sW = canvasWidth / imageWidth;
        //画布高度/图片高度
        float sH = canvasHeight / imageHeight;

        if (sW > sH) {
            scale = Math.min(sW, maxRelativeSize);//限定最大缩放值
            matrix.setTranslate((canvasWidth - imageWidth * scale) / 2f, 0);
            movementType = MovingViewAnimator.VERTICAL_MOVE;//垂直移动

        } else if (sW < sH) {
            scale = Math.min(sH, maxRelativeSize);//限定最大缩放值
            matrix.setTranslate(0, (canvasHeight - imageHeight * scale) / 2f);
            movementType = MovingViewAnimator.HORIZONTAL_MOVE;//水平移动

        } else {
            scale = Math.max(sW, maxRelativeSize);//限定最大缩放值
            movementType = (scale == sW) ? MovingViewAnimator.NONE_MOVE :
                    MovingViewAnimator.DIAGONAL_MOVE;//对角线移动
        }
    } else if (offsetWidth == 0) {//宽度太小,无法执行水平动画,放大宽度
        scale = canvasWidth / imageWidth;
        movementType = MovingViewAnimator.VERTICAL_MOVE;

    } else if (offsetHeight == 0) {//高度太小,无法执行垂直动画,放大高度
        scale = canvasHeight / imageHeight;//求出画布高度和图片高度的比值用于确定画布起始坐标
        movementType = MovingViewAnimator.HORIZONTAL_MOVE;

    } else if (scaleByImage > maxRelativeSize) {//图片太大,根据最大比值设定图片缩放值
        scale = maxRelativeSize / scaleByImage;
        if (imageWidth * scale < canvasWidth || imageHeight * scale < canvasHeight) {
            scale = Math.max(canvasWidth / imageWidth, canvasHeight / imageHeight);
        }
    }

    matrix.preScale(scale, scale);
    setImageMatrix(matrix);
    return scale;
}
 
Example 20
Project: PaoMovie   File: GifTextureView.java   Source Code and License Vote up 4 votes
private void updateTextureViewSize(final GifInfoHandle gifInfoHandle) {
	final Matrix transform = new Matrix();
	final float viewWidth = getWidth();
	final float viewHeight = getHeight();
	final float scaleRef;
	final float scaleX = gifInfoHandle.width / viewWidth;
	final float scaleY = gifInfoHandle.height / viewHeight;
	RectF src = new RectF(0, 0, gifInfoHandle.width, gifInfoHandle.height);
	RectF dst = new RectF(0, 0, viewWidth, viewHeight);
	switch (mScaleType) {
	case CENTER:
		transform.setScale(scaleX, scaleY, viewWidth / 2, viewHeight / 2);
		break;
	case CENTER_CROP:
		scaleRef = 1 / Math.min(scaleX, scaleY);
		transform.setScale(scaleRef * scaleX, scaleRef * scaleY,
				viewWidth / 2, viewHeight / 2);
		break;
	case CENTER_INSIDE:
		if (gifInfoHandle.width <= viewWidth
				&& gifInfoHandle.height <= viewHeight) {
			scaleRef = 1.0f;
		} else {
			scaleRef = Math.min(1 / scaleX, 1 / scaleY);
		}
		transform.setScale(scaleRef * scaleX, scaleRef * scaleY,
				viewWidth / 2, viewHeight / 2);
		break;
	case FIT_CENTER:
		transform.setRectToRect(src, dst, Matrix.ScaleToFit.CENTER);
		transform.preScale(scaleX, scaleY);
		break;
	case FIT_END:
		transform.setRectToRect(src, dst, Matrix.ScaleToFit.END);
		transform.preScale(scaleX, scaleY);
		break;
	case FIT_START:
		transform.setRectToRect(src, dst, Matrix.ScaleToFit.START);
		transform.preScale(scaleX, scaleY);
		break;
	case FIT_XY:
		return;
	case MATRIX:
		transform.set(mTransform);
		transform.preScale(scaleX, scaleY);
		break;
	}
	super.setTransform(transform);
}