如何将imageview的可缩放/放大部分显示为另一个像flipkart的图像视图?

时间:2016-10-17 07:51:08

标签: java android

我的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#ffffff"
>
 <ImageView
    android:layout_width="wrap_content"
    android:layout_height="200dp"
    android:id="@+id/imageViewZooming"
    android:scaleType="fitXY"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

 <utility.ImageMagnifier
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:src="@drawable/banner_image"
    android:id="@+id/imageViewOriginal"
    android:scaleType="fitXY"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/imageViewZooming"
    android:layout_toEndOf="@+id/imageViewZooming" 
    />
</RelativeLayout>

我的ImageMagnifier.java代码如下:

public class ImageMagnifier extends ImageView {

private PointF zoomPos;
private boolean zooming = false;
private Matrix matrix;
private Paint paint;
private Bitmap bitmap;
private BitmapShader shader;
private int sizeOfMagnifier = 200;

public ImageMagnifier(Context context) {
    super(context);
    init();
}

public ImageMagnifier(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}

public ImageMagnifier(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

private void init() {
    zoomPos = new PointF(0, 0);
    matrix = new Matrix();
    paint = new Paint();

}

@Override
public boolean onTouchEvent(MotionEvent event) {
    int action = event.getAction();

    zoomPos.x = event.getX();
    zoomPos.y = event.getY();

    switch (action) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_MOVE:
            zooming = true;
            this.invalidate();
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            zooming = false;
            this.invalidate();
            break;

        default:
            break;
    }

    return true;
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (!zooming) {
        buildDrawingCache();
    } else {

        bitmap = getDrawingCache();
        shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);

        paint = new Paint();
        paint.setShader(shader);
        matrix.reset();
        matrix.postScale(2f, 2f, zoomPos.x, zoomPos.y);
        paint.getShader().setLocalMatrix(matrix);
        canvas.drawCircle(zoomPos.x, zoomPos.y, sizeOfMagnifier, paint);

    }
  }
}

我使用上面的代码来放大/缩放使用触摸的imageview的某些部分。但我无法在另一个图像视图中看到特定的缩放图像。这里我使用canvas.drawCircle方法进行缩放。

enter image description here

0 个答案:

没有答案