欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 自定义可拖拽,可放大缩小的ImageView

程序员文章站 2022-03-11 23:23:44
最近需要开发控件,需要放大缩小可随意拖拽,于是就自定义View来实现,具体如下:案例:1.自定义Viewimport android.annotation.SuppressLint;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Rect;impor...

最近需要开发控件,需要放大缩小可随意拖拽,于是就自定义View来实现,具体如下:
案例:
Android 自定义可拖拽,可放大缩小的ImageView1.自定义View

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.view.View;

public class ZoomDragImageView extends View {


    private Context mContext;
    private float mRation_WH = 0;
    private float mLastX = 0;
    private float mLastY = 0;
    private double mDoubleOne;
    private Paint mZoomPaint;
    private Drawable mZoomDrawable;
    private Rect mZoomDrawableRect = new Rect();
    private boolean isFirst = true;
    private int SINGALDOWN = 1;// 单点按下
    private int MUTILDOWM = 2;// 双点按下
    private int MUTILMOVE = 3;// 双点拖拽
    private int mCurStatus = 0;

    public ZoomDragImageView(Context context) {
        super(context);
        this.mContext = context;
        mZoomPaint = new Paint();
        mZoomPaint.setAntiAlias(true);
        mZoomPaint.setColor(Color.BLACK);
        mZoomPaint.setStyle(Paint.Style.FILL);
        mZoomPaint.setTextSize(35.0f);
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        if (mZoomDrawable == null || mZoomDrawable.getIntrinsicHeight() == 0
                || mZoomDrawable.getIntrinsicWidth() == 0) {
            return;
        }
        setBackground();
        mZoomDrawable.draw(canvas);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        switch (event.getPointerCount()) {
            case 1:

                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        mCurStatus = SINGALDOWN;
                        mLastX = event.getX();
                        mLastY = event.getY();
                        break;
                    case MotionEvent.ACTION_UP:
                        //checkBounds();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        if (mCurStatus == SINGALDOWN) {
                            int offsetWidth = (int) (event.getX() - mLastX);
                            int offsetHeight = (int) (event.getY() - mLastY);
                            int mWidth = mZoomDrawableRect.right-mZoomDrawableRect.left;
                            int mHeight = mZoomDrawableRect.bottom-mZoomDrawableRect.top;
                            int left=mZoomDrawableRect.left;
                            int top = mZoomDrawableRect.top;
                            int l, r, t, b;
                            //当水平或者垂直滑动距离大于10,才算是拖动事件
                            if (Math.abs(offsetWidth) > 10 || Math.abs(offsetHeight) > 10) {
                                l = (int) (left + offsetWidth);
                                r = l + mWidth;
                                t = (int) (top + offsetHeight);
                                b = t + mHeight;
                                //边界判断,不让布局滑出界面
                                if (l < 0) {
                                    l = 0;
                                    r = l + mWidth;
                                } else if (r > 1920) {
                                    r = 1920;
                                    l = r - mWidth;
                                }
                                if (t < 0) {
                                    t = 0;
                                    b = t + mHeight;
                                } else if (b > 1080) {
                                    b = 1080;
                                    t = b - mHeight;
                                }
                                mLastX = event.getX();
                                mLastY = event.getY();
                                mZoomDrawableRect.set(l,t,r,b);
                                invalidate();
                            }
                        }

                        break;
                    default:
                        break;
                }
                break;
            default:
                switch (event.getAction()) {
                    case MotionEvent.ACTION_POINTER_DOWN:
                        break;
                    case MotionEvent.ACTION_MOVE:
                        mCurStatus = MUTILMOVE;//双指
                        float X0 = event.getX(0);
                        float Y0 = event.getY(0);
                        float X1 = event.getX(1);
                        float Y1 = event.getY(1);
                        float k = (Y1 - Y0) / (X1 - X0);
                        float proportion = (Y0 * X1 - Y1 * X0) / (X1 - X0);
                        int RectCenterX = mZoomDrawableRect.centerX();
                        int RectCenterY = mZoomDrawableRect.centerY();
                        float mHandsX = (X0 + X1) / 2;
                        float mHandsY = mHandsX * k + proportion;
                        double mDoubleTwo = Math.sqrt(Math.pow(X0 - X1, 2)
                                + Math.pow(Y0 - Y1, 2));
                        if (mDoubleOne < mDoubleTwo) {//放大
                            if (mZoomDrawableRect.width() < mContext.getResources()
                                    .getDisplayMetrics().widthPixels * 2) {
                                int offsetwidth = 10;
                                int offsettop = (int) (offsetwidth / mRation_WH);
                                int l, r, t, b;
                                int left=mZoomDrawableRect.left;
                                int top = mZoomDrawableRect.top;
                                int right = mZoomDrawableRect.right;
                                int bottom = mZoomDrawableRect.bottom;
                                l=left-offsetwidth;
                                t=top-offsettop;
                                r = right + offsetwidth;
                                b = bottom + offsettop;
                                mZoomDrawableRect.set(l, t, r, b);
                                invalidate();
                            }
                        } else {//缩小
                            if (mZoomDrawableRect.width() > mContext.getResources()
                                    .getDisplayMetrics().widthPixels / 3) {
                                int offsetwidth = 10;
                                int offsettop = (int) (offsetwidth / mRation_WH);
                                int l, r, t, b;
                                int left=mZoomDrawableRect.left;
                                int top = mZoomDrawableRect.top;
                                int right = mZoomDrawableRect.right;
                                int bottom = mZoomDrawableRect.bottom;
                                l=left+offsetwidth;
                                t=top+offsettop;
                                r = right - offsetwidth;
                                b = bottom - offsettop;
                                mZoomDrawableRect.set(l, t, r, b);
                                invalidate();
                            }
                        }
                        mDoubleOne = mDoubleTwo;
                        if (mHandsX < RectCenterX) {
                            if (mHandsY < RectCenterY) {

                            } else {
                                
                            }
                        } else {
                            if (mHandsY < RectCenterY) {
                       
                            } else {
                          
                            }
                        }

                        break;
                    case MotionEvent.ACTION_UP:
                        mCurStatus = 0;
                        break;
                    default:
                        break;
                }
                break;
        }

        return true;
    }

    public void setBackground() {
        if (isFirst) {
            mRation_WH = (float) mZoomDrawable.getIntrinsicWidth()
                    / (float) mZoomDrawable.getIntrinsicHeight();
            int px_w = Math.min(getWidth(),
                    dip2px(mContext, mZoomDrawable.getIntrinsicWidth()));
            int px_h = (int) (px_w / mRation_WH);
            int left = (getWidth() - px_w) / 2;
            int top = (getHeight() - px_h) / 2;
            int right = px_w + left;
            int bottom = px_h + top;
            mZoomDrawableRect.set(left, top, right, bottom);
            // mDrawableOffsetRect.set(mZoomDrawableRect);
            isFirst = false;
        }
        mZoomDrawable.setBounds(mZoomDrawableRect);
    }

    public Drawable getmZoomDrawable() {
        return mZoomDrawable;
    }

    public void setmZoomDrawable(Drawable mZoomDrawable) {
        this.mZoomDrawable = mZoomDrawable;
    }

    public int dip2px(Context context, int value) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (value * scale + 0.5f);
    }

}

2.Activity中应用:

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Window;

import com.xinrui.ndkapp.view.ZoomDragImageView;

public class ZoomActivity extends Activity {
    Context mContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.mContext = getApplicationContext();
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        ZoomDragImageView mView = new ZoomDragImageView(mContext);
        mView.setmZoomDrawable(mContext.getResources().getDrawable(R.drawable.newpic));
        setContentView(mView);
    }
}

本文地址:https://blog.csdn.net/baidu_41666295/article/details/107409150

相关标签: android