在项目中,我自定义了一个RelativeLayout控件,写的也很简单。使用的过程竟然遇到很多问题,让我很惊讶,一下觉得自己对android认识太浅。
遇到了三个问题:
1.重载的onDraw()函数不会被执行;
2.Imageview同时使用了setImageResource和setBackgroundDrawable,图片竟然出现前后图片重叠在一起;
3.setVisibility(View.INVISIBLE)竟然无效;
由于项目比较紧张,感紧百度一下:
问题1:加上这句就可以了setWillNotDraw(false);
具体原因可以看这个http://blog.csdn.net/look85/article/details/8442675
问题2:这个问题我也不知到原因,后来我只使用setBackgroundDrawable,问题就解决了。真是奇怪,懂的求教!
问题3:这个就纠结了,网上几乎找不到这方面的问题,后来看到一篇文章就说加一句clearAnimation();靠,还真解决了。可是我都把animation有关的代码注释掉了。不懂不懂。
附上我定义的MyCollectButton代码:
public class MyCollectButton extends RelativeLayout {
private static final String TAG = "MyCollectButton";
private Paint mPaint = new Paint();
private boolean mDrawOutline = false;
public ImageView imageView;
private TextView textView;
private RelativeLayout layout = null;
private int nBgColor = Color.rgb(73, 85, 104);
public MyCollectButton(Context context) {
super(context);
initView(context);
// TODO Auto-generated constructor stub
}
public MyCollectButton(Context context, AttributeSet attrs) {
super(context, attrs);
setWillNotDraw(false);
initView(context);
}
public MyCollectButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView(context);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
}
private void initView(Context context){
if(layout==null){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = (RelativeLayout) inflater.inflate(R.layout.collect_item_layout,this);
}
this.setBackgroundColor(nBgColor);
imageView = (ImageView)layout.findViewById(R.id.collect_app_imageView);
textView = (TextView)layout.findViewById(R.id.collect_app_title);
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,25*Launcher.g_fscale);
this.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if (hasFocus) {
mDrawOutline = true;
v.bringToFront();
curImageView(v);
// onCursorFocusOn();
} else {
lastImageView(v);
mDrawOutline = false;
}
}
});
mPaint.setColor(Color.WHITE);
mPaint.setStyle(Paint.Style.FILL);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mDrawOutline) {
//this.setBackgroundColor(Color.rgb(236, 107, 13));
final float left = getPaddingLeft();
final float right = getWidth() - getPaddingRight();
final float top = getPaddingTop();
final float bottom = getHeight() - getPaddingBottom();
canvas.drawRect(left, top, right, top + 1.5f, mPaint);
canvas.drawRect(left, top, left + 1.5f, bottom, mPaint);
canvas.drawRect(left, bottom - 1.5f, right, bottom, mPaint);
canvas.drawRect(right - 1.5f, top, right, bottom, mPaint);
} else {
//this.setBackgroundColor(nBgColor);
}
super.onDraw(canvas);
}
private void curImageView(View paramView) {
final ScaleAnimation localScaleAnimation = new ScaleAnimation(1.0F, 1.1F,
1.0F, 1.1F, 1, 0.5F, 1, 0.5F);
localScaleAnimation.setInterpolator(new AccelerateInterpolator(1.0F));
localScaleAnimation.setDuration(200L);
localScaleAnimation.setFillAfter(true);
localScaleAnimation
.setAnimationListener(new Animation.AnimationListener() {
public void onAnimationEnd(Animation paramAnimation) {
}
public void onAnimationRepeat(Animation paramAnimation) {
}
public void onAnimationStart(Animation paramAnimation) {
}
});
paramView.startAnimation(localScaleAnimation);
}
private void lastImageView(View paramView) {
final ScaleAnimation localScaleAnimation = new ScaleAnimation(1.1F, 1.0F,
1.1F, 1.0F, 1, 0.5F, 1, 0.5F);
localScaleAnimation.setInterpolator(new AccelerateInterpolator(1.0F));
localScaleAnimation.setDuration(200L);
localScaleAnimation.setFillAfter(true);
paramView.startAnimation(localScaleAnimation);
}
/**
* 设置图片资源
*/
public void setIconResource(int resId) {
imageView.setImageResource(resId);
}
public void setIconBackgroundDrawable(Drawable drawable) {
imageView.setBackgroundDrawable(drawable);
}
/**
* 设置显示的文字
*/
public void setTextViewText(String text) {
textView.setText(text);
if(text.equals("添加应用")){
//nBgColor = Color.rgb(138, 138, 138);
textView.setTextColor(Color.rgb(145, 145, 145));
}else{
//nBgColor = Color.rgb(73, 85, 104);
textView.setTextColor(Color.rgb(255, 255, 255));
}
//this.setBackgroundColor(nBgColor);
}
}