2017-02-26 1 views
0

내 장치에서 이미지를로드하고 격자보기로 채우는 중입니다. 이 목적으로 비동기 작업을 사용하고 있는데도 내 눈금보기 스크롤이 지연됩니다. 어떤 도움이미지를 표시 할 때 지연 격자보기

이 내 어댑터

public class ImageAdapter extends BaseAdapter { 
Context c; 
LayoutInflater inflater; 
GridView gv; 
ArrayList<File> list; 

ImageAdapter(Context c, GridView gv, ArrayList<File> list) { 
    this.gv = gv; 
    this.list = list; 
    this.c = c; 
} 

@Override 
public int getCount() { 
    return list.size(); 
} 

@Override 
public Object getItem(int i) { 
    return list.get(i); 
} 

@Override 
public long getItemId(int i) { 
    return list.size(); 
} 


@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View v; 
    if (convertView == null) { 
     inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = inflater.inflate(R.layout.single_grid, parent, false); 
    } else { 
     v = convertView; 
    } 

    String path = list.get(position).getPath(); 
    Bitmap bitmap = BitmapFactory.decodeFile(path); 
    Bitmap bitmap2 = scaleBitmap(bitmap, 100, 100); 
    ImageView iv = (ImageView) v.findViewById(R.id.imageView); 
    iv.setImageBitmap(bitmap2); 
    return v; 
} 

public static Bitmap scaleBitmap(Bitmap bitmapToScale, float newWidth, float newHeight) { 
    if (bitmapToScale == null) 
     return null; 
    //get the original width and height 
    int width = bitmapToScale.getWidth(); 
    int height = bitmapToScale.getHeight(); 
    // create a matrix for the manipulation 
    Matrix matrix = new Matrix(); 

    // resize the bit map 
    matrix.postScale(newWidth/width, newHeight/height); 

    // recreate the new Bitmap and set it back 
    return Bitmap.createBitmap(bitmapToScale, 0, 0, bitmapToScale.getWidth(), bitmapToScale.getHeight(), matrix, true); 
} 

} 당신은 기본 응용 프로그램 스레드에서 이미지를로드

답변

0

이다}

public class Images extends Fragment { 
int imageCount = 0; 
GridView gv; 
ArrayList<File> list; 
static ImageAdapter imageAdapter; 
static File root; 
static ArrayList<File> a = new ArrayList<File>(); 

public Images() { 
    // Required empty public constructor 
} 

ArrayList<File> imageReader(File root) { 
    ArrayList<File> a = new ArrayList<>(); 
    File[] files = root.listFiles(); 
    for (int i = 0; i < files.length; i++) { 
     if (files[i].isDirectory()) { 
      a.addAll(imageReader(files[i])); 
     } else { 
      if (files[i].getName().endsWith(".jpg") || files[i].getName().endsWith(".png")) { 
       a.add(files[i]); 
       imageCount = imageCount + 1; 
      } 
     } 
    } 
    return a; 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_images, null); 
    gv = (GridView) view.findViewById(R.id.gridView); 

    new MyImagesAsync().execute(); 
    return view; 
} 

public class MyImagesAsync extends AsyncTask { 

    @Override 
    protected void onPostExecute(Object o) { 
     super.onPostExecute(o); 
     imageAdapter = new ImageAdapter(getContext(), gv, Images.a); 
     gv.setAdapter(imageAdapter); 
    } 

    @Override 
    protected Object doInBackground(Object[] params) { 
     root = new File("/sdcard"); 
     File[] files = new File("/sdcard").listFiles(); 
     for (int i = 0; i < files.length; i++) { 
      if (files[i].isDirectory()) { 
       Images.a.addAll(imageReader(files[i])); 
      } else { 
       if (files[i].getName().endsWith(".jpg") || files[i].getName().endsWith(".png")) { 
        Images.a.add(files[i]); 
       } 
      } 
     } 
     return null; 
    } 
} 

을 감사합니다. 이것은 제대로 수행되지 않습니다. 배경 스레드에서 이미지를로드하도록 설계된 많은 image-loading libraries available for Android 중 하나를 사용하고 이미지가 준비되면 ImageView을 채 웁니다. 또한

는 :

  • 자신이

  • 당신에게 LayoutInflater를 제공 할 수 있습니다 c.getSystemService(Context.LAYOUT_INFLATER_SERVICE)을 사용하여 몇 가지 가능성이-존재하지 않는 /sdcard 디렉토리의 모든 이미지에 대한 MediaStore 조회보다는 스캔 훨씬 더 효율적입니다 귀하의 활동 테마에 대해 아는 것이 없습니다 — ActivitygetLayoutInflater()으로 전화 하시려면

0

이미지 로딩에 AsyncTask 사용

관련 문제