Android Project Scratch Award detailed extension--Open Source Scratch award View Production

Source: Internet
Author: User

Android Project Scratch award detailed (iv)

Objective

We have successfully achieved the function of scraping award, this is an extension, we have this view directly defined as open source control, published to Jitpack, after the need or can be used directly, about the custom control of the knowledge, do not understand the students can see this below I wrote these two articles

Android Custom Controls

Android Development--release third-party libraries to Jitpack

Realize
  1. Defining Properties
      • Text text content
      • TextColor text color
      • TextSize Text Size
      • Paintsize the width of the erase effect
      • Messagebackground Winning pictures
      • Cover cover layer picture or cover layer color
      • Isdrawtext display text or a picture, display text by default
      • Clearflag How many thresholds are reached to clear the masking layer, default 60

    In accordance with the previous custom control, we set up a atts.xml, where we define attributes, where we can combine the winning picture and the information layer color into one item, and the overlay image and the overlay color together as one

      <?xml version= "1.0" encoding= "Utf-8"?> <resources> <declare-styleable name= "Guajiangview" > <attr name= "Text" format= "string"/> <attr name= "textcolor" format= "Color"/> <a         TTR name= "textSize" format= "integer"/> <attr name= "Messagebackground" format= "Color|reference"/> <attr name= "Cover" format= "Color|reference"/> <attr name= "paintsize" format= "integer"/> <at TR name= "Isdrawtext" format= "boolean"/> <attr name= "Clearflag" format= "integer"/> </declare-stylea Ble> </resources>  
  2. Get the properties we define

     TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.GuaJiangView); text = ta.getString(R.styleable.GuaJiangView_text); textColor = ta.getColor(R.styleable.GuaJiangView_textColor,0); textSize = ta.getInteger(R.styleable.GuaJiangView_textSize,16); coverDrawable = ta.getDrawable(R.styleable.GuaJiangView_cover); isDrawText = ta.getBoolean(R.styleable.GuaJiangView_isDrawText,true); clearFlag = ta.getInteger(R.styleable.GuaJiangView_clearFlag,60); if (!isDrawText){     backgroundDrawable = ta.getDrawable(R.styleable.GuaJiangView_messageBackground); } paintSize = ta.getInteger(R.styleable.GuaJiangView_PaintSize,10); ta.recycle();
    This adds a judgment that when the user chooses to write only the winning information, we can block out the image setting to get the information layer and prevent the error.
  3. Modify the previous project code

    Open Source Library, nature is not as much as the previous project so messy, it is natural to write a thick note, the Code refactoring optimization, but also to consider the relevant logic

    In this case, attrs can be used \| to make the property receive two properties, most commonly the background color and background image composition, such as the background in the attr defined above

     <attr name="messageBackground" format="color|reference"/>

    We can use this property in the following way to get the conversion to bitmap

     /**  * drawable转换为bitmap  * @param drawable 需要转换的drawble  * @param width 宽  * @param height 高  * @return 返回bitmap */ public Bitmap drawableToBitmap(Drawable drawable, int width, int height) {     if (drawable instanceof BitmapDrawable) {         return ((BitmapDrawable) drawable).getBitmap();     }     Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);     Canvas canvas = new Canvas(bitmap);     drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());     drawable.draw(canvas);     return bitmap; }

    In fact, drawable it has a draw method, as long as we call the setBounds set range, in the call draw method can be directly drawn, the above drawable actually contains a color , so we directly call draw method to draw a bitmap of pure color

    Simply observe that the Mcanvas before the meeting is the same.

    With the new bitmap as the artboard, and then drawable painting on the canvas (actually painted in bitmap), we return to this bitmap to use

    Other than before, we think about it, and finally I will send out the complete code

  4. Upload GitHub and Jitpack

    There will not be much to say, I wrote these two articles very clearly
    • Simple use of Git
    • Android Development--release third-party libraries to Jitpack
  5. Simple test use

Guajiangviewdemo
We follow the methods on the document, import dependencies, use can

--To be a beauty stripper

Originally wanted to separate out to write an article, however, found that there is no new code to write, we directly take the open source Library to use (need beautiful dress and stripping clothes oh ~)

I prepared two times the girl's clothes, put two pictures in the Drawable folder, we can start to work

When it's done, my Android studio is dead?!

What the heck!! It's good to restart the software.

Exciting Moments ————

Haha, success, scraping the card project to the end of the explanation!

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.

Tags Index: