package
com.test.horizontalgalleryview;
import
java.util.ArrayList;
import
android.content.Context;
import
android.graphics.Bitmap;
import
android.graphics.BitmapFactory;
import
android.graphics.Color;
import
android.util.AttributeSet;
import
android.widget.ImageView;
import
android.widget.LinearLayout;
public
class
HorizontalGalleryView
extends
HorizontalPagerView {
public
static
interface
OnItemSelectedListener {
public
void
onItemSelected(
int
idx, Bitmap bitmap);
}
private
Context context;
private
OnItemSelectedListener listener =
null
;
ArrayList<Bitmap> imgs =
new
ArrayList<Bitmap>();
ArrayList<LinearLayout> layouts =
new
ArrayList<LinearLayout>();
private
int
max =
0
;
public
HorizontalGalleryView(Context context, AttributeSet attrs) {
super
(context, attrs);
this
.context = context;
max = attrs.getAttributeIntValue(
null
,
"max"
,
0
);
addImage(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
addImage(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
addImage(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
addImage(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
addImage(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
addImage(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
}
public
void
addImage(Bitmap bitmap) {
imgs.add(bitmap);
int
size = imgs.size()-
1
;
int
layout_idx = size/max;
int
img_idx = size%max;
if
(layout_idx > layouts.size()-
1
) {
LinearLayout layout =
new
LinearLayout(context);
layout.setBackgroundColor(Color.GRAY);
layout.setOrientation(LinearLayout.HORIZONTAL);
for
(
int
i =
0
; i < max; i++){
ImageView img =
new
ImageView(context);
img.setId(i);
LinearLayout.LayoutParams param =
new
LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,
1
);
img.setLayoutParams(param);
img.setAdjustViewBounds(
true
);
layout.addView(img);
}
addView(layout);
layouts.add(layout);
}
ImageView view = (ImageView)layouts.get(layout_idx).findViewById(img_idx);
view.setImageBitmap(bitmap);
}
@Override
protected
void
onMeasure(
int
widthMeasureSpec,
int
heightMeasureSpec) {
final
int
widthSize = MeasureSpec.getSize(widthMeasureSpec);
int
size = widthSize/max;
super
.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY));
}
public
void
setOnItemSelectedListener(OnItemSelectedListener listener){
this
.listener = listener;
}
}