android自定义一圆角ImageView
2012-09-13 16:25
369 查看
java类如下:
import android.content.Context; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.graphics.RectF; import android.util.AttributeSet; import android.widget.ImageView; import cn.dotcreate.tt.R; public class RoundAngleImageView extends ImageView { private Paint paint; private int roundWidth = 5; private int roundHeight = 5; private Paint paint2; public RoundAngleImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(context, attrs); } public RoundAngleImageView(Context context, AttributeSet attrs) { super(context, attrs); init(context, attrs); } public RoundAngleImageView(Context context) { super(context); init(context, null); } private void init(Context context, AttributeSet attrs) { if(attrs != null) { TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundAngleImageView); roundWidth= a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundWidth, roundWidth); roundHeight= a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundHeight, roundHeight); }else { float density = context.getResources().getDisplayMetrics().density; roundWidth = (int) (roundWidth*density); roundHeight = (int) (roundHeight*density); } paint = new Paint(); paint.setColor(Color.WHITE); paint.setAntiAlias(true); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT)); paint2 = new Paint(); paint2.setXfermode(null); } @Override public void draw(Canvas canvas) { Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888); Canvas canvas2 = new Canvas(bitmap); super.draw(canvas2); drawLiftUp(canvas2); drawRightUp(canvas2); drawLiftDown(canvas2); drawRightDown(canvas2); canvas.drawBitmap(bitmap, 0, 0, paint2); bitmap.recycle(); } private void drawLiftUp(Canvas canvas) { Path path = new Path(); path.moveTo(0, roundHeight); path.lineTo(0, 0); path.lineTo(roundWidth, 0); path.arcTo(new RectF( 0, 0, roundWidth*2, roundHeight*2), -90, -90); path.close(); canvas.drawPath(path, paint); } private void drawLiftDown(Canvas canvas) { Path path = new Path(); path.moveTo(0, getHeight()-roundHeight); path.lineTo(0, getHeight()); path.lineTo(roundWidth, getHeight()); path.arcTo(new RectF( 0, getHeight()-roundHeight*2, 0+roundWidth*2, getHeight()), 90, 90); path.close(); canvas.drawPath(path, paint); } private void drawRightDown(Canvas canvas) { Path path = new Path(); path.moveTo(getWidth()-roundWidth, getHeight()); path.lineTo(getWidth(), getHeight()); path.lineTo(getWidth(), getHeight()-roundHeight); path.arcTo(new RectF( getWidth()-roundWidth*2, getHeight()-roundHeight*2, getWidth(), getHeight()), 0, 90); path.close(); canvas.drawPath(path, paint); } private void drawRightUp(Canvas canvas) { Path path = new Path(); path.moveTo(getWidth(), roundHeight); path.lineTo(getWidth(), 0); path.lineTo(getWidth()-roundWidth, 0); path.arcTo(new RectF( getWidth()-roundWidth*2, 0, getWidth(), 0+roundHeight*2), -90, 90); path.close(); canvas.drawPath(path, paint); } }定义一个attr.xml的文件,放在values目录下面,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="RoundAngleImageView">
<attr name="roundWidth" format="dimension" />
<attr name="roundHeight" format="dimension" />
</declare-styleable>
</resources>
使用示例如下:
先要声明属性的名字空间:
然后再写跟一般定义View一样:
<cn.dotcreate.tt.ui.RoundAngleImageView
android:id="@+id/headIV"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_centerVertical="true"
android:layout_marginLeft="2dp"
app:roundWidth="10dp"
app:roundHeight="10dp"
android:src="@drawable/default_head_icon" />
效果如图:
相关文章推荐
- Android使用Xfermode图形渲染方法实现自定义圆形、圆角和椭圆ImageView
- Android自定义ImageView实现图片圆形 ,椭圆和矩形圆角显示
- Android开发之自定义圆角矩形图片ImageView的实现 - Jamy Cai
- android 自定义圆角imageview
- Android自定义View(ImageView圆角)
- Android 自定义ImageView实现圆角/圆形 附加OnTouchListener详细注释以及Button圆角
- Android自定义ImageView实现图片缩放滑动,双击放大缩小,多点触控旋转,圆角矩形,圆形和仿刮刮卡效果
- Android自定义圆角ImageView 支持网络图片
- Android 自定义ImageView实现圆角/圆形 附加OnTouchListener详细注释以及Button圆角
- android自定义一圆角ImageView
- Android imageView 自定义单个或多个角为圆角及圆形图片工具
- 轻量级自定义圆角ImageView-适配-Android-L
- android 自定义View开发实战(四) 圆角矩形ImageView实现
- [置顶] android 自定义圆角ImageView以及锯齿的处理
- Android自定义圆角ImageView 支持网络图片
- android自定义圆角矩形Imageview
- android自定义一圆角ImageView
- Android自定义圆角矩形图片ImageView
- Android:自定义ImageView展示大图中的指定区域并切圆角(雪碧图)
- Android自定义ImageView实现图片圆形 ,椭圆和矩形圆角显示