Android Aquery - 我想在本地使用drawable

时间:2014-06-02 10:00:25

标签: android aquery

在项目过程中,我遇到了内存泄漏问题 因此解决方案是选择AQuery。

aq.id.image不能使用R.drawable

如何为Aquery使用可绘制图像?

AQuery aq = new Aquery(getActivity());

....

aq.id(R.id.imageview).image(R.drawable.image1);    // It can't

2 个答案:

答案 0 :(得分:0)

我按照以下方式做到了:

ImageView image=(ImageView)homeFragmentView.findViewById(R.id.helpImage);
        image.setImageBitmap(ImageDecoder.decodeSampledBitmapFromResource(getSherlockActivity().getResources(), R.drawable.swipr_logo, 200, 200));

ImageDecoder类是

public class ImageDecoder {
public static int calculateInSampleSize(BitmapFactory.Options options,
        int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and
        // keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

public static Bitmap decodeSampledBitmapFromResource(String path,
        int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth,
            reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    options.inPreferredConfig = Config.RGB_565;
    options.inDither = true;
    return BitmapFactory.decodeFile(path, options);
}

public static Bitmap decodeSampledBitmapFromResource(Resources res,
        int resId, int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth,
            reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    options.inPreferredConfig = Config.RGB_565;
    options.inDither = true;
    return BitmapFactory.decodeResource(res, resId, options);
}

}

这里ImageDecoder类根据移动屏幕对图像进行解码。

希望它可以帮到你!

答案 1 :(得分:0)

试试这个

Bitmap placeholder = aq.getCachedImage(R.drawable.image1);

aq.id(R.id.imageview).image(placeholder);