2

Hi all i've an app that places a circle on a bitmapa and with a slidebar alters the rgb values of the pixels within the circle. i'd like to use asynctask to speed up the process of altering the pixels. i'm not sure where to start. i've commented out some code at the bottom of the activity file as this is my first attept at it:). could anyone point me in the correct direction in how to implement this. The activity invokes a custom view(touchview), as i understand the asynctask must be instantiated on the UI thread. i'm thinking of initializing the slidebar ect in onPreExecute(), but not sure how to put all the worker stuff in doInBackground(). Any help will be appreciated, thanks Matt.

public class Jjilapp extends Activity {


    private Button b1;

    private static final String TAG = "*********jjil";


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e(TAG, "***********inside oncreate about to set contentview = ");
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.touchview);
        final TouchView touchView = (TouchView)findViewById(R.id.touchview); 
        final HorizontalSlider slider = (HorizontalSlider)findViewById(R.id.slider); 

        touchView.initSlider(slider);


        b1 = (Button)findViewById(R.id.button1);
        b1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.e(TAG, "onClickButton1");

            }
        });



    }//end of oncreate

   /* private class UpdateCirclePixels extends AsyncTask<Integer,Integer,Integer>{

        @Override
        protected void onPreExecute(){
            final TouchView touchView = (TouchView)findViewById(R.id.touchview); 
            final HorizontalSlider slider = (HorizontalSlider)findViewById(R.id.slider); 

            touchView.initSlider(slider);

        }



        @Override
        protected Integer doInBackground(Integer... arg0) {
            // TODO Auto-generated method stub

            publishProgress(progress);
            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... progress){


        }


    }//end of AsyncTask     */


}//end of jjilapp

.

public class TouchView extends View{


    private File tempFile;
    private byte[] imageArray;
    private Bitmap bgr;
    private Bitmap bm;
    private Paint pTouch;
    private int centreX = 1;
    private int centreY = 1;
    private int radius = 50;
    private int Progress;
    private static final String TAG = "*********TouchView";




    public TouchView(Context context) {
        super(context);
       // TouchView(context, null);
    }




    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() > 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);



        pTouch = new Paint(Paint.ANTI_ALIAS_FLAG);         
        pTouch.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT)); 
        pTouch.setColor(Color.TRANSPARENT);
        pTouch.setStyle(Paint.Style.STROKE);


        //pTouch.setMaskFilter(new BlurMaskFilter(15, Blur.NORMAL));



    }// end of touchView constructor


    public void findCirclePixels(){ 



        for (int i=centreX-50; i < centreX+50; ++i) {
            for (int y=centreY-50; y <centreY+50 ; ++y) {

    if( Math.sqrt( Math.pow(i - centreX, 2) + ( Math.pow(y - centreY, 2) ) ) <= radius ){

                    bgr.setPixel(i,y,Color.rgb(Progress+50,Progress,Progress+100));
                }
            }
        }

        }// end of changePixel()




    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {

            case MotionEvent.ACTION_DOWN: {

                centreX = (int) ev.getX();
                centreY = (int) ev.getY();
                findCirclePixels();
                invalidate();

                break;
            }

            case MotionEvent.ACTION_MOVE: {

                    centreX = (int) ev.getX();
                    centreY = (int) ev.getY();
                    findCirclePixels();
                    invalidate();
                    break;

            }           

            case MotionEvent.ACTION_UP: 

                break;

        }
        return true;
    }//end of onTouchEvent





    public void initSlider(final HorizontalSlider slider)
    {
        Log.e(TAG, "******setting up slider*********** ");
        slider.setOnProgressChangeListener(changeListener);
    }



    private OnProgressChangeListener changeListener = new OnProgressChangeListener() {


        @Override
        public void onProgressChanged(View v, int progress) {
            // TODO Auto-generated method stub

            setProgress(progress);
            Log.e(TAG, "***********progress = "+Progress);

        }
    };





    @Override
    public void onDraw(Canvas canvas){
        super.onDraw(canvas);


        canvas.drawBitmap(bgr, 0, 0, null);
        canvas.drawCircle(centreX, centreY, radius,pTouch);


    }//end of onDraw




    protected void setProgress(int progress2) {
        this.Progress = progress2;
        findCirclePixels();
        invalidate();

    }


}
turtleboy
  • 8,210
  • 27
  • 100
  • 199

2 Answers2

8

As an alternative to runOnUiThread, you can use an AsyncTask, and use the publishProgress/onProgressUpdate mechanism to touch the Views. Google has a pretty good post about using AsyncTask, including which methods run in the task, and which ones run in the UI thread. Do your computations in doInBackground, and call publishProgress whenever there is some quanta of data to give to the UI to render. Then render that data in the onProgressUpdate function. Note that the parameters for both functions are quite arbitrary, and you can define them as you wish.

edit: Upon re-reading your question, I wonder if using a separate thread is the way to go, since you are taking input from the user as you are making your computations. It's possible that there could be a significant lag between the slider bar movement and the rendering, especially if the computation thread gets starved. If the computations are intense enough to warrant a separate thread, you may want to consider tossing up a progress bar during the computations, rather than confuse a user by having the rendering lag too far behind their movement of the slider bar. Either that, or you'll have to add in some logic to cancel the computation thread if a change is detected and the current render is incomplete, then fire it off again with the new parameters. For more information about cancelling AsyncTasks, read the introduction section(s) of the documentation for AsyncTask.

edit2: When I implemented my AsycTask, I defined an Object that contained all the elements I'd need: Views, Cursors, Exceptions, etc., and used that as a parameter for passing back and forth. I'm implying the same concept with TouchViewData, since the thread can't touch the View. Just pack it up with the data you need, and let the thread go crazy on it.

public class MyAsyncTask extends AsyncTask<TouchViewData, Object, void> {

    /*
     * These run on the UI thread, and can access the Views
     */
    protected void onProgressUpdate(Object... values) {
      // Here's where the magic happens! 
      //   Update your views, open dialogs, Toast the user, whatever!
    }

    protected void onPreExecute() {
      // Prep anything you need for the thread
    }

    protected void onPostExecute() {
      // Finalize any rendering or cleanup
    }

    protected void onCancelled() {
      // Got cancelled! Time to clean up
    }

    /*
     * This runs in a separate thread, and can not change the View at all
     */
    public void doInBackground(TouchViewData... params) {
      while(stillCalculating && ! isCancelled()) {
        // Do your calculations, then...
        publishProgress(...); // pass some data to the UI thread to render
      }
      return;
    }

}

In your Activity:

MyAppTask calculator = new MyAppTask();
calculator.execute(touchViewInstanceData, someObject);

...

// if you need to:
calculator.cancel();
Kenneth Cummins
  • 407
  • 1
  • 4
  • 13
  • @kvcummins hi, i suppose what i'm having trouble with is which of the 2 above files is the UI thread made from and where do i declare and instansiate the asynctask. sorry if it's a stupid question, i'm not too good with threads. – turtleboy May 17 '11 at 16:18
  • @kvcummins i can look at the asynctask documentation and i can understand what it does, but not sure where to put the different parts of asynctask in the above 2 files. – turtleboy May 17 '11 at 16:42
  • @turtleboy sorry I didn't answer sooner. You may have to do some serious refactoring. Extend AsyncTask, and implement doInBackground and onProgressUpdate in that class. Then instantiate it in your Activity and call the run function. You may need to pass a reference to your View and possibly more. That's it. If you pass a reference to your View, you can call its functions and update any UI bits and baubles from onProgressUpdate. – Kenneth Cummins May 17 '11 at 23:40
  • I'm on my phone, so I'm just going to add comments... :) It helps me to think about AsyncTask this way: Android fires up a new thread, calls onPreExecute, then the thread calls doInBackground, which can call publishProgress which causes the UI thread to call onProgressUpdate. But that does mean you need to pass references for everything you'll need in either doInBackground or onProgressUpdate... – Kenneth Cummins May 17 '11 at 23:59
  • @turtleboy I added some example code. One more thing to think about is that if you do cancel the thread due to user input, you may need to set up some sort of notification system so that you can restart the thread with new data when `onCancelled()` finally returns. – Kenneth Cummins May 18 '11 at 14:10
  • @kvcummins wow thanks for taking the time to explain this:), and no worries about the time frame, i work night-shift so gotta do coding when it fits. Again thanks alot. Matt – turtleboy May 18 '11 at 18:25
2

AsyncTask is easy to use: just do your stuff in doInBackground(...) and it will run in a separate thread.

However, take into account that you can't touch the views in that separate thread or you'll get a CalledFromWrongThreadException. Only the original thread that created a view hierarchy can touch its views.

However, if you really need to touch the views you can use:

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        //do your stuff here
    }
});
inazaruk
  • 74,247
  • 24
  • 188
  • 156
pandre
  • 6,685
  • 7
  • 42
  • 50