Android dialog fillet display and resolution of black edges and corners

Source: Internet
Author: User

Recently in the development of a weather forecast app, see a relatively good friendship tips, as follows:

What, it looks better than the original dialog. Well, there may be a lot of practice, I'll introduce my approach,

First of all, my first thought was to rewrite dialog. Customize a layout layouts, referencing the shape.xml to implement rounded corners.

Before I do this, I would like to explain: in our new layout file, as long as it is not the outermost layer we reference shape will not encounter the following problem, look at the picture:

Look carefully is four corner, found that there are four black corners, this is sure to make us do not want the results, I in the configuration file how to set up is not, depressed for a long time, suddenly found that before dialog show the effect of the picture, find a sample, the original really did, it seems I really do not fit the program Ah, Try to make money doing business. It seems to be running off the point: the solution is:GetWindow (). setbackgrounddrawable (New Bitmapdrawable ()), or GetWindow (). Setbackgrounddrawableresource (Android. R.color.transparent), so there will be no four black corners.

This time we solve the angular, and there is a problem, you will find you in the configuration file, set the middle of a layout of the background color, the fillet will be swallowed, the solution we can set the middle fill color in shape, for the middle part we can set in the layout file, Because in the middle she does not affect the round angle, haha this achieves the final effect. If someone wants to say, that I realize three kinds of colors how to do, in fact, we can also separate shape, is not it, is the trouble point, but certainly can be achieved.

It looks fine, but it's still being perfected.

It's time to go to bed, that's all, maybe I'm talking nonsense, but I'm stuck on this card three times, did not know, write down to deepen the memory.

/*************************************************************************************/

The above is a custom dialog to implement. Let's look at an example below.


This is a view that I have developed in my current project, and the style has not been processed yet, so it's a bit rough. (As for the reason: a friend with a small company development experience, you know.)

Implementation method we can completely use the above custom dialog can do. But one drawback of customization is that you have to integrate into her parent view. Because the environment variable context is used. Maybe you can do a good layering. There is also a lot of interface involved. This is similar to the activity of a mini. Here we use another simple method: Change the style of the activity .

Customize a style in the style file.

  <style name="theme.halfscreen" parent="android:style/theme.dialog  "></style>

References in activity:

<activity            android:name=". Memberactivity"            android:screenorientation="portrait"             android:theme="@style/theme.halfscreen" />

Or do not define that you directly refer to an existing style line. This is not the point.

First, we'll define the view in XML: (Here I copy the style.) It looks more comfortable than a dense piece of code.)


I don't know if you noticed. She is surrounded by rounded corners. It is this fillet that adds a unique feature to this mini version of the activity. Here's a brief description of the implementation:

First round corners of the layout you can customize a background XML file

<?xml version="1.0" encoding="utf-8"? ><shape xmlns : android="http://schemas.android.com/apk/res/android" >    <corners android: radius="10dp" />    <solid android:color="@color/white  " /></shape>

Very simple here does not explain.

This is not finished, so we can only get the bottom half is a fillet, and the upper half is not rounded, because the upper part is a picture. Here through the configuration file is not done, we need to work in the Code Squadron picture. Objective: The Upper part of the picture is rounded.

Code snippet:

/** * picture cut rounded corners, direction free. */     Public StaticFinalintall =347120;  Public StaticFinalintTOP =547120;  Public StaticFinalintleft =647120;  Public StaticFinalintright =747120;  Public StaticFinalintBOTTOM =847120;  Public StaticBitmap Fillet (intType,bitmap Bitmap,introundpx) {          Try {              //The principle is to create a transparent bitmap artboard with the same size as the image.//then draw an area of the desired shape on the artboard. //Finally, the source image is posted. Finalintwidth =bitmap.getwidth (); FinalintHeight =bitmap.getheight (); Bitmap Paintingboard=Bitmap.createbitmap (Width,height, config.argb_8888); Canvas Canvas=NewCanvas (Paintingboard);              Canvas.drawargb (Color.transparent, Color.transparent, Color.transparent, color.transparent); Final Paint Paint=NewPaint (); Paint.setantialias (true);                 Paint.setcolor (Color.Black); if(TOP = =type)              {cliptop (canvas,paint,roundpx,width,height); }Else if(left = =type)              {clipleft (canvas,paint,roundpx,width,height); }Else if(right = =type)              {clipright (canvas,paint,roundpx,width,height); }Else if(BOTTOM = =type)              {clipbottom (canvas,paint,roundpx,width,height); }Else{clipall (canvas,paint,roundpx,width,height); } paint.setxfermode (NewPorterduffxfermode (mode.src_in)); //Post MapFinal Rect src =NewRect (0,0, width, height); Final Rect DST=src;                 Canvas.drawbitmap (bitmap, SRC, DST, paint); returnPaintingboard; } Catch(Exception exp) {returnbitmap; }      }           Private Static voidClipleft (final Canvas canvas,final paint paint,intOffsetintWidthintheight) {Final Rect block=NewRect (offset,0, Width,height);              Canvas.drawrect (block, paint); Final RECTF RECTF=NewRECTF (0,0, offset *2, height);          Canvas.drawroundrect (RECTF, offset, offset, paint); }                    Private Static voidClipright (final Canvas canvas,final paint paint,intOffsetintWidthintheight) {Final Rect block=NewRect (0,0, width-offset, height);              Canvas.drawrect (block, paint); Final RECTF RECTF=NewRECTF (Width-offset *2,0, width, height);          Canvas.drawroundrect (RECTF, offset, offset, paint); }                    Private Static voidCliptop (final Canvas canvas,final paint paint,intOffsetintWidthintheight) {Final Rect block=NewRect (0, offset, width, height);              Canvas.drawrect (block, paint); Final RECTF RECTF=NewRECTF (0,0, Width, offset *2);          Canvas.drawroundrect (RECTF, offset, offset, paint); }                    Private Static voidClipbottom (final Canvas canvas,final paint paint,intOffsetintWidthintheight) {Final Rect block=NewRect (0,0, width, height-offset);              Canvas.drawrect (block, paint); Final RECTF RECTF=NewRECTF (0, Height-offset *2, width, height);          Canvas.drawroundrect (RECTF, offset, offset, paint); }                    Private Static voidClipall (final Canvas canvas,final paint paint,intOffsetintWidthintheight) {Final RECTF RECTF=NewRECTF (0,0, width, height);          Canvas.drawroundrect (RECTF, offset, offset, paint); }  

First of all, this code is copy others. It's pretty clear, and I'll just use it.

Here you have to pay attention to a value roundpx. Look at this picture below (upper fillet).


Maybe some friends already understand that the white is our bottom rounded layout. Because at this point ( picture fillet radian > Bottom rounded layout. So this will happen, the solution is very simple, you can make the picture fillet arc smaller than the bottom corner of the fillet layout can be, or for more accurate, you can use PX as the unit, all refer to this size.



Android dialog fillet display and resolution of black edges and corners

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.