我正在尝试向对象添加一个简单的选择器 - imageView - 这是扩展imageview的类的实例。
final PackageTypeView ptv = packageTypeViewsList.get(index);
index++;
Drawable drawable = getResources().getDrawable(
R.drawable.selector);
ImageTitleView imageView = new ImageTitleView(
getActivity());
imageView.setImageBitmap(BeanUtils.decodeSampledBitmapFromResource(new File(ptv.getTileRenderPath()), screenWidth, screenWidth));
if (ptv.getTitleBgArgb() != null) {
imageView.setColorCode(ptv.getTitleBgArgb());
}
imageView.setImageDrawable(drawable);
//TODO check this entry in the database
if (ptv.isShowingTitle()) {
imageView.setDescription(ptv.getName());
}
imageView.setScaleType(ScaleType.CENTER_CROP);
imageView.setClickable(true);
// imageView.setImageResource(R.drawable.selector);
imageView.setOnClickListener(this);
imageView.setTag(ptv.getId());
这是ImageTitleView类。
public class ImageTitleView extends ImageView {
private String description;
private String colorCode = "aaaaaaBB";
private static float fontSize = -1;
private Paint paint = new Paint();
private Rect rect = new Rect();
private Bitmap bitmap = null;
private static Typeface tf = Typeface.create("Helvetica", Typeface.BOLD);
public ImageTitleView(Context context, Bitmap bitmap) {
super(context);
initPaint(context);
this.bitmap = bitmap;
}
public ImageTitleView(Context context) {
super(context);
initPaint(context);
}
public ImageTitleView(Context context, String description) {
super(context);
this.description = description;
initPaint(context);
}
public ImageTitleView(Context context, AttributeSet attrs) {
super(context, attrs);
initPaint(context);
}
public ImageTitleView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initPaint(context);
}
private void initPaint(Context context) {
fontSize = context.getResources().getDimension(R.dimen.text_size_grid_info);
paint.setTextSize(fontSize);
paint.setTypeface(tf);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
if (description != null) {
rect.set(new Rect(0, canvas.getHeight() - (canvas.getHeight() / 4), canvas.getWidth(), canvas.getHeight()));
paint.setColor(Color.parseColor("#" + colorCode));
canvas.drawRect(rect, paint);
float y = rect.top + ( (rect.bottom - rect.top) / 2 + fontSize / 2);
paint.setColor(Color.WHITE);
canvas.drawText(description, 15, y, paint);
}
}
The problem is the the following when I add the selector the imageView's bitmap is replaced with
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getColorCode() {
return colorCode;
}
public void setColorCode(String colorCode) {
if (colorCode != null && !colorCode.isEmpty()) {
this.colorCode = colorCode;
}
}
@Override
public void setClickable(boolean clickable) {
super.setClickable(clickable);
}
@Override
public void setImageBitmap(Bitmap bm) {
super.setImageBitmap(bm);
}
}
这是选择器:
<item android:state_pressed="true" android:drawable="@color/button_pressed_color"/>
<item android:state_focused="true" android:drawable="@color/button_pressed_color"/>
<item android:drawable="@android:color/transparent"/>
问题是选择器会更改imageView对象的背景,并用透明背景替换此对象的位图。