Android实用控件自定义逼真相机光圈View
最近手机界开始流行双摄像头,大光圈功能也应用而生。所谓大光圈功能就是能够对照片进行后期重新对焦,其实现的原理主要是对拍照期间获取的深度图片与对焦无穷远的图像通过算法来实现重新对焦的效果。
在某双摄手机的大光圈操作界面有个光圈的操作图标,能够模拟光圈调节时的真实效果,感觉还不错,于是想着实现该效果。现在把我的实现方法贡献给大家,万一你们公司也要做双摄手机呢?( ̄┰ ̄*)
首先,百度一下光圈图片,观察观察,就可以发现其关键在于计算不同的光圈值时各个光圈叶片的位置。为了计算简便,我以六个直边叶片的光圈效果为例来实现(其他形式,比如七个叶片,也就是位置计算稍微没那么方便;而一些圆弧的叶片,只要满足叶片两边的圆弧半径是一样的就行。为什么要圆弧半径一样呢?仔细观察就可以发现,相邻两叶片之间要相互滑动,而且要保持一样的契合距离,根据我曾今小学几何科打满分的经验可以判断出,等径的圆弧是不错滴,其他高级曲线能不能实现该效果,请问数学家( ̄┰ ̄*)!其他部分原理都是一样的)。
制作效果图:
先说明一下本自定义view的主要内容:
1.本效果的实现就是在光圈内六边形六个角上分别绘制六个光圈叶片
2.根据不同的光圈值计算出内六边形的大小,从而计算每个六边形的顶点的位置
3.设计叶片。也可以让美工mm提供,本方案是自己用代码画的。注意预留叶片之间的间隔距离以及每个叶片的角度为60°
4.定义颜色、间隔等自定义属性
5.上下滑动可以调节光圈大小
6.提供光圈值变动的监听接口
代码
可以在github上下载:https://github.com/willhua/cameraaperture.git
package com.example.cameraaperture; 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.paint; import android.graphics.path; import android.graphics.pointf; import android.util.attributeset; import android.util.log; import android.view.motionevent; import android.view.view; /** * 上下滑动可以调节光圈大小; * 调用setaperturechangedlistener设置光圈值变动监听接口; * 绘制的光圈最大直径将填满整个view * @author willhua http://www.cnblogs.com/willhua/ * */ public class apertureview extends view { public interface aperturechanged { public void onaperturechanged(float newapert); } private static final float rotate_angle = 30; private static final string tag = "apertureview"; private static final float cos_30 = 0.866025f; private static final int width = 100; // 当设置为wrap_content时测量大小 private static final int height = 100; private int mcircleradius; private int mbladecolor; private int mbackgroundcolor; private int mspace; private float mmaxapert = 1; private float mminapert = 0.2f; private float mcurrentapert = 0.5f; //利用pointf而不是point可以减少计算误差,以免叶片之间间隔由于计算误差而不均衡 private pointf[] mpoints = new pointf[6]; private bitmap mblade; private paint mpaint; private path mpath; private aperturechanged maperturechanged; private float mprevx; private float mprevy; public apertureview(context context, attributeset attrs) { super(context, attrs); init(context, attrs); } private void init(context context, attributeset attrs) { //读取自定义布局属性 typedarray array = context.obtainstyledattributes(attrs, r.styleable.apertureview); mspace = (int)array.getdimension(r.styleable.apertureview_blade_space, 5); mbladecolor = array.getcolor(r.styleable.apertureview_blade_color, 0xff000000); mbackgroundcolor = array.getcolor(r.styleable.apertureview_background_color, 0xffffffff); array.recycle(); mpaint = new paint(paint.filter_bitmap_flag | paint.dither_flag); mpaint.setantialias(true); for (int i = 0; i < 6; i++) { mpoints[i] = new pointf(); } } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { super.onmeasure(widthmeasurespec, heightmeasurespec); int widthspecmode = measurespec.getmode(widthmeasurespec); int heightspecmode = measurespec.getmode(heightmeasurespec); int widthspecsize = measurespec.getsize(widthmeasurespec); int heightspecsize = measurespec.getsize(heightmeasurespec); int paddx = getpaddingleft() + getpaddingright(); int paddy = getpaddingtop() + getpaddingbottom(); //光圈的大小要考虑减去view的padding值 mcircleradius = widthspecsize - paddx < heightspecsize - paddy ? (widthspecsize - paddx) / 2 : (heightspecsize - paddy) / 2; //对布局参数为wrap_content时的处理 if (widthspecmode == measurespec.at_most && heightspecmode == measurespec.at_most) { setmeasureddimension(width, height); mcircleradius = (width - paddx) / 2; } else if (widthspecmode == measurespec.at_most) { setmeasureddimension(width, heightspecsize); mcircleradius = width - paddx < heightspecsize - paddy ? (width - paddx) / 2 : (heightspecsize - paddy) / 2; } else if (heightspecmode == measurespec.at_most) { setmeasureddimension(widthspecsize, height); mcircleradius = widthspecsize - paddx < height - paddy ? (widthspecsize - paddx) / 2 : (height - paddy) / 2; } if (mcircleradius < 1) { mcircleradius = 1; } //measure之后才能知道所需要绘制的光圈大小 mpath = new path(); mpath.addcircle(0, 0, mcircleradius, path.direction.cw); createblade(); } @override public void ondraw(canvas canvas) { canvas.save(); calculatepoints(); //先把canbvas平移到view的中间 canvas.translate(getwidth() / 2, getheight() / 2); //让光圈的叶片整体旋转,更加贴合实际 canvas.rotate(rotate_angle * (mcurrentapert - mminapert) / (mmaxapert - mminapert)); canvas.clippath(mpath); canvas.drawcolor(mbackgroundcolor); for (int i = 0; i < 6; i++) { canvas.save(); canvas.translate(mpoints[i].x, mpoints[i].y); canvas.rotate(-i * 60); canvas.drawbitmap(mblade, 0, 0, mpaint); canvas.restore(); } canvas.restore(); } @override public boolean ontouchevent(motionevent event) { if (event.getpointercount() > 1) { return false; } switch (event.getaction()) { case motionevent.action_down: mprevx = event.getx(); mprevy = event.gety(); break; case motionevent.action_move: float diffx = math.abs((event.getx() - mprevx)); float diffy = math.abs((event.gety() - mprevy)); if (diffy > diffx) { // 竖直方向的滑动 float diff = (float) math.sqrt(diffx * diffx + diffy * diffy) / mcircleradius * mmaxapert; if (event.gety() > mprevy) { //判断方向 setcurrentapert(mcurrentapert - diff); } else { setcurrentapert(mcurrentapert + diff); } mprevx = event.getx(); mprevy = event.gety(); } break; default: break; } return true; } private void calculatepoints() { if (mcircleradius - mspace <= 0) { log.e(tag, "the size of view is too small and space is too large"); return; } //mcircleradius - mspace可以保证内嵌六边形在光圈内 float curradius = mcurrentapert / mmaxapert * (mcircleradius - mspace); //利用对称关系,减少计算 mpoints[0].x = curradius / 2; mpoints[0].y = -curradius * cos_30; mpoints[1].x = -mpoints[0].x; mpoints[1].y = mpoints[0].y; mpoints[2].x = -curradius; mpoints[2].y = 0; mpoints[3].x = mpoints[1].x; mpoints[3].y = -mpoints[1].y; mpoints[4].x = -mpoints[3].x; mpoints[4].y = mpoints[3].y; mpoints[5].x = curradius; mpoints[5].y = 0; } //创建光圈叶片,让美工mm提供更好 private void createblade() { mblade = bitmap.createbitmap(mcircleradius, (int) (mcircleradius * 2 * cos_30), config.argb_8888); path path = new path(); canvas canvas = new canvas(mblade); path.moveto(mspace / 2 / cos_30, mspace); path.lineto(mblade.getwidth(), mblade.getheight()); path.lineto(mblade.getwidth(), mspace); path.close(); canvas.clippath(path); canvas.drawcolor(mbladecolor); } /** * 设置光圈片的颜色 * @param bladecolor */ public void setbladecolor(int bladecolor) { mbladecolor = bladecolor; } /** * 设置光圈背景色 */ public void setbackgroundcolor(int backgroundcolor) { mbackgroundcolor = backgroundcolor; } /** * 设置光圈片之间的间隔 * @param space */ public void setspace(int space) { mspace = space; } /** * 设置光圈最大值 * @param maxapert */ public void setmaxapert(float maxapert) { mmaxapert = maxapert; } /** * 设置光圈最小值 * @param mminapert */ public void setminapert(float mminapert) { this.mminapert = mminapert; } public float getcurrentapert() { return mcurrentapert; } public void setcurrentapert(float currentapert) { if (currentapert > mmaxapert) { currentapert = mmaxapert; } if (currentapert < mminapert) { currentapert = mminapert; } if (mcurrentapert == currentapert) { return; } mcurrentapert = currentapert; invalidate(); if (maperturechanged != null) { maperturechanged.onaperturechanged(currentapert); } } /** * 设置光圈值变动的监听 * @param listener */ public void setaperturechangedlistener(aperturechanged listener) { maperturechanged = listener; } }
自定义属性的xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="apertureview"> <attr name="blade_color" format="color" /> <attr name="background_color" format="color" /> <attr name="blade_space" format="dimension" /> </declare-styleable> </resources>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。