Created
January 3, 2014 06:50
-
-
Save NLMartian/8233902 to your computer and use it in GitHub Desktop.
CircleImageView
用Shader新写的控件,做圆形头像用。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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