환경 설정
참고로 실재 적용한 안드로이드 API 버전이 5.1 인 이유에서 4.x 버전을 사용하지 않고 3.x 버전을 사용하였다.Gradle 빌드환경에서는 아래와 같이 build.gradle 파일에 의존성을 추가하면 된다.
1 2 3 4 5 6 7 8 | repositories { mavenCentral() } dependencies { compile 'com.github.bumptech.glide:glide:3.8.0' } |
다운로드한 jar 파일을 사용하여 의존성을 추가하는 경우는 아래와 같이 설정한다. (/lib 경로에 라이브러리가 있는 경우 )
1 2 3 4 5 6 7 8 9 10 11 | repositories { mavenCentral() } dependencies { compile files( 'libs/volley.jar' ) compile files( 'libs/okhttp-2.3.0.jar' ) compile files( 'libs/okhttp-urlconnection-2.0.0.jar' ) compile files( 'libs/glide-3.8.0.jar' ) } |
volley, okhttp-xx 라이브러리는 glide 가 의존성을 갖는 라이브러리이기 때문에 추가하였다.
사용하기
참고로 여기에서는 URL 를 통하여 이미지를 불러오는 대신에 앱에 포함된 이미지를 불러오는 것으로 사용하였다. GIF 이미지를 보여주기 위해서는 아래와 같은 방법으로 원하는 위치의 ImageView에 이미지를 로드하면 된다.1 2 3 4 5 6 7 8 9 10 | @Bind (R.id.ivSnowBall) ImageView mIvSnowBall; protected void onCreate(Bundle savedInstanceState) { animateSnowball(); } private void animateSnowball(){ Glide.with( this ).load(R.drawable.snowball_animate).asGif().into(mIvSnowBall); } |
이경우 최초 이미지 로드시에 상당한 시간의 딜레이가 발생하는 문제에 직면하게 된다. 해당 이슈는 캐쉬 설정을 추가하는 방법으로 해결이 가능하다. (4.x 버전에서는 해당 이슈가 해결되었다고 들었다. )
1 2 3 4 5 6 7 8 9 10 | @Bind (R.id.ivSnowBall) ImageView mIvSnowBall; protected void onCreate(Bundle savedInstanceState) { animateSnowball(); } private void animateSnowball(){ Glide.with( this ).load(R.drawable.snowball_animate).asGif().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(mIvSnowBall); } |
마지막으로 이미지를 로드하는 동안 진행상태를 보여주고자 하는 경우는 아래와 같은 방법으로 구현이 가능하다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | @Bind (R.id.ivSnowBall) ImageView mIvSnowBall; protected void onCreate(Bundle savedInstanceState) { animateSnowball(); } private void animateSnowball(){ final ProgressBar progressBar = (ProgressBar) findViewById(R.id.snowball_loading_progress); progressBar.setVisibility(View.VISIBLE); Glide.with( this ).load(R.drawable.snowball_animate).asGif().diskCacheStrategy(DiskCacheStrategy.SOURCE).listener( new RequestListener(){ @Override public boolean onResourceReady(Object o, Object o2, Target target, boolean b, boolean b1) { progressBar.setVisibility(View.GONE); return false ; } @Override public boolean onException(Exception e, Object o, Target target, boolean b) { progressBar.setVisibility(View.GONE); return false ; } }).into(mIvSnowBall); } |
참고 layout.xml 파일
1 2 3 4 5 6 | <linearlayout android:background= "#ffffff" android:layout_height= "match_parent" android:layout_width= "match_parent" android:orientation= "vertical" xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:sl= "http://schemas.android.com/apk/res-auto" > <imageview android:duration= "50" android:id= "@+id/ivSnowBall" android:layout_height= "wrap_content" android:layout_width= "wrap_content" android:scaletype= "fitStart" android:src= "@drawable/snowball_01" android:visibility= "visible" > </imageview> <progressbar android:id= "@+id/snowball_loading_progress" android:layout_centerinparent= "true" android:layout_height= "wrap_content" android:layout_marginleft= "100dp" android:layout_margintop= "100dp" android:layout_width= "wrap_content" android:visibility= "invisible" > </progressbar> </linearlayout> |
댓글 없음:
댓글 쓰기