Android Open Source - RoundImageView New Round Image View






From Project

Back to project page RoundImageView.

License

The source code is released under:

Apache License

If you think the Android project RoundImageView listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2015 The Android Open Source Project.
 */* w  w  w. j  a v a  2  s.  c o m*/
 *        yinglovezhuzhu@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.opensource.widget;


import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

/**
 * 
 * ???????ImageView??????????????????<br>
 * 
 * <p>???????????????????Path????Path.FillType.INVERSE_WINDING?????????????
 * ?????????????<br>
 * ???????????ImageView??????????????????????????????????<br>
 * ?????????????????API 11?????????????<br>
 *   
 * <p><font color="#FF0000">
 * ??????????API 11???????????????????<br><br>
 * ???????????????????????
 * ???????????????{@link ImageView.ScaleType#CENTER_CROP},??????????????????
 * ???????????????????????????????????????????????????????
 * ?????????????????????????????????
 * </font>
 * 
 * @author yinglovezhuzhu@gmail.com
 * 
 * @version 1.0
 * 
 */
@TargetApi(value = 11)
public class NewRoundImageView extends BaseRoundImageView {

  private final Paint mPaint = new Paint();
  
  private final Path mPath = new Path();

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

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

  public NewRoundImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
  }

  
    private void init() {  
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        mPaint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
        mPath.setFillType(Path.FillType.INVERSE_WINDING);
    }  
  
  @Override
  protected void onDraw(Canvas canvas) {
      if(mCornerRate <= DEFAULT_CORNER_RATE && mCornerRadius <= DEFAULT_CORNER_RADIUS) {
        // ??????????????????????
        super.onDraw(canvas);
        return;
      }

    if(mViewWidth <= 0 || mViewHeight <= 0) {
      super.onDraw(canvas);
      return;
    }
    
    int diameter = mViewWidth > mViewHeight ? mViewHeight : mViewWidth;
    
    mDrawRect.set(0, 0, mViewWidth, mViewHeight);
    
    float radius = 0;
    if(mCornerRate > DEFAULT_CORNER_RATE) { // ????
      radius = ((float) diameter) / mCornerRate;
    } else if(mCornerRadius > DEFAULT_CORNER_RADIUS) { // ???????
      float halfDiameter = ((float) diameter) / 2;
      // ????????????????????????????????????
      radius = mCornerRadius > halfDiameter ? halfDiameter : mCornerRadius;
    } else { // ????????????
      super.onDraw(canvas);
      return;
    }
    
    mPaint.setAntiAlias(true);
    
    if(mBorderThickness != DEFAUTL_BORDER_THICKNESS) { // ????
      
      float imageRadius = radius - mBorderThickness;
      
      // ????
      drawRoundColorShape(canvas, mDrawRect, radius, mBorderColor);
      
      // ??????
          mTempRect.set(mDrawRect.left + mBorderThickness, mDrawRect.top + mBorderThickness, 
              mDrawRect.right - mBorderThickness, mDrawRect.bottom - mBorderThickness);
          drawRoundColorShape(canvas, mTempRect, imageRadius, mFillColor);
          
          
          // ??????layer?????????????????????????255??layer 
          // ????
          int saveCount = canvas.saveLayerAlpha(0.0F, 0.0F, mViewWidth, mViewHeight, 
              255, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG);
          super.onDraw(canvas);
          canvas.drawPath(mPath, mPaint);
          canvas.restoreToCount(saveCount);
      
    } else if(mBorderInsideThickness != DEFAUTL_BORDER_THICKNESS 
        || mBorderOutsideThickness != DEFAUTL_BORDER_THICKNESS) { // ????
      
      float imageRadius = radius - mBorderInsideThickness - mBorderOutsideThickness;
      
      if(mBorderInsideThickness == DEFAUTL_BORDER_THICKNESS) { // ??????
        // ????
        drawRoundColorShape(canvas, mDrawRect, radius, mBorderOutsideColor);
        
        // ??????
            mTempRect.set(mDrawRect.left + mBorderOutsideThickness, mDrawRect.top + mBorderOutsideThickness, 
                mDrawRect.right - mBorderOutsideThickness, mDrawRect.bottom - mBorderOutsideThickness);
            drawRoundColorShape(canvas, mTempRect, imageRadius, mFillColor);
            
            
            // ??????layer?????????????????????????255??layer 
            // ????
            int saveCount = canvas.saveLayerAlpha(0.0F, 0.0F, mViewWidth, mViewHeight, 
                255, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG);
            super.onDraw(canvas);
            canvas.drawPath(mPath, mPaint);
            canvas.restoreToCount(saveCount);
      } else if(mBorderOutsideThickness == DEFAUTL_BORDER_THICKNESS) { // ??????
        // ????
        drawRoundColorShape(canvas, mDrawRect, radius, mBorderOutsideColor);
        
        // ???????
        mTempRect.set(mDrawRect.left + mBorderOutsideThickness, mDrawRect.top + mBorderOutsideThickness, 
                mDrawRect.right - mBorderOutsideThickness, mDrawRect.bottom - mBorderOutsideThickness);
            drawRoundColorShape(canvas, mTempRect, imageRadius, mBorderInsideColor);
            
            // ??????layer?????????????????????????255??layer 
            // ????
            int saveCount = canvas.saveLayerAlpha(0.0F, 0.0F, mViewWidth, mViewHeight, 
                255, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG);
            super.onDraw(canvas);
            canvas.drawPath(mPath, mPaint);
            canvas.restoreToCount(saveCount);
      } else { // ????
        
        // ?????
        drawRoundColorShape(canvas, mDrawRect, radius, mBorderOutsideColor);
        
        // ?????
            mTempRect.set(mDrawRect.left + mBorderOutsideThickness, mDrawRect.top + mBorderOutsideThickness, 
                mDrawRect.right - mBorderOutsideThickness, mDrawRect.bottom - mBorderOutsideThickness);
            drawRoundColorShape(canvas, mTempRect, radius - mBorderOutsideThickness, mBorderInsideColor);
        
        // ???????
            mTempRect.set(mTempRect.left + mBorderInsideThickness, mTempRect.top + mBorderInsideThickness, 
                mTempRect.right - mBorderInsideThickness, mTempRect.bottom - mBorderInsideThickness);
            drawRoundColorShape(canvas, mTempRect, imageRadius, mFillColor);
            
            // ??????layer?????????????????????????255??layer 
            // ????
            int saveCount = canvas.saveLayerAlpha(0.0F, 0.0F, mViewWidth, mViewHeight, 
                255, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG);
            super.onDraw(canvas);
            canvas.drawPath(mPath, mPaint);
            canvas.restoreToCount(saveCount);
      }
    } else { // ??
      // ???????
      drawRoundColorShape(canvas, mDrawRect, radius, mFillColor);
      
          // ??????layer?????????????????????????255??layer 
          // ????
          int saveCount = canvas.saveLayerAlpha(0.0F, 0.0F, mViewWidth, mViewHeight, 
              255, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG);
          super.onDraw(canvas);
          canvas.drawPath(mPath, mPaint);
          canvas.restoreToCount(saveCount);
    }
  }
  
  /**
   * ??????
   * @param canvas
   * @param rect
   * @param radius
   * @param color
   */
  private void drawRoundColorShape(Canvas canvas, RectF rect, float radius, int color) {
    mPath.reset();
      mPath.addRoundRect(rect, radius, radius, Path.Direction.CW);  
      
        // ??????layer?????????????????????????255??layer
        int saveCount = canvas.saveLayerAlpha(0.0F, 0.0F, mViewWidth, mViewHeight, 
            255, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG);
        canvas.drawColor(color);
        canvas.drawPath(mPath, mPaint);
        canvas.restoreToCount(saveCount);
  }
}




Java Source Code List

com.opensource.roundimageview.demo.MainActivity.java
com.opensource.widget.BaseRoundImageView.java
com.opensource.widget.NewRoundImageView.java
com.opensource.widget.RoundImageView.java