使用线程时的动画问题
问题描述:
所以我有一个imageview,我已经应用翻译动画,它的位置是用户触摸点...其工作正常... bt当我添加时间更新到活动这显然是在一个线程上运行...翻译动画开始行为异常,即它只是出界...我尝试了不同的方法..没有工作..我消除了时间udater和使用dgital时钟部件..但同样不作为数字时钟ALSE使用线程更新时间工作...使用线程时的动画问题
看一看的翻译动画代码
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;
}
这是更新时间每60秒的简单代码..
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帮助我出去家伙...我只是不明白为什么它的发生....日Thnx提前
答
您正在执行非常昂贵的操作在onTouch
。动画不需要随用户手指一起移动视图。你需要做的是在onTouch
只更新视图位置(设置新的LayoutParams
)并使其无效。
得到了你的观点,但我必须显示流畅的运动..为什么m使用动画...好吧,如果我xclude它...否线程问题将得到解决?? – user632905
不知道。我认为动画在这种情况下无助于平滑运动。删除动画应该会在拖动视图的同时提高整体性能。 – Ronnie
k会尝试dat ... thnx 4 dat建议虽然.. – user632905