How to make canvas transparent in Android?

前端 未结 1 1507
温柔的废话
温柔的废话 2021-02-11 07:32

I want to make the drawing surface of my app transparent so that the my app can look like the user can draw on top of my background image, I have the following XML:



        
相关标签:
1条回答
  • 2021-02-11 08:11
    package com.logistics.kiddiekuts.Trans;
    
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Paint;
    import android.graphics.RectF;
    import android.graphics.Paint.Style;
    import android.util.AttributeSet;
    import android.widget.RelativeLayout;
    
    public class TransparentPanel extends RelativeLayout {
        private Paint innerPaint, borderPaint;
    
        public TransparentPanel(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        public TransparentPanel(Context context) {
            super(context);
            init();
        }
    
        private void init() {
            innerPaint = new Paint();
            innerPaint.setARGB(225, 225, 225, 225); // gray
            innerPaint.setAntiAlias(true);
    
            borderPaint = new Paint();
            borderPaint.setARGB(255, 255, 255, 255);
            borderPaint.setAntiAlias(true);
            borderPaint.setStyle(Style.STROKE);
            borderPaint.setStrokeWidth(2);
        }
    
        public void setInnerPaint(Paint innerPaint) {
            this.innerPaint = innerPaint;
        }
    
        public void setBorderPaint(Paint borderPaint) {
            this.borderPaint = borderPaint;
        }
    
        protected void dispatchDraw(Canvas canvas) {
    
            RectF drawRect = new RectF();
            drawRect.set(0, 0, getMeasuredWidth(), getMeasuredHeight());
    
            canvas.drawRoundRect(drawRect, 8, 8, innerPaint);
            // canvas.drawRoundRect(drawRect, 5, 5, borderPaint);
    
            super.dispatchDraw(canvas);
        }
    }
    

    XML::

                    <TextView android:layout_width="wrap_content"
                        android:text="Name :" android:textColor="#000000"
                        android:layout_marginLeft="15dip" android:layout_marginTop="27dip"
                        android:id="@+id/tvname" android:layout_height="wrap_content"></TextView>
                    <EditText android:layout_width="197dip" android:id="@+id/name"
                        android:layout_marginLeft="7dip" android:textSize="12dip"
                        android:layout_marginTop="23dip" android:layout_toRightOf="@+id/tvname"
                        android:layout_height="35dip" />
    
                    <TextView android:layout_width="wrap_content"
                        android:text="Phone :" android:textColor="#000000"
                        android:layout_marginLeft="15dip" android:layout_marginTop="18dip"
                        android:layout_below="@+id/tvname" android:id="@+id/tvphone"
                        android:layout_height="wrap_content"></TextView>
                    <EditText android:layout_width="197dip" android:id="@+id/phone"
                        android:textSize="12dip" android:layout_marginLeft="5dip"
                        android:layout_marginTop="3dip" android:layout_toRightOf="@+id/tvphone"
                        android:layout_below="@+id/name" android:layout_height="35dip"></EditText>
    
                    <TextView android:layout_width="wrap_content"
                        android:text="Email :" android:textColor="#000000"
                        android:layout_marginLeft="15dip" android:layout_marginTop="20dip"
                        android:layout_below="@+id/tvphone" android:id="@+id/tvemail"
                        android:layout_height="wrap_content"></TextView>
                    <EditText android:layout_width="197dip" android:id="@+id/email"
                        android:textSize="12dip" android:layout_marginLeft="9dip"
                        android:layout_marginTop="3dip" android:layout_toRightOf="@+id/tvemail"
                        android:layout_below="@+id/phone" android:layout_height="35dip"></EditText>
    
    
                    <TextView android:layout_width="wrap_content"
                        android:text="Category :" android:textColor="#000000"
                        android:layout_marginLeft="15dip" android:layout_marginTop="20dip"
                        android:layout_below="@+id/tvemail" android:id="@+id/tvcategory"
                        android:layout_height="wrap_content"></TextView>
    
                    <TextView android:layout_width="wrap_content" android:id="@+id/category"
                        android:textColor="#000000" android:layout_marginLeft="10dip"
                        android:text="For Appointment" android:layout_marginTop="8dip"
                        android:layout_toRightOf="@+id/tvcategory" android:layout_below="@+id/email"
                        android:layout_height="wrap_content"></TextView>
    
    
                    <TextView android:layout_width="wrap_content"
                        android:text="Location :" android:textColor="#000000"
                        android:layout_marginLeft="15dip" android:layout_marginTop="10dip"
                        android:layout_below="@+id/tvcategory" android:id="@+id/tvlocation"
                        android:layout_height="wrap_content"></TextView>
    
                    <TextView android:layout_width="wrap_content" android:id="@+id/txtlocation"
                        android:textColor="#000000" android:layout_marginLeft="10dip"
                        android:text="Atlanta,Ga"  android:layout_marginTop="10dip"
                        android:layout_toRightOf="@+id/tvlocation" android:layout_below="@+id/category"
                        android:layout_height="wrap_content"></TextView>
    
    
                    <TextView android:layout_width="wrap_content"
                        android:textColor="#000000" android:text="Message:"
                        android:layout_marginLeft="15dip" android:layout_marginTop="10dip"
                        android:layout_below="@+id/tvlocation" android:id="@+id/tvatn"
                        android:layout_height="wrap_content"></TextView>
    
                    <EditText android:layout_width="250dip" android:id="@+id/atn"
                        android:textSize="12dip" android:lines="7" android:layout_below="@+id/tvatn"
                        android:layout_marginLeft="15dip" android:layout_marginTop="3dip"
                        android:gravity="top|left" android:layout_height="wrap_content"></EditText>
                    <Button android:layout_height="25dip" android:layout_below="@+id/atn"
                        android:layout_marginLeft="15dip" android:layout_marginTop="3dip"
                        android:id="@+id/btnsubmit" android:background="@drawable/submit_btn"
    
    
                        android:layout_width="250dip"></Button>
                    <TextView android:id="@+id/text" android:layout_width="fill_parent"
                        android:layout_below="@+id/btnsubmit" android:layout_height="wrap_content"></TextView>
                </com.logistics.kiddiekuts.Trans.TransparentPanel>
    
    0 讨论(0)
提交回复
热议问题