Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to customize View in Android to realize Digital Rain effect

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, I would like to share with you how to customize Android View to achieve digital rain effect related knowledge points, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you can learn something after reading this article, let's take a look at it.

Effect picture

There are many types of animation in Android, including frame animation, tweened animation and attribute animation. In addition, using custom View combined with mathematical formulas, you can draw complex interfaces or animations. This article records the digital rain modeled on the Matrix. Let's take a look at the effect.

Implementation steps

Preparatory work, constant configuration information

/ / the color value of the text final int DEFAULT_TEXT_COLOR = Color.argb (255,0,255,70); / / text size final int TEXT_SIZE = 24; / / ordinary brush Paint mPaint; / / highlight brush Paint mPaintLight; / / the interval between changing the text int switchInternal = interval.nextInt (interval.length)]; / / the speed at which numbers fall.

Build the content that displays the text, because the computer is made up of 0 and 1, so use 0 and 1 to represent its content data.

/ / build the strings if (contentArray = = null) {contentArray = new String [2]; contentArray [0] = "0"; contentArray [1] = "1";}

Because it is randomly displayed, use random numbers to get the content

Private String getChar () {return contentArray [random.nextInt (2)];}

Because it is a custom View, the entry is in the constructor of the custom View.

Public NumberView (Context context, AttributeSet attrs, int defStyleAttr) {super (context, attrs, defStyleAttr); init ();}

Initialize the above configuration information in the init method, such as creating a specific brush color, text size, etc.

Private void init () {mPaint = new Paint (Paint.ANTI_ALIAS_FLAG); mPaint.setARGB (255,0,255,70); mPaint.setTextSize (TEXT_SIZE); a = textColor > > 24 & 0xff; r = textColor > > 16 & 0xff; g = textColor > > 8 & 0xff; b = textColor & 0xff; mPaint.setARGB (a, r, g, b) MPaintLight = new Paint (Paint.ANTI_ALIAS_FLAG); mPaintLight.setARGB (255,140,255,170); mPaintLight.setTextSize (TEXT_SIZE);}

The size of the View and the size of the View itself are measured in the onMeasure method, which is determined by onMeasure ().

Override protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure (widthMeasureSpec, heightMeasureSpec); int widthSize = MeasureSpec.getSize (widthMeasureSpec); int widthMode = MeasureSpec.getMode (widthMeasureSpec); int heightSize = MeasureSpec.getSize (heightMeasureSpec); int heightMode = MeasureSpec.getMode (heightMeasureSpec); if (widthMode = MeasureSpec.EXACTLY) {mWidth = widthSize } if (heightMode = = MeasureSpec.EXACTLY) {mHeight = heightSize;} setMeasuredDimension ((int) mWidth, (int) mHeight);}

OnDraw () defines how to draw the View, so in the onDraw method, you must do a drawing operation in order to display the animation.

@ Override protected void onDraw (Canvas canvas) {super.onDraw (canvas); render (canvas); rain ();} private void rain () {for (int j = 0; j)

< streams.length; j++) { Symbol[] symbols = (Symbol[]) streams[j]; for (int i = 0; i < symbols.length; i++) { Symbol symbol = symbols[i]; symbol.y = symbol.y >

= mHeight? 0: symbol.y + symbol.speed;}

No matter what kind of operation, custom View is always inseparable from the core method of onMeasure onLayout onDraw. For example, if we want to draw a painting, we also need to design it this way, its size, location, and what it looks like. Just like the method of this code. OnMeasure: determines the size of the view onLayout: determines the location of the view

These are all the contents of the article "how to customize View to achieve Digital Rain effect in Android". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report