从本地存储中加载图像时生涩滚动

时间:2017-03-02 07:38:46

标签: android

从协调器布局和应用栏布局中的本地存储路径加载图像时,滚动时会出现抖动

我曾使用myImageview.setImageURI(MYURI);但工作不正常

1 个答案:

答案 0 :(得分:0)

我的问题已经解决了,因为我正在使用Picasso,我尝试过使用下面的代码

 Picasso.with(context).load(YOUR_IMAGE_URI).placeholder(R.drawable.profile_img).error(R.drawable.profile_imgd).resize(250,250).centerCrop().into(myImageview);

然而我尝试了以下代码,但我认为这可能会有所帮助

public static Bitmap decodeFile(File f, int reqWidth, int reqHeight) {
    Bitmap b = null;

    //Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;

    FileInputStream fis = null;
    try {
        fis = new FileInputStream(f);
        BitmapFactory.decodeStream(fis, null, o);
        fis.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    //Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = calculateInSampleSize(o2,reqWidth, reqHeight);

    try {
        fis = new FileInputStream(f);
        b = BitmapFactory.decodeStream(fis, null, o2);
        fis.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return b;

}

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;
}
相关问题