android 图片手势放大缩小

建立img工具类 MyImgView

public class MyImgView extends AppCompatImageView implements View.OnTouchListener, ScaleGestureDetector.OnScaleGestureListener {

    boolean mBoo = true;
    Matrix matrix = new Matrix();
    ScaleGestureDetector scaleGestureDetector;

    public MyImgView(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mBoo) {
            matrix.postTranslate((getWidth() - getDrawable().getIntrinsicWidth()) / 2, (getHeight() - getDrawable().getIntrinsicHeight()) / 2);
            setImageMatrix(matrix);
            mBoo = false;
        }
    }

    public MyImgView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setOnTouchListener(this);
        scaleGestureDetector = new ScaleGestureDetector(context, this);
        setScaleType(ScaleType.MATRIX);
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        return scaleGestureDetector.onTouchEvent(motionEvent);
    }

    @Override
    public boolean onScale(ScaleGestureDetector scaleGestureDetector) {
        float scaleFactor = scaleGestureDetector.getScaleFactor();
        matrix.postScale(scaleFactor, scaleFactor, getWidth() / 2, getHeight() / 2);
        setImageMatrix(matrix);
        return true;
    }

    @Override
    public boolean onScaleBegin(ScaleGestureDetector scaleGestureDetector) {
        return true;
    }

    @Override
    public void onScaleEnd(ScaleGestureDetector scaleGestureDetector) {

    }
}

Activity 代码

public class Activity_04 extends AppCompatActivity {

    private ImageView activity04Img1;
    private ImageView activity04Img2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_04);
        initView();
    }

    private void initView() {
        activity04Img1 = (ImageView) findViewById(R.id.activity_04_img1);
        activity04Img2 = (ImageView) findViewById(R.id.activity_04_img2);
        activity04Img1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                activity04Img1.setVisibility(View.GONE);
                activity04Img2.setVisibility(View.VISIBLE);
                activity04Img2.setImageResource(R.drawable.example_picture);
            }
        });
    }
}

xml文件

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <ImageView
            android:id="@+id/activity_04_img1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/example_picture" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:gravity="center">
        
		<!--写上自己建的img类路径-->
        <com.baidu.demo.Util.MyImgView
            android:id="@+id/activity_04_img2"
            android:layout_width="400dp"
            android:layout_height="400dp"
            android:layout_centerInParent="true"
            android:visibility="gone" />
    </LinearLayout>

最后在模拟上点开图片,img1隐藏,img2居中显示,按住crtl键 鼠标移动便可放都缩小了

你可能感兴趣的:(Android项目)