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();
}
}
As an alternative to
runOnUiThread, you can use anAsyncTask, and use thepublishProgress/onProgressUpdatemechanism to touch the Views. Google has a pretty good post about usingAsyncTask, including which methods run in the task, and which ones run in the UI thread. Do your computations indoInBackground, and callpublishProgresswhenever there is some quanta of data to give to the UI to render. Then render that data in theonProgressUpdatefunction. 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
cancelthe 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 cancellingAsyncTasks, read the introduction section(s) of the documentation forAsyncTask.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.
In your Activity: