«

6.9 android相机预览最佳分辨率计算 SwipeRefreshLayout下拉刷新的使用 仿开源中国api包内容的网络访问结构

时间:2024-3-2 18:50     作者:韩俊     分类: Android


这几天做一个自定义相机界面,发现相机老是被拉伸,于是自己想了一个自动选择最佳的尺寸,计算要求如下:

1.尺寸越大越好,2.宽高比越接近越好

标准化尺寸和比例至0-1

最佳的总分=尺寸标准化的值*0.1+比例标准化的值*1

为什么*0.1呢?因为比例是最影响变形的因素,而尺寸对于变形来说基本没有影响,如果比0.1大,会比较明显的印象最终结果,所以选择一个0.1对变形影响比较少:

最终计算方法:

 /**
     * size 是横向计算的,height比width大
     * @param supportedSizes
     * @param w
     * @param h
     * @return
     */
    private static Camera.Size getMaxSize (List<Camera.Size> supportedSizes,int w,int h) {
        LogUtil.showlog("getsize:" + w + "*" + h);//480*800

        //获取预览的各种分辨率
        Camera.Size max = null;

        double maxSize=0,minSize=Integer.MAX_VALUE;
        double maxFit=0,minFix=Double.MAX_VALUE;

        for (Camera.Size size :supportedSizes) {
            double tagSize = size.height*1d*size.width;//越大越好
            if (tagSize>maxSize) {
                maxSize = tagSize;
            }
            if (tagSize<minSize) {
                minSize = tagSize;
            }
            double tagFit = Math.abs(size.height*1d/size.width-h*1d/w);//越小越好
            LogUtil.showlog("tagFix:" + tagFit);
            if (tagFit>maxFit) {
                maxFit=tagFit;
            }
            if (tagFit<minFix) {
                minFix = tagFit;
            }
        }

//        LogUtil.showlog("最大尺寸:" + max.width + "*" + max.height);

        for (Camera.Size size :supportedSizes) {
            double tagSize = size.height*1d*size.width;
            double sizeScore = (tagSize-minSize)*1d/(maxSize-minSize);

//            double tagFix = size.height*1d/size.width;
            double tagFix = Math.abs(size.height*1d/size.width-h*1d/w);
            double fixScore = (tagFix-minFix)*1d/(maxFit-minFix);

            if (max==null) {
                max = size;
            } else {
                double bSize = max.height*1d*max.width;
                double bestSize = (bSize-minSize)*1d/(maxSize-minSize);
//                double bFix = max.height/max.width;
                double bFix = Math.abs(max.height*1d/max.width-h*1d/w);
                double bestFix = (bFix-minFix)*1d/(maxFit-minFix);

                LogUtil.showlog("=========");
                LogUtil.showlog("匹配w*h:" + size.width  + "*" + size.height);

                LogUtil.showlog("目标尺寸分值:" + sizeScore);
                LogUtil.showlog("目标吻合分值:" + fixScore);

                LogUtil.showlog("最优尺寸分值:" + bestSize);
                LogUtil.showlog("最优吻合分值:" + bestFix);

                if (sizeScore*0.1+(1-fixScore) > bestSize*0.1+(1-bestFix)) {
                    if (max.height<h && max.width<w) {
                        max = size;
                    }
                }
            }
        }

        LogUtil.showlog("最合适的尺寸:"  + max.width + "*" + max.height);

        return max;
    }


后来也在网上看见的一个计算相机预览时的最佳分辨率:

 /**
     * 获取最适合屏幕的照片 尺寸
     *
     * @param sizes
     * @param w
     * @param h
     * @return
     */
    private static Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {
        final double ASPECT_TOLERANCE = 0.1;
        double targetRatio = (double) w / h;
        if (sizes == null)
            return null;

        Camera.Size optimalSize = null;
        double minDiff = Double.MAX_VALUE;

        int targetHeight = h;

        // Try to find an size match aspect ratio and size
        for ( Camera.Size size : sizes) {
            double ratio = (double) size.width / size.height;
            if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
                continue;
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }

        // Cannot find the one match the aspect ratio, ignore the requirement
        if (optimalSize == null) {
            minDiff = Double.MAX_VALUE;
            for (Camera.Size size : sizes) {
                if (Math.abs(size.height - targetHeight) < minDiff) {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }
        }
        return optimalSize;
    }

经过两个算法计算,将最好的尺寸设置进相机

parameters.setPictureSize(size.w,size.h);
发现还是变形,最终选择不设置参数,用系统默认的。





SwipeRefreshLayout使用方法:
http://www.apkbus.com/android-166418-1-1.html

SwipeRefreshLayout是V4包得其中一个类

swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
    swipeLayout.setOnRefreshListener(this);
    swipeLayout.setColorScheme(android.R.color.holo_blue_bright, 
            android.R.color.holo_green_light, 
            android.R.color.holo_orange_light, 
            android.R.color.holo_red_light);
}

public void onRefresh() {
    new Handler().postDelayed(new Runnable() {
        [url=home.php?mod=space&uid=389554]@Override[/url] public void run() {
            swipeLayout.setRefreshing(false);
        }
    }, 5000);
}



仿开源中国api包内容的网络访问结构

最近项目功能渐渐的多了起来,接口的访问重复次数越来越多,为了防止以后接口改动app要修改多出,想起了开源中国的api包,于是,自己也写了一个api包,如下:

public class DynamicApi extends BaseApi {
    public DynamicApi(Context context, String falg, NetAccess.NetAccessListener listener) {
        super(context, falg, listener);
    }

    public static DynamicApi init (Context context,String flag,NetAccess.NetAccessListener listener) {
        return new DynamicApi(context,flag,listener);
    }
<span style="white-space:pre">   </span>/**
<span style="white-space:pre">   </span>删除动态
**/
    public void delDynam (boolean isClass,int dyid) {
        // 删除动态
        HashMap<String, String> params2 = new HashMap<String, String>();
        params2.put("dyid", "" + dyid);
        params2.put("isclass",isClass+"");

        params2 = RequestUtil.parse(mContext, params2);
        NetAccess.request(mContext)
                .setFlag(mFlag)
                .setParams(params2)
                .byPost(Global.Urls.delDym,
                        mListener);
    }
}

使用:

DynamicApi.init(context,"del",Mainactivity.this).delDynam(true,1);






标签: android

热门推荐