Android Open Source - Android-Sessions Circle Image View






From Project

Back to project page Android-Sessions.

License

The source code is released under:

MIT License

If you think the Android project Android-Sessions 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

package uk.ac.icappsoc.appsoctwo.circleimage;
/*from  ww w.  j  a  va  2  s  .  c om*/
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * A circular ImageView.
 * WARNINGS:
 * - This may not work with an ImageView that is initialized without an image.
 * - If the size of the image does not match the size of this View, the circular
 * image will NOT be centered! To alleviate this, the Bitmap that the BitmapShader
 * is initialized with needs to be scaled to the size of the View (there are multiple
 * ways this may be accomplished).
 * - The shader needs to be updated if the image changes at runtime,
 * e.g. when {@link #setImageBitmap(Bitmap)} is called.
 */
public class CircleImageView extends ImageView {
  
  // A shader that we will use to draw our circle.
  private BitmapShader shader;
  // The paint to which we'll apply the shader.
  private Paint paint;
  
  // Provide the appropriate constructor
  public CircleImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
    
    // Create the BitmapShader, pointing it to the bitmap of the image this ImageView started with
    shader = new BitmapShader(((BitmapDrawable)getDrawable()).getBitmap(), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    // Create our paint and attach the shader to it
    paint = new Paint();
    paint.setAntiAlias(true);
    paint.setShader(shader);
  }
  
  @Override
  public void setImageBitmap(Bitmap bm){
    super.setImageBitmap(bm);

    shader = new BitmapShader(bm, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    paint.setShader(shader);
  }
  
  @Override
  public void onDraw(Canvas canvas){
    canvas.drawCircle(getWidth()/2, getHeight()/2, Math.min(getWidth(), getHeight())/2, paint);
  }
  
}




Java Source Code List

uk.ac.icappsoc.appsocone.MainActivity.java
uk.ac.icappsoc.appsocthree.MainActivity.java
uk.ac.icappsoc.appsocthree.SensorListActivity.java
uk.ac.icappsoc.appsocthree.accel.BouncyBallView.java
uk.ac.icappsoc.appsocthree.accel.Gravity2DActivity.java
uk.ac.icappsoc.appsocthree.accel.ShakeActivity.java
uk.ac.icappsoc.appsocthree.compass.CompassActivity.java
uk.ac.icappsoc.appsocthree.compass.CompassView.java
uk.ac.icappsoc.appsocthree.light.LightClockActivity.java
uk.ac.icappsoc.appsocthree.proximity.ProximityActivity.java
uk.ac.icappsoc.appsoctwo.MainActivity.java
uk.ac.icappsoc.appsoctwo.circleimage.CircleImageActivity.java
uk.ac.icappsoc.appsoctwo.circleimage.CircleImageView.java
uk.ac.icappsoc.appsoctwo.circleimage.SessionTitleActivity.java
uk.ac.icappsoc.appsoctwo.rainbowtv.RainbowTextViewActivity.java
uk.ac.icappsoc.appsoctwo.rainbowtv.RainbowTextView.java
uk.ac.icappsoc.appsoctwo.split.SplitActivity.java
uk.ac.icappsoc.appsoctwo.split.SplitGameView.java