Skip to content

Instantly share code, notes, and snippets.

@NLMartian
Created January 3, 2014 06:50
Show Gist options
  • Save NLMartian/8233902 to your computer and use it in GitHub Desktop.
Save NLMartian/8233902 to your computer and use it in GitHub Desktop.
CircleImageView 用Shader新写的控件,做圆形头像用。
package org.example.ShaderTest;
import android.content.Context;
import android.graphics.*;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
/**
* Created by jgzhu on 14-1-2.
*/
public class CircleImageView extends ImageView {
public CircleImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CircleImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onDraw(Canvas canvas) {
Drawable drawable = getDrawable();
int width = getWidth();
int height = getHeight();
if (drawable == null) {
return;
}
if (width == 0 || height == 0) {
return;
}
Bitmap b = ((BitmapDrawable) drawable).getBitmap();
Bitmap bitmap = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), getImageMatrix(), true);
BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(shader);
int radius = width < height ? width / 2 : height / 2;
canvas.drawCircle(width / 2, height / 2, radius, paint);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment