Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It appears that for every value change in your slider, you are creating your <code>MyTask</code> and running it. Instead, one thing that might perform better is only updating your listener when the slider has been released (stopped moving). It looks like <code>HorizontalSlider</code> is a custom class, so I can't speak for what is happening in there.</p> <p>If a slider is being moved from left to right, it may fire off update events 100 times during that move. That would be 100 background tasks!</p> <p><strong>UPDATE</strong></p> <p>One possibility would be to <code>cancel()</code> your active <code>MyTask</code> before you produce another one. If you take that route you need to call <code>isCancelled()</code> in <code>onPostExecute</code> and only invalidate your view if the task is <em>not</em> cancelled. However, if the code in <code>findCirclePixels()</code> is the bottleneck, this will not work. My guess is that cancelling the active task before creating a new one will <strong>NOT</strong> solve your problem.</p> <p>Coding this up properly (only creating your <code>MyTask</code> when necessary) is still going to be the best route. Consider that for every <code>AsyncTask</code> the OS has to spawn a new background thread, which comes with its own overhead. Don't be fooled by the small amount of code you have put in <code>doInBackground()</code>.</p> <p><strong>UPDATE AGAIN</strong></p> <p>I have cleaned up your code and added comments for suggestion:</p> <pre><code>public class TouchView extends View{ private File tempFile; private byte[] imageArray; private Bitmap bgr; private Bitmap bm; private Bitmap bgr2 = null;; private Paint pTouch; private int centreX = 1; private int centreY = 1; private int radius = 50; private int Progress = 1; private static final String TAG = "*********TouchView"; private Filters f = null; private MyTask mt = null; public TouchView(Context context) { super(context); } public TouchView(Context context, AttributeSet attr) { super(context,attr); tempFile = new File(Environment.getExternalStorageDirectory(). getAbsolutePath() + "/"+"image.jpg"); imageArray = new byte[(int) tempFile.length()]; try{ InputStream is = new FileInputStream(tempFile); BufferedInputStream bis = new BufferedInputStream(is); DataInputStream dis = new DataInputStream(bis); int i = 0; while (dis.available() &gt; 0) { imageArray[i] = dis.readByte(); i++; } dis.close(); } catch (Exception e) { e.printStackTrace(); } BitmapFactory.Options bfo = new BitmapFactory.Options(); bfo.inSampleSize = 1; bm = BitmapFactory.decodeByteArray(imageArray, 0, imageArray.length, bfo); bgr = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), bm.getConfig()); bgr = bm.copy(bm.getConfig(), true); bgr2 = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), bm.getConfig()); f = new Filters(); } public void findCirclePixels(){ float prog = (float) Progress / 150000; bgr2 = f.barrel(bgr, prog); } public void initSlider(final HorizontalSlider slider) { slider.setOnProgressChangeListener(changeListener); } private OnProgressChangeListener changeListener = new OnProgressChangeListener() { @Override public void onProgressChanged(View v, int progress) { /* TODO: If there is a way to see if the slider is still being changed (control has not been released), then you should return and not continue with the creation of another task */ this.Progress = progress2; if (mt != null) { mt.cancel(); } mt = new MyTask(); mt.execute(); } }; private class MyTask extends AsyncTask&lt;Void, Void, Void&gt; { protected void onPreExecute() { } @Override protected Void doInBackground(Void... params) { // This check is not necessary if coded properly if (!isCancelled()) { TouchView.this.findCirclePixels(); } return null; } protected void onPostExecute(Void result) { // This check is not necessary if coded properly if (!isCancelled()) { TouchView.this.invalidate(); } } } @Override public void onDraw(Canvas canvas){ super.onDraw(canvas); canvas.drawBitmap(bgr2, 0, 0, null); canvas.drawCircle(centreX, centreY, radius, pTouch); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload