Create a layer with black transparent over the activity.

I want to create a layer with a transparent layer above the activity, and I need to make visible only a specific view, for example, for example, "Login Button". This is something to make a hole in the layer exactly the size of a button. Please help me with this.

Thanks in advance.

+4
source share
4 answers

Programmatically, you can use Canvas to create a layer. Fill it all with your color and cut a hole.

public class DrawView extends View { Paint paint = new Paint(); Paint transparentPaint = new Paint; public DrawView(Context context) { super(context); } @Override public void onDraw(Canvas canvas) { //first fill everything with your covering color paint.setColor(yourTransparentColor); canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint); //now clear out the area you want to see through transparentPaint.setAlpha(0xFF); transparentPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); Rect rect=new Rect(left, top, right, bottom);//make this your rect! canvas.drawRect(rect,transparentPaint); } } 
+1
source

If I understand your question correctly

Your parent Activity layout will be RelativeLayout . In your parent's layout at the bottom, add another child, RelativeLayout, which inserts match_parent parameters for width and height. Then you can set a color similar to this #99000000 background. This will give you a black transparent layer above your parent.

Something like that

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/parentLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFF"> <!-- here you add your activities views ect... --> <RelativeLayout android:id="@+id/childLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#99000000"> <!-- This will be your overlay so here you can add your login button ect --> </RelativeLayout> </RelativeLayout> 

I have not tested this, but it should solve your problem.

+2
source

Following the steps of "HalR", I was able to achieve a transparent background. Thanks to him, I discovered that some of you get blackness in the cut over the transparent layer due to graphic rendering. which can be enabled using below code inside the onDraw method

 ` @Override public void onDraw(Canvas canvas) { if (android.os.Build.VERSION.SDK_INT >= 11) { setLayerType(View.LAYER_TYPE_SOFTWARE, null); } ..... ..... // your draw rect creation and other stuff ` 

which will allow you to get a transparent area by eliminating the black color of the pitch

+1
source

If I understood your question, then this is what you wanted: You tried to impose a transparent view on your activity and cut holes to see a few things.

here I found a useful example that shows how you can cut bitmap images and combine them. I would suggest that for your problem, just take part in cutting the bitmap and don't forget to set the backview to the image with opacity, as shown here.

android: background = "# 00ffffff"

here is the layout i created

  <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/transparent" android:src="@drawable/ic_launcher" android:layout_alignParentRight="true" android:layout_alignParentBottom="true"/>" <ImageView android:id="@+id/img" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00ffffff" android:scaleType="centerCrop" > </ImageView> </RelativeLayout> 

and this is my maneuverability

public class MainActivity extends the action {

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageView iv = (ImageView) findViewById(R.id.img); Bitmap foreground = BitmapFactory.decodeResource(getResources(), R.drawable.android_cat_wanted); foreground = punchAHoleInABitmap(foreground); iv.setImageBitmap(foreground); } private Bitmap punchAHoleInABitmap(Bitmap foreground) { Bitmap bitmap = Bitmap.createBitmap(foreground.getWidth(), foreground.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); canvas.drawBitmap(foreground, 0, 0, paint); paint.setAntiAlias(true); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); float radius = (float)(getScreenSize().x *.35); float x = (float) ((getScreenSize().x*.5)); float y = (float) ((getScreenSize().y*.5)); canvas.drawCircle(x, y, radius, paint); return bitmap; } @SuppressLint("NewApi") private Point getScreenSize() { WindowManager window = (WindowManager) getSystemService(Context.WINDOW_SERVICE); Display display = window.getDefaultDisplay(); Point size = new Point(); display.getSize(size); return size; } 

}

hope you find it useful

0
source

Source: https://habr.com/ru/post/1485916/


All Articles