在scrollView中包含控件imageView,而Imageview需要画一个边框 而重写onDraw()。结果如图中多了一条竖线。
而把ScrollView换成是LinearLayout等就是正常的。
请教大家这个问题如何解决阿?
代码如下:
xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<abc.app.MyImageView
android:id="@+id/image_11"
android:background="#CFF"
android:focusable="true"
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
/>
</ScrollView>
MainActivity.java:
public class MainActivity extends Activity {
private MyImageView image;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/
setContentView(R.layout.main);
image = (MyImageView)findViewById(R.drawable.ic_launcher);
image.setImageResource(R.drawable.photo);
image.setScaleType(ScaleType.FIT_XY);
image.setBackgroundColor(Color.BLUE);
}
}
MyImageView.java:
class MyImageView extends ImageView {
public MyImageView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MyImageView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public MyImageView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
// add draw troke
Rect rec = canvas.getClipBounds();
rec.bottom--;
rec.right--;
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(4);
paint.setAntiAlias(true);
canvas.drawRect(rec, paint);
}
}
结果图:
------解决方案--------------------
canvas.getClipBounds()表示的是获取控件需要重新绘制的区域,所以当绘制SCROLLVIEW时也会被拿到来重绘,所以画边框最好直接用IMAGEVIEW控件的宽高来绘制就可以了。