Code Fragment-一些工具类的特点

本文介绍了一种典型的工具类设计模式,重点强调了工具类的特点,例如使用静态方法而非实例方法,构造方法为私有等。并通过BitmapUtils类的具体实现案例,展示了如何正确地创建并使用工具类。

我们的工程中会有很多工具类,工具类通常有如下的特点。

  • 工具类的方法大多是静态方法。(使用的过程中,不需要new创建对象)
  • 构造方法应该是private的,做到一种强制性的非new创建 ,而是通过类名直接调用。如PackgeUtils.getPackageInforation();
  • 方法中需要的变量如Context,是通过参数传入,非属性(因为在静态的方法中,需要静态的属性,静态的属性会引用当前对象,阻止了垃圾回收)。
  • 类名通常是XXXUtils
一个节选例子如下:

package com.android.phone;

import android.graphics.Bitmap;
import android.os.SystemClock;
import android.os.SystemProperties;
import android.util.Log;

public class BitmapUtils {

    /** This class is never instantiated.正如这个英文的说明,私有的构造器是一种强制非new */
    private BitmapUtils() {
    }

    public static Bitmap createBlurredBitmap(Bitmap bitmap) {

        long startTime = SystemClock.uptimeMillis();
        if (bitmap == null) {
            return null;
        }

        final int scaledSize = 128;
        bitmap = Bitmap.createScaledBitmap(bitmap,
                                           scaledSize, scaledSize,
                                           true /* filter */);

        bitmap = gaussianBlur(bitmap);

        long endTime = SystemClock.uptimeMillis();
        return bitmap;
    }

    public static Bitmap gaussianBlur(Bitmap source) {//静态方法,需要的变量传入
        int width = source.getWidth();
        int height = source.getHeight();

        int numPixels = width * height;
        int[] in = new int[numPixels];
        int[] tmp = new int[numPixels];

        source.getPixels(in, 0, width, 0, 0, width, height);

        gaussianBlurFilter(in, tmp, width, height);
        gaussianBlurFilter(tmp, in, width, height);

        Bitmap filtered = Bitmap.createBitmap(in, width, height, Bitmap.Config.ARGB_8888);
        source.recycle();
        return filtered;
    }

    private static void gaussianBlurFilter(int[] in, int[] out, int width, int height) {

        final int RADIUS = 4;
        final int[] weights = { 13, 23, 32, 39, 42, 39, 32, 23, 13}; // Adds up to 256
        int inPos = 0;
        int widthMask = width - 1; // width must be a power of two.
        for (int y = 0; y < height; ++y) {

            int alpha = 0xff;

            int outPos = y;
            for (int x = 0; x < width; ++x) {
                int red = 0;
                int green = 0;
                int blue = 0;
                for (int i = -RADIUS; i <= RADIUS; ++i) {
                    int argb = in[inPos + (widthMask & (x + i))];
                    int weight = weights[i+RADIUS];
                    red += weight *((argb & RED_MASK) >> RED_MASK_SHIFT);
                    green += weight *((argb & GREEN_MASK) >> GREEN_MASK_SHIFT);
                    blue += weight *(argb & BLUE_MASK);
                }

                out[outPos] = (alpha << 24) | ((red >> 8) << RED_MASK_SHIFT)
                    | ((green >> 8) << GREEN_MASK_SHIFT)
                        | (blue >> 8);
                outPos += height;
            }
            inPos += width;
        }
    }

}



运行后ERROR::SHADER_COMPILATION_ERROR of type: FRAGMENT Fragment shader failed to compile with the following errors: ERROR: 0:35: error(#132) Syntax error: "norm" parse error ERROR: error(#273) 1 compilation errors. No code generated -- --------------------------------------------------- -- ERROR::PROGRAM_LINKING_ERROR of type: PROGRAM Fragment shader(s) were not successfully compiled before glLinkProgram() was called. Link failed. -- --------------------------------------------------- -- ERROR::SHADER_COMPILATION_ERROR of type: FRAGMENT Fragment shader failed to compile with the following errors: ERROR: 0:35: error(#132) Syntax error: "norm" parse error ERROR: error(#273) 1 compilation errors. No code generated -- --------------------------------------------------- -- ERROR::PROGRAM_LINKING_ERROR of type: PROGRAM Fragment shader(s) were not successfully compiled before glLinkProgram() was called. Link failed. -- --------------------------------------------------- -- ERROR::SHADER_COMPILATION_ERROR of type: FRAGMENT Fragment shader failed to compile with the following errors: ERROR: 0:35: error(#132) Syntax error: "norm" parse error ERROR: error(#273) 1 compilation errors. No code generated -- --------------------------------------------------- -- ERROR::PROGRAM_LINKING_ERROR of type: PROGRAM Fragment shader(s) were not successfully compiled before glLinkProgram() was called. Link failed. -- --------------------------------------------------- -- ERROR::SHADER_COMPILATION_ERROR of type: FRAGMENT Fragment shader failed to compile with the following errors: ERROR: 0:35: error(#132) Syntax error: "norm" parse error ERROR: error(#273) 1 compilation errors. No code generated -- --------------------------------------------------- -- ERROR::PROGRAM_LINKING_ERROR of type: PROGRAM Fragment shader(s) were not successfully compiled before glLinkProgram() was called. Link failed. -- --------------------------------------------------- -- ERROR::SHADER_COMPILATION_ERROR of type: FRAGMENT Fragment shader failed to compile with the following errors: ERROR: 0:35: error(#132) Syntax error: "norm" parse error ERROR: error(#273) 1 compilation errors. No code generated -- --------------------------------------------------- -- ERROR::PROGRAM_LINKING_ERROR of type: PROGRAM Fragment shader(s) were not successfully compiled before glLinkProgram() was called. Link failed. -- --------------------------------------------------- -- E:\广软大一作业\图形学\Lab4-1\x64\Debug\Demo10.exe (进程 27196)已退出,代码为 0。 要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。 按任意键关闭此窗口. . .
最新发布
11-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值