在 android 中的 ImageView 上方画一条线

Drawing a line above the ImageView in android

我有一个自定义的 imageView,比如说 MyImageView class,我正试图在它上面画一些线。

public class MyImageView extends ImageView
{

public MyImageView(Context context) {
    super(context);

    invalidate();
    // TODO Auto-generated constructor stub
}

public MyImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
    invalidate();
}

public MyImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    invalidate();
}

@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
    p.setColor(Color.BLACK);
    canvas.drawLine(0, 0, 100, 100, p);

    canvas.drawLine(0, 0, 20, 20, p);
    canvas.drawLine(20, 0, 0, 20, p);
    super.onDraw(canvas);


}
}

在我的 activity class 中,我已借助位图将图像设置为该自定义 imageView。

MyImageView imageView;
 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imageView=(MyImageView)findViewById(R.id.image);
    Bitmap dbitmap = BitmapFactory.decodeResource(getResources(), R.drawable.dinkan);
    Bitmap bitmap = dbitmap.copy(Bitmap.Config.ARGB_8888, true);
    imageView.setImageBitmap(bitmap);
    imageView.invalidate();
}

所以现在的问题是,线条画在图像下面,所以我看不到它。我想要在图像上绘制线条。我该如何实施?

尝试在 super.onDraw(canvas) 下方编写 canvas.drawLine() 代码。这意味着首先会发生 imageview 的默认实现,然后是自定义实现,即绘制线。

您可以通过像这样创建视图来添加水平线

<View
    android:id="@+id/line_77"
    android:layout_width="match_parent"
    android:layout_height="3dp"
    android:layout_below="@+id/relativeLayout8"
    android:layout_marginTop="5dp" />