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 use Thread Bezier Curve in Android Development

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Android development how to use thread Bezier curve, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Thread Thread is used in Android in the same way as Java SE. Like most OS systems, there is a main thread called UI Thread in Android. UI Thread is mainly used to distribute messages to the corresponding Widget, including Drawing events. UI Thread is also a thread used to handle user interaction events. For example, if you press a button on the screen, the UI thread notifies the corresponding control (Widgets) of the Touch event, Widget sets its state to "press" and sends the "Invalidate" event to Event Queue. The UI thread reads the event from the Event Queue and tells the Widgets to redraw itself.

If your application is not well designed, this single-threaded mode of UI threads can lead to very poor user response performance. In particular, if you put some time-consuming operations such as network access or database access in the UI thread, these operations will cause the user interface to become unresponsive, and worst of all, if the UI thread blocks for more than a few seconds (5 seconds), the famous ANR dialog box will appear:

So when designing an application, you need to use a separate worker thread to run some time-consuming tasks to avoid blocking the UI thread, but if you want to update the UI thread in the worker thread, you can't update the UI directly in the worker thread, because the UI thread is not "Thread Safe". Therefore, all UI-related operations must generally be done in UI Thread.

Android OS provides several ways to access UI threads on non-UI threads.

Activity.runOnUiThread (Runnable)

View.post (Runnable)

View.postDelayed (Runnable, long)

Handler

The Bezier example dynamically displays the Bezier curve and uses Activity.runOnUiThread to update the screen. The complete code is as follows:

1 public class Bezier extends Graphics2DActivity 2 implements OnClickListener,Runnable {3 4 / * * 5 * The animation thread. 6 * / 7 private Thread thread; 8 private volatile boolean stopThread=false; 9 private boolean stopOrNot=false; 10 boolean drawn; 11 / * 12 * The random number generator. 13 * / 14 static java.util.Random random = new java.util.Random (); 15 / * 16 * The animated path 17 * / 18 Path path = new Path (); 19 / * 20 * Red brush used to fill the path. 21 * / 22 SolidBrush brush = new SolidBrush (Color.RED); 23 private static final int NUMPTS = 6; 24 private int animpts [] = new int [NUMPTS * 2]; 25 private int deltas [] = new int [NUMPTS * 2]; 26 long startt, endt; 27 28 private Button btnOptions; 29 @ Override 30 protected void drawImage () {31 drawDemo 32 33} 34 35 public void onCreate (Bundle savedInstanceState) {36 super.onCreate (savedInstanceState); 37 setContentView (R.layout.beziers); 38 graphic2dView 39 = (GuidebeeGraphics2DView) findViewById (R.id.graphics2dview); 40 btnOptions = (Button) findViewById (R.id.btnStopStart); 41 btnOptions.setOnClickListener (this); 42 reset (100100); 43 if (thread = = null) {44 thread = new Thread (this); 45 thread.start () 46} 47 48} 49 50 @ Override 51 public void onClick (View view) {52 53 if (! stopOrNot) {54 btnOptions.setText ("Start"); 55 stopThread=true; 56} 57 else {58 stopThread=false; 59 btnOptions.setText ("Stop"); 60 if (thread = = null) {61 thread = new Thread (this); 62 thread.start () 63} 64} 65 stopOrNotNot; 66 67} 68 / * 69 * Generates new points for the path. 70 * / 71 private void animate (int [] pts, int [] deltas, 72 int I, int limit) {73 int newpt = pts [I] + deltas [I]; 74 if (newpt = limit) {79 newpt = 2 * limit-newpt; 80 deltas [I] =-(random.nextInt () & 0x00000003) 81 + 2) 82} 83 pts [I] = newpt; 84} 85 86 / * 87 * Resets the animation data. 88 * / 89 private void reset (int w, int h) {90 for (int I = 0; I)

< animpts.length; i += 2) { 91 animpts[i + 0] 92 = (random.nextInt() & 0x00000003) 93 * w / 2; 94 animpts[i + 1] 95 = (random.nextInt() & 0x00000003) 96 * h / 2; 97 deltas[i + 0] 98 = (random.nextInt() & 0x00000003) 99 * 6 + 4; 100 deltas[i + 1] 101 = (random.nextInt() & 0x00000003) 102 * 6 + 4; 103 if (animpts[i + 0] >

W / 2) {104 deltas [I + 0] =-deltas [I + 0]; 105} 106 if (animpts [I + 1] > h / 2) {107 deltas [I + 1] =-deltas [I + 1] Final Runnable updateCanvas = new Runnable () {113 public void run () {114 int offsetX = (graphic2dView.getWidth ()-115 SharedGraphics2DInstance.CANVAS_WIDTH) / 2; 116 int offsetY = (graphic2dView.getHeight () 117-SharedGraphics2DInstance.CANVAS_HEIGHT) / 2; 118 graphic2dView.invalidate (offsetX,offsetY, 119 offsetX+100,offsetY+100); 120} 121} 122 / * 123 * Sets the points of the path and draws and fills the path. 124 * / 125 private void drawDemo (int w, int h) {126 for (int I = 0; I < animpts.length; I + = 2) {127 animate (animpts, deltas, I + 0, w); 128 animate (animpts, deltas, I + 1, h); 131int [] ctrlpts = animpts; 133int len = ctrlpts.length; 134int prevx = ctrlpts [len-2]; 135int prevy = ctrlpts [len-1]; 136int curx = ctrlpts [0]; 137int cury = ctrlpts [1]; 138int midx = (curx + prevx) / 2; 139int midy = (cury + prevy) / 2 140 path.moveTo (midx, midy); 141 for (int I = 2; I)

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