80,352
社区成员
发帖
与我相关
我的任务
分享
package com.tanyixiu.memo.widget;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;
public class RoundImageView extends ImageView {
public RoundImageView(Context context) {
super(context);
}
public RoundImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public RoundImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int wSize = MeasureSpec.getSize(widthMeasureSpec);
int hSize = MeasureSpec.getSize(heightMeasureSpec);
int min = Math.min(wSize, hSize);
setMeasuredDimension(min, min);
Log.v("tanyixiu", "wSize=" + wSize + ";hSize="+hSize);
}
@Override
protected void onDraw(Canvas canvas) {
Drawable d = getDrawable();
if (null == d || !(d instanceof BitmapDrawable)) {
super.onDraw(canvas);
return;
}
int wCanvas = getWidth();
int hCanvas = getHeight();
if (wCanvas != hCanvas) {
float minCanvas = Math.min(wCanvas, hCanvas);
canvas.translate((wCanvas - minCanvas) / 2, (hCanvas - minCanvas) / 2);
}
ShapeDrawable shape = getRoundShapeDrawable((BitmapDrawable) d);
shape.draw(canvas);
}
private ShapeDrawable getRoundShapeDrawable(BitmapDrawable d) {
ShapeDrawable shapeDrawable = new ShapeDrawable(new OvalShape());
Bitmap bitmap = getBitmap(d);
BitmapShader bitmapShader = new BitmapShader(bitmap, TileMode.MIRROR, TileMode.MIRROR);
shapeDrawable.getPaint().setShader(bitmapShader);
int min = Math.min(getWidth(), getHeight());
shapeDrawable.setBounds(0, 0, min, min);
return shapeDrawable;
}
private Bitmap getBitmap(BitmapDrawable d) {
Bitmap source = d.getBitmap();
int sourceW = source.getWidth();
int sourceH = source.getHeight();
Bitmap squear = null;
if (sourceW != sourceH) {
int min = Math.min(sourceW, sourceH);
squear = Bitmap.createBitmap(source, (sourceW - min) / 2, (sourceH - min) / 2, min, min);
} else {
squear = source;
}
int minCanvas = Math.min(getWidth(), getHeight());
int minBitmap = Math.min(squear.getWidth(), squear.getHeight());
float rate = minCanvas / (float) minBitmap;
Matrix matrix = new Matrix();
matrix.setScale(rate, rate);
Bitmap result = Bitmap.createBitmap(squear, 0, 0, squear.getWidth(), squear.getHeight(), matrix, true);
return result;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<com.tanyixiu.memo.widget.RoundImageView
android:id="@+id/main_roundImg"
android:layout_width="200dp"
android:layout_height="100dp"
android:background="#FFF000"
android:src="@drawable/headphoto" >
</com.tanyixiu.memo.widget.RoundImageView>
</RelativeLayout>