简体   繁体   中英

Put View On Another View In Android

In my android app, I wanna put a border on a ImageView . I don't know how can put view on another view. Actually border is a custom view.

Custom View Class:

public class CropBorder extends View {

    private Paint paint = new Paint();


    public CropBorder(Context context) {
        super(context);
    }

    public CropBorder(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }


    @Override
    public void onDraw(Canvas canvas) {
        float[][] circleXY = {{5, 5}, {155, 5}, {305, 5}, {5, 105}, {155, 105}, {305, 105}, {5, 55}, {305, 55}};
        float[][] lineXY = {{5, 5, 305, 5}, {5, 105, 305, 105}, {5, 5, 5, 105}, {305, 5, 305, 105}};

        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(1);

        for(int i = 0 ; i < circleXY.length ; i++)
            canvas.drawCircle(circleXY[i][0], circleXY[i][1], 5, paint);

        paint.setStrokeWidth(2);

        for(int i = 0 ; i < lineXY.length ; i++)
            canvas.drawLine(lineXY[i][0], lineXY[i][1], lineXY[i][2], lineXY[i][3], paint);
    }

}

Activity Class:

public class CropTestActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.crop_test_layout);
    }

}

Layout XML:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/crop_test_layout"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <com.nvtech.backend.utility.imageEditor.CropBorder
        android:id="@+id/crop_border"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp" >
    </com.nvtech.backend.utility.imageEditor.CropBorder>

    <ImageView
        android:id="@+id/android_image"
        android:src="@drawable/android"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:layout_gravity="center"
        android:scaleType="fitXY"
        android:contentDescription="@string/android_image_description" >
    </ImageView>

</LinearLayout>

You can just wrap your ImageView in a LinearLayout, and use the padding to set the border:

<LinearLayout // this is your root container ... >
    <LinearLayout
        android:id="@+id/content_item_image_wrapper"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#00FFFFFF"
        android:padding="3dip" >

        <ImageView
            android:id="@+id/user_profile_thumbnail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:maxHeight="80dp"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>
</LinearLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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