android 自定义圆角ImageView以及锯齿的处理
看到很多人开发过程中要使用圆角图片时,解决方法有:
1.重新绘制一张图片
2.通过布局来配置
3.通过重写View来实现
其中1,2在这里就不讲了,重点讲讲方法三的实现。
实现一:通过截取画布一个圆形区域与图片的相交部分进行绘制,缺点:锯齿明显,设置Paint,Canvas抗锯齿无效。
package com.open.circleimageview.widget
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.PaintFlagsDrawFilter
import android.graphics.Path
import android.graphics.Rect
import android.graphics.Region
import android.util.AttributeSet
import android.view.View
public class CircleImageViewA extends View {
public CircleImageViewA(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle)
}
public CircleImageViewA(Context context, AttributeSet attrs) {
super(context, attrs)
}
public CircleImageViewA(Context context) {
super(context)
}
private Bitmap bitmap
private Rect bitmapRect=new Rect()
private PaintFlagsDrawFilter pdf=new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG)
private Paint paint = new Paint()
{
paint.setStyle(Paint.Style.STROKE)
paint.setFlags(Paint.ANTI_ALIAS_FLAG)
paint.setAntiAlias(true)// 设置画笔的锯齿效果。 true是去除,大家一看效果就明白了
}
private Path mPath=new Path()
public void setImageBitmap(Bitmap bitmap)
{
this.bitmap=bitmap
}
@Override
protected void onDraw(Canvas canvas) {
if(null==bitmap)
{
return
}
bitmapRect.set(0, 0, getWidth(), getHeight())
canvas.save()
canvas.setDrawFilter(pdf)
mPath.reset()
canvas.clipPath(mPath)// makes the clip empty
mPath.addCircle(getWidth()/2, getWidth()/2, getHeight()/2, Path.Direction.CCW)
canvas.clipPath(mPath, Region.Op.REPLACE)
canvas.drawBitmap(bitmap, null, bitmapRect, paint)
canvas.restore()
}
}
实现二:通过PorterDuffXfermode 方式(注意要设置硬件加速,否则部分机子无效),优点:锯齿基本没有
package com.open.circleimageview.widget
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.PaintFlagsDrawFilter
import android.graphics.PorterDuff
import android.graphics.PorterDuffXfermode
import android.graphics.Rect
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
public class CircleImageViewB extends View {
public CircleImageViewB(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle)
init()
}
public CircleImageViewB(Context context, AttributeSet attrs) {
super(context, attrs)
init()
}
public CircleImageViewB(Context context) {
super(context)
init()
}
private Bitmap bitmap
private Rect bitmapRect=new Rect()
private PaintFlagsDrawFilter pdf=new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG)
private Paint paint = new Paint()
{
paint.setStyle(Paint.Style.STROKE)
paint.setFlags(Paint.ANTI_ALIAS_FLAG)
paint.setAntiAlias(true)// 设置画笔的锯齿效果。 true是去除,大家一看效果就明白了
}
private Bitmap mDstB=null
private PorterDuffXfermode xfermode=new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY)
private void init()
{
try {
if(android.os.Build.VERSION.SDK_INT>=11)
{
setLayerType(LAYER_TYPE_SOFTWARE, null)
}
} catch (Exception e) {
e.printStackTrace()
}
}
public void setImageBitmap(Bitmap bitmap)
{
this.bitmap=bitmap
}
private Bitmap makeDst(int w, int h)
{
Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
Canvas c = new Canvas(bm)
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG)
p.setColor(Color.parseColor("#ffffffff"))
c.drawOval(new RectF(0, 0, w, h), p)
return bm
}
@Override
protected void onDraw(Canvas canvas) {
if(null==bitmap)
{
return
}
if(null==mDstB)
{
mDstB=makeDst(getWidth(), getHeight())
}
bitmapRect.set(0, 0, getWidth(), getHeight())
canvas.save()
canvas.setDrawFilter(pdf)
canvas.drawBitmap(mDstB, 0, 0, paint)
paint.setXfermode(xfermode)
canvas.drawBitmap(bitmap, null, bitmapRect, paint)
paint.setXfermode(null)
canvas.restore()
}
}
可以通过shape设置圆角<!-- 设置圆角 -->
<corners android:radius="2dp" >
</corners>
扩展:
<?xml version="1.0" encoding="utf-8"?>
<!-- shape如果不声明形状默认是正方形 -->
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 设置填充色 -->
<solid android:color="#4285f4" >
</solid>
<!-- 设置边框的颜色和宽度 -->
<stroke
android:width="1dp"
android:color="#4285f4" >
</stroke>
</shape>
通过selector设置点击效果
button_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 这个是用于控制按钮组背景的文件 -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- **点击时效果**********背景引用的资源*************************是否获得焦点*********************是否按下******* -->
<item android:drawable="@drawable/button_p" android:state_focused="true" android:state_pressed="false"/>
<item android:drawable="@drawable/button_p" android:state_focused="true" android:state_pressed="true"/>
<item android:drawable="@drawable/button_p" android:state_focused="false" android:state_pressed="true"/>
<!-- **************没有任何 *** 作时显示的背景************** -->
<item android:drawable="@drawable/button_n"></item>
</selector>
在xml文件中设置button的background属性。
android:background="@drawable/button_bg"
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)