繁体   English   中英

Android:如何在ImageView中绘制矩形

[英]Android: How to draw a rect in a ImageView

我需要在ImageView的中间绘制一个rect来创建CostumeCheckBox。 我是Android的新手,我不知道要这样做。 ImageView / CostumeCheckbox具有边框:

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    Rect rect = new Rect();
    Paint paint = new Paint();
    paint.setStyle(Paint.Style.STROKE);        
    paint.setColor(CheckboxBorderColor);        
    paint.setStrokeWidth(borderWidth);
    getLocalVisibleRect(rect);
    canvas.drawRect(rect, paint); }

尝试这个 :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/ic_launcher"
    android:gravity="center" >

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

作为参考,请检查我对更苗条的问题的回答: 如何将复选框绑定到图像

如果在imageview顶部有一个常规复选框对您来说是一个可行的解决方案,则可以将它们都放在FrameLayout中。 无需自己绘制复选框,您可以将其添加为XML:

<FrameLayout>
    <ImageView .../>
    <CheckBox .../>
</FrameLayout>

FrameLayout中元素的顺序定义了Z顺序,最后一个元素在顶部。 在此处查看FrameLayout:

http://developer.android.com/reference/android/widget/FrameLayout.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM