扩展图像视图。覆盖 Draw 并绘制一个矩形
public class ExtendedImageView extends ImageView {
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setStrokeWidth(3);
canvas.drawRect(50, 50, 80, 80, paint);
}
}
编辑:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ExtendedImageView ev= new ExtendedImageView(this);
ev.setBackgroundResource(R.drawable.ic_launcher);// set background
setContentView(ev);
}
class ExtendedImageView extends ImageView {
public ExtendedImageView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setStrokeWidth(3);
canvas.drawRect(300, 300, 100, 100, paint);// draw rectangle on it.
}
}
}