【发布时间】:2014-06-05 07:36:27
【问题描述】:
我创建了一个基于this answer 的圆形imageView。 它使图像完美圆润。但是,我有两个问题。
- 图片旋转了 90 度,我不知道为什么(用户单击按钮,用户的图库显示,用户选择图片作为他的个人资料图片)
- 图片有深色背景,我不知道是从哪里来的。
我正在使用的类:
public class RoundImageView extends ImageView {
private Path path;
private Paint paint;
private PorterDuffXfermode porterDuffXfermode;
public RoundImageView(Context context) {
super(context);
init();
}
public RoundImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public RoundImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
setWillNotDraw(false);
path = new Path();
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
porterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.TRANSPARENT);
// Create a circular path.
final float halfWidth = canvas.getWidth()/2;
final float halfHeight = canvas.getHeight()/2;
final float radius = Math.max(halfWidth, halfHeight);
path.addCircle(halfWidth, halfHeight, radius, Path.Direction.CCW);
paint.setXfermode(porterDuffXfermode);
canvas.drawPath(path, paint);
}
}
我在布局中添加它的方式:
<com.allstarxi.widget.RoundImageView
android:layout_width="35dp"
android:layout_height="35dp"
android:src="@drawable/ic_launcher"
android:id="@+id/imageView"
android:contentDescription="@string/general_content_description"
android:scaleType="center" />
这是截图:
【问题讨论】:
-
Hesam ,我得到你的代码并把 mTopCard.setLayerType(LAYER_TYPE_HARDWARE, null);在 init() 方法中并为我工作,我在清单上也有硬件加速标志“true”
标签: android imageview android-imageview