我在viewpager中发生页面滑动时绘制滑块。所以我在ViewPager onPageChange事件中绘制它。问题是; onDraw方法是(有时)没有被调用。 相同的代码在其他项目中工作,但不是在这一个,这使得它更奇怪。
我的SliderBar View类;
public class SliderBar extends View {
Paint p = new Paint();
int startX;
int width;
Rect r = new Rect();
public SliderBar(Context c,int x,int width){
super(c);
this.startX = x;
this.width = width;
this.setWillNotDraw(false);// these didnt fix
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
p.setColor(Color.parseColor("#bbbbbbbb"));
p.setAntiAlias(true);
p.setStyle(Paint.Style.FILL);
r.set(startX,0,startX+width,500);
canvas.drawRect(r,p);
}
}
我的通话地点
cvp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
int width = cvp.getWidth()/btnCount;
int currentPos = cvp.getCurrentItem();
int startX;
if(currentPos> position)
startX = (currentPos-1) * width - (cvp.getWidth()-positionOffsetPixels)/btnCount;
else
startX = (currentPos-1) * width + positionOffsetPixels/btnCount;
SliderBar dsb = new SliderBar(getBaseContext(),startX,width);
ll.removeAllViews();
ll.addView(dsb,lp);
ll.invalidate(); // these didnt fix
mainLayout.invalidate();// these didnt fix
}
@Override
public void onPageSelected(int position) {}
@Override
public void onPageScrollStateChanged(int state) {}
});
My LinearLayout
android:layout_width="match_parent" android:orientation="vertical"
android:background="#77000000"
android:layout_height="0dp"
android:layout_weight="1" android:id="@+id/ll"
答案 0 :(得分:1)
尝试在意图中添加标记FLAG_ACTIVITY_NO_ANIMATION
。
Intent intent = new Intent(SourceActivity.this, TargetActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
答案 1 :(得分:0)
View#invalidate()
可能无效。因此FLAG_ACTIVITY_NO_ANIMATION
可以解决您的问题。
在我的情况下onDraw()
方法在动画帧更新期间不调用。