Android Open Source - RoundImageView Base 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.
 */*from  w  w w.ja  v  a 2s  .c om*/
 *        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.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * {@link RoundImageView}????????????????????????
 * 
 * @author yinglovezhuzhu@gmail.com
 * 
 * @version 1.0
 * 
 */
public class BaseRoundImageView extends ImageView {

  protected final int DEFAULT_CORNER_RADIUS = 0;
  
  protected final int DEFAULT_CORNER_RATE = 1;

  // ???????????
  protected final int DEFAULT_COLOR = 0xFFFFFFFF;
  protected final int DEFAUTL_BORDER_THICKNESS = 0;
  
  // ????
  protected int mBorderThickness = DEFAUTL_BORDER_THICKNESS;
  protected int mBorderInsideThickness = DEFAUTL_BORDER_THICKNESS;
  protected int mBorderOutsideThickness = DEFAUTL_BORDER_THICKNESS;

  // ????
  protected int mBorderColor = DEFAULT_COLOR;
  protected int mBorderInsideColor = DEFAULT_COLOR;
  protected int mBorderOutsideColor = DEFAULT_COLOR;
  protected int mFillColor = DEFAULT_COLOR;

  // ImageView???
  protected int mViewWidth;
  protected int mViewHeight;
  
  /** ??????? **/
  protected int mCornerRadius = DEFAULT_CORNER_RADIUS;
  
  /** ?????????????? **/
  protected int mCornerRate = DEFAULT_CORNER_RATE;
  
  /** ????? **/
  protected final RectF mDrawRect = new RectF();
  /** ???????????RectF?? **/
  protected final RectF mTempRect = new RectF();

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

  public BaseRoundImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.setCustomAttributes(attrs);
  }

  public BaseRoundImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    this.setCustomAttributes(attrs);
  }

  /**
   * ?????
   * @param attrs
   */
  private void setCustomAttributes(AttributeSet attrs) {
    TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.roundimageview);
    
    mCornerRate = typedArray.getInt(R.styleable.roundimageview_corner_rate, DEFAULT_CORNER_RATE);
    if(mCornerRate <= DEFAULT_CORNER_RATE) {
      // xml?????????????????????????????????????????
      mCornerRadius  = typedArray.getDimensionPixelSize(R.styleable.roundimageview_corner_radius, DEFAULT_CORNER_RADIUS);
      mCornerRate = DEFAULT_CORNER_RATE;
    }
    
    mBorderThickness = typedArray.getDimensionPixelSize(
        R.styleable.roundimageview_border_thickness, DEFAUTL_BORDER_THICKNESS);
    if(mBorderThickness <= DEFAUTL_BORDER_THICKNESS) {
      // xml?????????????????????????????????????????????????????
      mBorderInsideThickness = typedArray.getDimensionPixelSize(
          R.styleable.roundimageview_border_inside_thickness, DEFAUTL_BORDER_THICKNESS);
      mBorderOutsideThickness = typedArray.getDimensionPixelSize(
          R.styleable.roundimageview_border_outside_thickness, DEFAUTL_BORDER_THICKNESS);
      mBorderThickness = DEFAUTL_BORDER_THICKNESS;
    }
    
    mBorderColor = typedArray.getColor(
        R.styleable.roundimageview_border_color, DEFAULT_COLOR);
    mBorderInsideColor = typedArray.getColor(
        R.styleable.roundimageview_border_inside_color,DEFAULT_COLOR);
    mBorderOutsideColor = typedArray.getColor
        (R.styleable.roundimageview_border_outside_color, DEFAULT_COLOR);
    
    // xml???????????????????
    if (mBorderInsideColor == DEFAULT_COLOR) {
      mBorderInsideColor = mBorderColor;
    }
    if (mBorderOutsideColor == DEFAULT_COLOR) {
      mBorderOutsideColor = mBorderColor;
    }
    
    mFillColor = typedArray.getColor(R.styleable.roundimageview_fill_color, DEFAULT_COLOR);
    
    typedArray.recycle();
  }
  
  /**
   * ?????????????????????????????????????????????????xml??????<br>
   * ???????????????????????????????????????1/2????????
   * 
   * @param cornerRadius
   * 
   * @see {@link #setCornerRate(int)}} ????????
   */
  public void setCornerRadius(int cornerRadius) {
    this.mCornerRadius = cornerRadius;
    this.mCornerRate = DEFAULT_CORNER_RATE;
    postInvalidate();
   }
  
  /**
   * ???????????????????????????????????????????????xml??????<br>
   * <p>???????????????????????????????????????????????<br>
   * ??????????r = min(width, height) / rate?????2???????
   * @param cornerRate
   * 
   * @see {@link #setCornerRadius(int)}} ??????????
   */
  public void setCornerRate(int cornerRate) {
    this.mCornerRate = cornerRate;
    this.mCornerRadius = DEFAULT_CORNER_RADIUS;
    postInvalidate();
  }

  /**
   * ??????
   * @param borderThickness
   */
  public void setBorderThickness(int borderThickness) {
    this.mBorderThickness = borderThickness;
    if(mBorderThickness > DEFAUTL_BORDER_THICKNESS) {
      mBorderOutsideThickness = DEFAUTL_BORDER_THICKNESS;
      mBorderInsideThickness = DEFAUTL_BORDER_THICKNESS;
    }
    postInvalidate();
  }

  /**
   * ???????
   * @param borderInsideThickness
   */
  public void setBorderInsideThickness(int borderInsideThickness) {
    this.mBorderInsideThickness = borderInsideThickness;
    if(mBorderInsideThickness > DEFAUTL_BORDER_THICKNESS) {
      mBorderThickness = DEFAUTL_BORDER_THICKNESS;
    }
    postInvalidate();
  }

  /**
   * ???????
   * @param borderOutsideThickness
   */
  public void setBorderOutsideThickness(int borderOutsideThickness) {
    this.mBorderOutsideThickness = borderOutsideThickness;
    if(mBorderOutsideThickness > DEFAUTL_BORDER_THICKNESS) {
      mBorderThickness = DEFAUTL_BORDER_THICKNESS;
    }
    postInvalidate();
  }

  /**
   * ??????
   * @param borderColor
   */
  public void setBorderColor(int borderColor) {
    this.mBorderColor = borderColor;
    this.mBorderOutsideColor = borderColor;
    this.mBorderInsideColor = borderColor;
    postInvalidate();
  }

  /**
   * ???????
   * @param borderInsideColor
   */
  public void setBorderInsideColor(int borderInsideColor) {
    this.mBorderInsideColor = borderInsideColor;
    postInvalidate();
  }

  /**
   * ???????
   * @param borderOutsideColor
   */
  public void setBorderOutsideColor(int borderOutsideColor) {
    this.mBorderOutsideColor = borderOutsideColor;
    postInvalidate();
  }
  
  /**
   * ??????????<br>
   * ???????????????????????????????
   * @param fillColor
   */
  public void setFillColor(int fillColor) {
    this.mFillColor = fillColor;
    postInvalidate();
  }

    @Override  
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {  
        super.onSizeChanged(w, h, oldw, oldh);  
        if ((w != oldw) || (h != oldh)) {
          mViewWidth = w;
          mViewHeight = h;
        }
    }  
  
  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
  }
}




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