package com.example.widget;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
public class BottomIndicator extends LinearLayout {
public BottomIndicator(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public BottomIndicator(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public BottomIndicator(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
setOrientation(LinearLayout.HORIZONTAL);
}
public int[] selectItem;
public int[] noSelectItem;
public interface OnSelectItem{
public void setect(int pos);
}
public OnSelectItem onSelectItem;
public void setdrawId( int draw[],int noSelect[]){
selectItem=draw;
noSelectItem=noSelect;
setWeightSum(draw.length);
LayoutParams layoutParams=new LayoutParams(0,LayoutParams.WRAP_CONTENT);
layoutParams.gravity=Gravity.CENTER;
layoutParams.weight=1;
for (int i = 0; i < draw.length; i++) {
final int pos=i;
ImageView imageView=new ImageView(getContext());
Bitmap bitmap = BitmapFactory.decodeResource(getContext().getResources(),draw[i]);
int height =80;
Bitmap mBitmap = scalyByHigh(bitmap, height);
imageView.setScaleType(ScaleType.CENTER_INSIDE);
imageView.setImageBitmap(mBitmap);
imageView.setLayoutParams(layoutParams);
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
selectBottom(pos);
}
});
addView(imageView);
}
}
public Bitmap scalyByHigh(Bitmap bitmap,int high){//根据高来缩放
int mHeight = bitmap.getHeight();
int mWith=bitmap.getWidth();
int realWith=mWith*high/mHeight;
Bitmap realBitmap = Bitmap.createScaledBitmap(bitmap,realWith,high,true);
return realBitmap;
}
public void setImSelect(OnSelectItem onItemSelect){
this.onSelectItem=onItemSelect;
}
public void selectBottom(int pos){//选择哪个指标
for (int i = 0; i <getChildCount(); i++) {
ImageView child = (ImageView)getChildAt(i);
if(i==pos){
child.setImageResource(selectItem[i]);
child.setClickable(false);
if(onSelectItem!=null){
onSelectItem.setect(i);
}
}
else {
child.setImageResource(noSelectItem[i]);
child.setClickable(true);
}
}
}
}