Created
September 10, 2014 03:00
-
-
Save moltak/26f37cf6a3c762a3e757 to your computer and use it in GitHub Desktop.
android custom view FourSquareImageLayout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.day1song.app.View; | |
import android.content.Context; | |
import android.graphics.Bitmap; | |
import android.os.Handler; | |
import android.os.Message; | |
import android.util.AttributeSet; | |
import android.view.View; | |
import android.widget.ImageView; | |
import android.widget.RelativeLayout; | |
import com.day1song.app.DSPlayerTracks; | |
import com.day1song.app.R; | |
import com.day1song.app.View.Listeners.SimpleImageLoadingListener; | |
import com.day1song.app.tools.DSUtilities; | |
import com.nostra13.universalimageloader.core.DisplayImageOptions; | |
import com.nostra13.universalimageloader.core.ImageLoader; | |
import java.util.ArrayList; | |
/** | |
* Created by moltak on 2014. 8. 13.. | |
* four square image layout. | |
* 좋아요, 즐겨찾기에서 화면에서 사용됨. | |
*/ | |
public class FourSqureImageLayout extends RelativeLayout { | |
private ImageView fourSquareImageView[]; | |
private ImageView bigSuqareImageView; | |
private ArrayList<String> randomImage = new ArrayList<>(); | |
public FourSqureImageLayout(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public FourSqureImageLayout(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
} | |
@Override | |
protected void onFinishInflate() { | |
super.onFinishInflate(); | |
fourSquareImageView = new ImageView[4]; | |
fourSquareImageView[0] = (ImageView)findViewById(R.id.imageview_squre_small1); | |
fourSquareImageView[1] = (ImageView)findViewById(R.id.imageview_squre_small2); | |
fourSquareImageView[2] = (ImageView)findViewById(R.id.imageview_squre_small3); | |
fourSquareImageView[3] = (ImageView)findViewById(R.id.imageview_squre_small4); | |
bigSuqareImageView = (ImageView)findViewById(R.id.imageview_squre_big); | |
} | |
/** | |
* 이미지가 1개일땐 big image view를 사용해서 이미지를 크게 띄움 | |
* 이미지가 2개 이상일땐 4등분 된 small image view에 이미지를 띄움. | |
* | |
* @param dsPlayerTracks | |
* getTracks().get(0).getImage_url()를 이용해서 이미지 url을 가져오기 위함 | |
*/ | |
public void setSquareImage(DSPlayerTracks tracks, final OnRandomImageListener onRandomImageListener) { | |
if (tracks == null | |
|| tracks.getTracks() == null | |
|| tracks.getTracks().size() == 0) return; | |
final String []array = DSUtilities.removeDuplication(tracks.getTracksImgUrlToArray()); | |
if (array.length == 1) { | |
randomImage.add(array[0]); | |
setBigSuqareImageView(array[0]); | |
} else { | |
new Thread(new Runnable() { | |
@Override | |
public void run() { | |
randomImage = DSUtilities.getFourRandomImagesFromTrackList(array); | |
onRandomImageListener.complete(randomImage); | |
ImageSetHandler.sendEmptyMessage(0); | |
} | |
}).start(); | |
} | |
} | |
private Handler ImageSetHandler = new Handler() { | |
@Override | |
public void handleMessage(Message msg) { | |
super.handleMessage(msg); | |
if (randomImage != null) { | |
setFourSquareImageView(randomImage); | |
} | |
} | |
}; | |
/** | |
* 이미지 리스트를 받아서 square이미지를 구성함 | |
* @param randomImage 랜덤 이미지 리스트 | |
*/ | |
public void setRandomSquareImage(ArrayList<String> randomImage) { | |
if(randomImage.size() == 1) { | |
setBigSuqareImageView(randomImage.get(0)); | |
} | |
else { | |
setFourSquareImageView(randomImage); | |
} | |
} | |
/** | |
* 이미지가 1개일때 | |
* @param img_url | |
*/ | |
private void setBigSuqareImageView(String img_url) { | |
DisplayImageOptions imageOption = DSUtilities.getManyUsingImageOptions(R.drawable.bg_cover_loading); | |
ImageLoader.getInstance().displayImage( | |
img_url, | |
bigSuqareImageView, | |
imageOption | |
); | |
} | |
/** | |
* 이미지가 1개 이상일때 | |
* | |
* @param randomImage | |
* DSUtilities.getFourRandomImagesFromTrackList 에서 나온 random 이미지 리스트 | |
*/ | |
private void setFourSquareImageView(ArrayList<String> randomImage) { | |
DisplayImageOptions imageOption = DSUtilities.getManyUsingImageOptions(R.drawable.bg_cover_loading_small); | |
for(int i = 0; i < 4 && i < randomImage.size(); i ++) { | |
String image_url = randomImage.get(i); | |
if(image_url == null) break; | |
ImageLoader.getInstance().displayImage( | |
image_url, | |
fourSquareImageView[i], | |
imageOption, | |
new FourSqureImageLoadingListener(i) | |
); | |
} | |
} | |
/** | |
* 만약 이미지가 1개보다 많을 때, small image view중 1번 로딩이 끝나면 앞에서 가리고 있던 big image view를 사라지게한다. | |
*/ | |
private class FourSqureImageLoadingListener extends SimpleImageLoadingListener { | |
private final int index; | |
private FourSqureImageLoadingListener(int index) { | |
this.index = index; | |
} | |
@Override | |
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { | |
if(bigSuqareImageView.getVisibility() == View.VISIBLE) { | |
bigSuqareImageView.setVisibility(View.GONE); | |
} | |
} | |
} | |
/** | |
* 랜덤 이미지 리스트 생성이 끝났을 때, 콜백함수. | |
*/ | |
public static interface OnRandomImageListener { | |
public void complete(ArrayList<String> arrayList); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment