开发者

Animation problem when using thread

开发者 https://www.devze.com 2023-03-29 01:18 出处:网络
so i\'m having a imageview and i have applied translate animation to it whose positions are the points where user touches...its working fine...bt when i added a time updater to the activity which is o

so i'm having a imageview and i have applied translate animation to it whose positions are the points where user touches...its working fine...bt when i added a time updater to the activity which is obviously running on a thread...the translate animation starts behaving abnormally, i.e. it just go out of bounds....i tried different methods..nothing worked..i eliminated the time udater and used dgital clock widget..but again it doesn't work as digital clock alse uses threads to update time...

take a look at the translate animation code

 public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub 

    if(event.getAction() == MotionEvent.ACTION_MOVE)
    {
        float f1 = event.getX();
        swidth=image.getWidth();
        ((MaskedTextView) findViewById(R.id.text)).setVisibility(View.INVISIBLE);
        android.widget.RelativeLayout.LayoutParams localLayoutParams = (android.widget.RelativeLayout.LayoutParams)image.getLayoutParams();
        barwidth=bar.getWidth();
        bwidth=barwidth-swidth;
        android.widget.RelativeLayout.LayoutParams localLayoutParams2 = localLayoutParams;
        localLayoutParams2.leftMargin=(int) event.getX();
        if (localLayoutParams.leftMargin<bwidth && f1<bwidth)
        {
        TranslateAnimation anim = new TranslateAnimation(localLayoutParams.leftMargin,f1, 0, 0);
         anim.setStartOffset(0L);
            anim.setDuration(500L);         
            anim.setFillBefore(true);
            image.startAnimation(anim);
            image.invalidate();
        }
    }

    if (event.getAction() == MotionEvent.ACTION_UP)
    {
        float f1=event.getX();
        android.widget.RelativeLayout.LayoutParams localLayoutParams = (android.widget.RelativeLayout.LayoutParams)image.getLayoutParams();


        if(f1<bwidth)
        {   
        TranslateAnimation anim=new TranslateAnimation(f1,0,0,0);   
        anim.setStartOffset(0L);
        anim.setDuration(500L);
        anim.setFillBefore(true);
        image.startAnimation(anim);
        image.invalidate();
        }
        else
        {
            if(soundsEnabled && !silent)
            mpDot.start();
            SwipeImage.this.finish();
        }


    }
    return true;
}

and this is the simple code of updating time every 60 seconds..

 new Timer().schedule(new MyTimerTask(), 0, 60000);

 public class MyTimerTask extends TimerTask {
    private Runnable runnable = new Runnable() {
        public void run() {
            Calendar c = Calendar.getInstance();
            int hr;

            if(DateFormat.is24HourFormat(getApplicationContext()))
                 hr=c.get(Calendar.HOUR_OF_DAY);
            else
                 hr=c.get(Calendar.HOUR);

            int minute = c.get(Calendar.MINUTE);
            if(minute<10)
                timeTextView.setText(String.valueOf(hr)+":0"+String.valueOf(minute));
            else
            timeTextView.setText(String.valueOf(hr)+":"+String.valueOf(minute));


        }
    };

    public void run() {

        handler.post(runnable);
        //invalidate();
    }
}*/

plz help 开发者_JAVA技巧me out guys...i just dont understand why its happening....thnx in advance


You are performing a very costly operation in onTouch. Animation is not required to move a view along with the users finger. What you need to do is in onTouch just update the view position(set new LayoutParams) and invalidate.

0

精彩评论

暂无评论...
验证码 换一张
取 消