android ImageView遇到的问题

来源:互联网 发布:javascript url编码 编辑:程序博客网 时间:2024/05/25 18:11

1.ImageView显示图片的圆形切图

     解决方法:

      重写ImageView.onDraw(Canvas) ,把Canvas剪切成圆形,再在上面画图片 
        protected void onDraw(Canvas canvas) {
               intradius =this.getWidth() / 2;
               Pathpath =new Path();
               path.addCircle(radius,radius,radius, Path.Direction.CW);
               canvas.clipPath(path);// 裁剪区域,裁剪成圆形
               canvas.save();
               if (strokeWidth > 0) {
                    canvas.drawColor(strokeColor);
                    path.addCircle(radius -strokeWidth,radius -strokeWidth,radius
                              +strokeWidth, Path.Direction.CW);
                    canvas.clipPath(path);
               canvas.save();
               }
               super.onDraw(canvas);
       }


2.ImageView显示图片的不同部分

       ImageView的setScaleType()方法,提供了几种显示图片的不同部分

          ScaleType.FIT_XY: 
                           mDrawable.setBounds(0, 0, vwidth, vheight);
                           mDrawMatrix = null;
         ScaleType.MATRIX:
                         mDrawable.setBounds(0, 0, dwidth,height);
                         mDrawMatrix = mMatrix;
        ScaleType.CENTER:
                         mDrawable.setBounds(0, 0, dwidth,height);
                         mDrawMatrix = mMatrix;
                         mDrawMatrix.setTranslate((int) ((vwidth - dwidth) * 0.5f + 0.5f),
                                         (int) ((vheight - dheight) * 0.5f + 0.5f));


      ScaleType.CENTER_CROP :
                mDrawMatrix = mMatrix;
                float scale;
                float dx = 0, dy = 0;
                if (dwidth * vheight > vwidth * dheight) {
                    scale = (float) vheight / (float) dheight; 
                    dx = (vwidth - dwidth * scale) * 0.5f;
                } else {
                    scale = (float) vwidth / (float) dwidth;
                    dy = (vheight - dheight * scale) * 0.5f;
                }
                mDrawMatrix.setScale(scale, scale);     
                mDrawMatrix.postTranslate((int) (dx + 0.5f), (int) (dy + 0.5f));


     ScaleType.FIT_START/FIT_END/FIT_CENTER:
               mTempSrc.set(0, 0, dwidth, dheight);
               mTempDst.set(0, 0, vwidth, vheight);             
               mDrawMatrix = mMatrix;
                     mDrawMatrix.setRectToRect(mTempSrc, mTempDst, scaleTypeToScaleToFit(mScaleType));

上面的代码是在 configureBounds()中,而configureBounds()是private方法,主要是修改mDrawMatrix,mDrawable
          这两个变量在onDraw()方法中
              if (mDrawMatrix != null) {
                    canvas.concat(mDrawMatrix);
              }
              mDrawable.draw(canvas);



        如果想要下图效果:


                       
               scale = (float) vheight / (float) dheight;
               matrix.setScale(scale, scale);

    怎样才能把代码加进去?
        1.自定义ImageView类,把ImageView的代码全部粘出来的,修改configureBounds(),相对来说比较麻烦
        2.继承ImageView,在setFrame,setImageXXX中调用一个自定义方法,把代码加进去。

             点击打开链接              


0 0
原创粉丝点击