Solution to the Android Surfaceview drawing touch path flicker problem _android

Source: Internet
Author: User
Tags gety

This article has shared the solution to the Surfaceview touch path flicker problem The method, for everybody reference, the concrete content is as follows

The first way to solve the surfaceview touch path flicker problem:

Because Surfaceview uses a dual caching mechanism, the two canvases appear on the screen in turn. Then, to store the touch trajectory and avoid the flicker problem caused by the inconsistency of the two canvas contents, you can completely solve the flicker with the method of saving the drawing process and constantly redrawing, And that's also the kind of thing that happens occasionally in a trial. Because the MoveTo () function cannot read to the parameter execution default setting (the parameter set to the last touch point), the disconnection connection flicker problem, the detailed code is as follows:

Package Com.tobacco.touchdraw;
Import java.util.ArrayList;
Import Android.content.Context;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Paint;
Import Android.graphics.Path;
Import android.view.MotionEvent;
Import Android.view.SurfaceHolder;
Import Android.view.SurfaceView;
Import Android.view.View;
Import Android.view.SurfaceHolder.Callback;
Import Android.view.View.OnTouchListener;
 public class Lsurfaceview extends Surfaceview implements callback,ontouchlistener,runnable{private Surfaceholder sfh;
 Private Canvas Canvas;
 Private Paint Paint;
 private path Path;
 private arraylist<path> paths;
 Private Boolean flag;
 Public Lsurfaceview {Super (context);
 Sfh=this.getholder ();
 Sfh.addcallback (this);
 Paint=new paint ();
 Paint.setcolor (color.red);
 Paint.setantialias (TRUE);
 Paint.setstrokewidth (4);
 Paint.setstyle (Paint.Style.STROKE);
 Paint.setstrokecap (Paint.Cap.ROUND);
 Paths=new arraylist<path> (); Path=neW Path ();
 public void Mydraw (Motionevent e) {int action=e.getaction ();
  Switch (action) {Case MotionEvent.ACTION_DOWN:path.moveTo (E.getx (), e.gety ());
 Break
  Case MotionEvent.ACTION_MOVE:path.lineTo (E.getx (), e.gety ());
 Break
  Case MOTIONEVENT.ACTION_UP://path.close ();
  Path path1=new path (path);
  Paths.add (path1);
  Path.reset ();
 Break @Override public void surfacechanged (Surfaceholder arg0, int arg1, int arg2, int arg3) {} @Override Pub
 LIC void surfacecreated (Surfaceholder holder) {flag=true;
 Setontouchlistener (this);
 
 New Thread (This). Start ();
 @Override public void surfacedestroyed (Surfaceholder holder) {flag=false;
 @Override public boolean Ontouch (View V, motionevent event) {Mydraw (event);
 return true;
  @Override public void Run () {a while (flag) {long start=system.currenttimemillis ();
  Canvas=sfh.lockcanvas ();
  if (canvas!=null) {canvas.drawcolor (color.black); for (int i=0;i<paths.size (); i++) Canvas.drawPath (Paths.get (i), paint);
  Canvas.drawpath (Path,paint);
  Sfh.unlockcanvasandpost (canvas);
  Long End=system.currenttimemillis ();
  try{if (end-start<30) {Thread.Sleep (30-(End-start)); The catch (Exception e) {}}}}

Also note here that ArrayList is saving a reference to an object, so create a new object entity each time you add it.

The second way to solve the surfaceview touch path flicker problem:

When handling the drawing of the touch path, use the Surfaceview, set up the Path object, set the Path object when clicked, log the touch point during the slide, and reset the path object after leaving, because the main thread cannot be blocked, so a new thread is created to refresh the screen continuously. Which is to draw the path continuously. But then there is the problem: there is a small segment of the line at the end of each track line that flashes continuously. The guess is that Lockcanvas () gets a different area of the object, and tries to use Lockcanvas (Rect re), but it does not solve the problem after the run, and then the idea is that it might be because each lockcanvas () Gets the same object, In the main thread added a canvas object, each time in the canvas object to modify the screen, and then submit the display, but after the program has not changed the effect of running! The program code is as follows:

Package Com.tobacco.touchdraw;
Import Android.content.Context;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Paint;
Import Android.graphics.Path;
Import android.view.MotionEvent;
Import Android.view.SurfaceHolder;
Import Android.view.SurfaceView;
Import Android.view.View;
Import Android.view.SurfaceHolder.Callback;
Import Android.view.View.OnTouchListener;
 public class Mysurfaceview extends Surfaceview implements callback,ontouchlistener,runnable{private Surfaceholder sfh;
 Private Canvas Canvas;
 Private Paint Paint;
 private float lastx,lasty;
 private path Path;
 Private Boolean flag;
 Public Mysurfaceview {Super (context);
 Sfh=this.getholder ();
 Sfh.addcallback (this);
 Paint=new paint ();
 Paint.setcolor (color.red);
 Paint.setantialias (TRUE);
 Paint.setstrokewidth (5);
 Paint.setstyle (Paint.Style.STROKE);
 Paint.setstrokecap (Paint.Cap.ROUND);
 
 Path=new Path (); public void Mydraw (Motionevent e) {int action=e.getactiOn ();
  Switch (action) {Case MotionEvent.ACTION_DOWN:path.moveTo (E.getx (), e.gety ());
  Lastx=e.getx ();
  Lasty=e.gety ();
 Break
  Case MotionEvent.ACTION_MOVE:path.quadTo (Lastx,lasty,e.getx (), e.gety ());
  Lastx=e.getx ();
  Lasty=e.gety ();
 Break
  Case MOTIONEVENT.ACTION_UP://path.quadto (Lastx,lasty,e.getx (), e.gety ());
  Path.reset ();
 Break @Override public void surfacechanged (Surfaceholder arg0, int arg1, int arg2, int arg3) {} @Override Pub
 LIC void surfacecreated (Surfaceholder holder) {flag=true;
 Setontouchlistener (this);
 
 New Thread (This). Start ();
 @Override public void surfacedestroyed (Surfaceholder holder) {flag=false;
 @Override public boolean Ontouch (View V, motionevent event) {Mydraw (event);
 return true;
  @Override public void Run () {a while (flag) {long start=system.currenttimemillis ();
  Canvas=sfh.lockcanvas ();
  if (canvas!=null) {Canvas.drawpath (path,paint);
  Sfh.unlockcanvasandpost (canvas); Long END=SYSTEM.CUrrenttimemillis ();
  try{if (end-start<100) {Thread.Sleep (100-(End-start)); The catch (Exception e) {}}}}

Above is the entire content of this article, hope can help you to solve Surfaceview touch track flicker problem easily.

Related Article

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.