自定义椭圆形控件--积累轮子
2016-12-08 18:57
253 查看
这篇博客这不是讲解如何自定义控件,是怎么使用造好的轮子(好烦啊,博客竟然不能上传小附件,只能通过上传资源的方式)点我下载
资源文件中有两个类和一个xml文件,将其放入项目相对位置,然后就是在布局中引入CircleImageView这个类。
设置属性时,设置不同宽高(实际上确定椭圆的四个顶点),可以得到不同的椭圆,如图:
不想下载的话,直接复制下面代码
如果res/values没有attrs.xml 这个文件,就先创建,再复制下面的代码进去
下面两个类复制到java包相应位置下
资源文件中有两个类和一个xml文件,将其放入项目相对位置,然后就是在布局中引入CircleImageView这个类。
设置属性时,设置不同宽高(实际上确定椭圆的四个顶点),可以得到不同的椭圆,如图:
不想下载的话,直接复制下面代码
如果res/values没有attrs.xml 这个文件,就先创建,再复制下面的代码进去
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="CircleImageView"> <attr name="shape" format="integer"> <enum name="circle" value="1" /> <enum name="rectangle" value="2" /> <enum name="svg" value="3" /> </attr> <attr name="svg_raw_resource" format="reference" /> </declare-styleable> </resources>
下面两个类复制到java包相应位置下
import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.graphics.Xfermode; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.widget.ImageView; import java.lang.ref.WeakReference; public abstract class BaseImageView extends ImageView { protected Context mContext; private static final Xfermode sXfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN); private Bitmap mMaskBitmap; private Paint mPaint; private WeakReference<Bitmap> mWeakBitmap; public BaseImageView(Context context) { super(context); sharedConstructor(context); } public BaseImageView(Context context, AttributeSet attrs) { super(context, attrs); sharedConstructor(context); } public BaseImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); sharedConstructor(context); } private void sharedConstructor(Context context) { mContext = context; mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); } public void invalidate() { mWeakBitmap = null; if (mMaskBitmap != null) { mMaskBitmap.recycle(); } super.invalidate(); } @SuppressLint("DrawAllocation") @Override protected void onDraw(Canvas canvas) { if (!isInEditMode()) { int i = canvas.saveLayer(0.0f, 0.0f, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG); try { Bitmap bitmap = mWeakBitmap != null ? mWeakBitmap.get() : null; if (bitmap == null || bitmap.isRecycled()) { Drawable drawable = getDrawable(); if (drawable != null) { bitmap = Bitmap.createBitmap(getWidth(),getHeight(), Bitmap.Config.ARGB_8888); Canvas bitmapCanvas = new Canvas(bitmap); drawable.setBounds(0, 0, getWidth(), getHeight()); drawable.draw(bitmapCanvas); if (mMaskBitmap == null || mMaskBitmap.isRecycled()) { mMaskBitmap = getBitmap(); } mPaint.reset(); mPaint.setFilterBitmap(false); mPaint.setXfermode(sXfermode); bitmapCanvas.drawBitmap(mMaskBitmap, 0.0f, 0.0f, mPaint); mWeakBitmap = new WeakReference<Bitmap>(bitmap); } } if (bitmap != null) { mPaint.setXfermode(null); canvas.drawBitmap(bitmap, 0.0f, 0.0f, mPaint); return; } } catch (Exception e) { System.gc(); } finally { canvas.restoreToCount(i); } } else { super.onDraw(canvas); } } public abstract Bitmap getBitmap(); }
import android.content.Context; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.util.AttributeSet; public class CircleImageView extends BaseImageView { public static class Shape { public static final int CIRCLE = 1; public static final int RECTANGLE = 2; public static final int SVG = 3; } private int mShape = Shape.CIRCLE; public CircleImageView(Context context) { super(context); } public CircleImageView(Context context, int resourceId, int shape, int svgRawResourceId) { this(context); setImageResource(resourceId); mShape = shape; } public CircleImageView(Context context, AttributeSet attrs) { super(context, attrs); sharedConstructor(context, attrs); } public CircleImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); sharedConstructor(context, attrs); } private void sharedConstructor(Context context, AttributeSet attrs) { TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircleImageView); mShape = a.getInt(R.styleable.CircleImageView_shape, Shape.CIRCLE); a.recycle(); } public Bitmap getBitmap(int width, int height) { Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(Color.BLACK); canvas.drawOval(new RectF(0.0f, 0.0f, width, height), paint); return bitmap; } @Override public Bitmap getBitmap() { switch (mShape) { case Shape.CIRCLE: return getBitmap(getWidth(), getHeight()); } return null; } }
相关文章推荐
- android自定义控件及自定义组合控件
- android 自定义显示三角型控件
- Asp.net 2.0 自定义控件开发[创建自定义浮动菜单FloadMenu控件][示例代码下载]
- C#自定义工业控件开发
- 自定义分页控件
- 自定义.Net calendar控件
- 滑动控件自定义使用
- Android 好看的自定义滚动式日期选择控件
- asp.net 自定义服务器控件权威资料
- Android自定义View 自定义组合控件
- 自定义asp.net控件开发之(五)-UpdownPanel控件实例
- 自定义宽高比例的布局控件
- android 自定义ImageView控件实现圆形图片-适用于用户头像
- Ext.Net 1.2.0_在 Web 应用程序中自定义 TreePanel 控件
- SlidingMenu源码分析-附自定义底部滑出菜单控件
- 【Jquery】【控件】flexigrid 自定义查询
- android自定义控件之滚动广告条
- Android自定义圆角ImageView控件
- iOS开发UI篇—Quartz2D(自定义UIImageView控件)
- Asp.net MVC3 自定义HtmlHelper控件