android设置图像适合imageview

Posted

技术标签:

【中文标题】android设置图像适合imageview【英文标题】:android set image fit into imageview 【发布时间】:2013-07-07 00:34:25 【问题描述】:

我有一个自定义图像视图,并使用矩阵缩放实现了缩放/拖动功能。 我的问题是,当我打开具有 imagview 的活动时,打开的图像小于 imageview。 我希望图像完全适合图像视图。请建议我怎样才能做到这一点。

P.S:我不能使用 'fitXY' 或除矩阵以外的任何 scaleType(因为我使用矩阵来缩放全部)。我已经尝试过使用 centerInside、fitXy 等。

礼貌android Image View Pinch Zooming:ImageView 扩展:

public class CustomImageVIew extends ImageView implements OnTouchListener 


    private Matrix matrix = new Matrix();
    private Matrix savedMatrix = new Matrix();

    static final int NONE = 0;
    static final int DRAG = 1;
    static final int ZOOM = 2;

    private int mode = NONE;

    private PointF mStartPoint = new PointF();
    private PointF mMiddlePoint = new PointF();
    private Point mBitmapMiddlePoint = new Point();

    private float oldDist = 1f;
    private float matrixValues[] = 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f;
    private float scale;
    private float oldEventX = 0;
    private float oldEventY = 0;
    private float oldStartPointX = 0;
    private float oldStartPointY = 0;
    private int mViewWidth = -1;
    private int mViewHeight = -1;
    private int mBitmapWidth = -1;
    private int mBitmapHeight = -1;
    private boolean mDraggable = false;


    public CustomImageVIew(Context context) 
        this(context, null, 0);
    

    public CustomImageVIew(Context context, AttributeSet attrs) 
        this(context, attrs, 0);
    

    public CustomImageVIew(Context context, AttributeSet attrs, int defStyle) 
        super(context, attrs, defStyle);
        this.setOnTouchListener(this);
    

    @Override
    public void onSizeChanged (int w, int h, int oldw, int oldh)
        super.onSizeChanged(w, h, oldw, oldh);
        mViewWidth = w;
        mViewHeight = h;
    

    public void setBitmap(Bitmap bitmap)
        if(bitmap != null)
            setImageBitmap(bitmap);

            mBitmapWidth = bitmap.getWidth();
            mBitmapHeight = bitmap.getHeight();
            mBitmapMiddlePoint.x = (mViewWidth / 2) - (mBitmapWidth /  2);
            mBitmapMiddlePoint.y = (mViewHeight / 2) - (mBitmapHeight / 2);

            matrix.postTranslate(mBitmapMiddlePoint.x, mBitmapMiddlePoint.y);
            this.setImageMatrix(matrix);
        
    

    @Override
    public boolean onTouch(View v, MotionEvent event)
        switch (event.getAction() & MotionEvent.ACTION_MASK) 
        case MotionEvent.ACTION_DOWN:
            savedMatrix.set(matrix);
            mStartPoint.set(event.getX(), event.getY());
            mode = DRAG;
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            oldDist = spacing(event);
            if(oldDist > 10f)
                savedMatrix.set(matrix);
                midPoint(mMiddlePoint, event);
                mode = ZOOM;
            
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_POINTER_UP:
            mode = NONE;
            break;
        case MotionEvent.ACTION_MOVE:
            if(mode == DRAG)
                drag(event);
             else if(mode == ZOOM)
                zoom(event);
             
            break;
        

        return true;
    



   public void drag(MotionEvent event)
       matrix.getValues(matrixValues);

       float left = matrixValues[2];
       float top = matrixValues[5];
       float bottom = (top + (matrixValues[0] * mBitmapHeight)) - mViewHeight;
       float right = (left + (matrixValues[0] * mBitmapWidth)) -mViewWidth;

       float eventX = event.getX();
       float eventY = event.getY();
       float spacingX = eventX - mStartPoint.x;
       float spacingY = eventY - mStartPoint.y;
       float newPositionLeft = (left  < 0 ? spacingX : spacingX * -1) + left;
       float newPositionRight = (spacingX) + right;
       float newPositionTop = (top  < 0 ? spacingY : spacingY * -1) + top;
       float newPositionBottom = (spacingY) + bottom;
       boolean x = true;
       boolean y = true;

       if(newPositionRight < 0.0f || newPositionLeft > 0.0f)
           if(newPositionRight < 0.0f && newPositionLeft > 0.0f)
               x = false;
            else
               eventX = oldEventX;
               mStartPoint.x = oldStartPointX;
           
       
       if(newPositionBottom < 0.0f || newPositionTop > 0.0f)
           if(newPositionBottom < 0.0f && newPositionTop > 0.0f)
               y = false;
            else
               eventY = oldEventY;
               mStartPoint.y = oldStartPointY;
           
       

       if(mDraggable)
           matrix.set(savedMatrix);
           matrix.postTranslate(x? eventX - mStartPoint.x : 0, y? eventY - mStartPoint.y : 0);
           this.setImageMatrix(matrix);
           if(x)oldEventX = eventX;
           if(y)oldEventY = eventY;
           if(x)oldStartPointX = mStartPoint.x;
           if(y)oldStartPointY = mStartPoint.y;
       

   

   public void zoom(MotionEvent event)
       matrix.getValues(matrixValues);

       float newDist = spacing(event);
       float bitmapWidth = matrixValues[0] * mBitmapWidth;
       float bimtapHeight = matrixValues[0] * mBitmapHeight;
       boolean in = newDist > oldDist;

       if(!in && matrixValues[0] < 1)
           return;
       
       if(bitmapWidth > mViewWidth || bimtapHeight > mViewHeight)
           mDraggable = true;
        else
           mDraggable = false;
       

       float midX = (mViewWidth / 2);
       float midY = (mViewHeight / 2);

       matrix.set(savedMatrix);
       scale = newDist / oldDist;
       matrix.postScale(scale, scale, bitmapWidth > mViewWidth ? mMiddlePoint.x : midX, bimtapHeight > mViewHeight ? mMiddlePoint.y : midY); 

       this.setImageMatrix(matrix);


   





    /** Determine the space between the first two fingers */
    private float spacing(MotionEvent event) 
        float x = event.getX(0) - event.getX(1);
        float y = event.getY(0) - event.getY(1);

        return (float)Math.sqrt(x * x + y * y);
    

    /** Calculate the mid point of the first two fingers */
    private void midPoint(PointF point, MotionEvent event) 
        float x = event.getX(0) + event.getX(1);
        float y = event.getY(0) + event.getY(1);
        point.set(x / 2, y / 2);
    



【问题讨论】:

没有代码我们是零。 ImageView fillparent/fill parent 在相对布局中应该可以工作 ImageView 已经是 fill_parent。 imageView 覆盖整个屏幕。它的图像不适合图像视图 android:scaleType="fitXY" 我尝试过使用 fitXY。我在问题本身中提到过它 【参考方案1】:

将此添加到布局中的 imageView:

    android:scaleType="centerCrop"

【讨论】:

以上是关于android设置图像适合imageview的主要内容,如果未能解决你的问题,请参考以下文章

如何在android中永久设置壁纸

android:在imageview中拉伸图像以适应屏幕

如何使用相机设置捕获的图像以适合固定高度和宽度的 Imageview

Android,以GridLayoutManager和ImageView为单元格内容的RecyclerView不适合垂直空间

如何加载图像 url 以适合我在 android 中的 webview?

如何在 Android 中设置背景图像并将该图像适合整个卡片以用于卡片视图?