为什么从GridView中随机调用onDraw?

时间:2015-09-01 01:40:51

标签: android gridview android-custom-view

我正在尝试创建一个在gridview的每个单元格上显示时钟的应用程序。它可以工作3或4个时钟,但在我上下滚动后,所有时钟都开始改变位置。当我调试时,我意识到onGraw目前没有调用gridView调用,但它稍后会被调用,可能是随机调用的。

如何跟踪onDraw并将正确的时钟放在正确的网格上?请帮我解决一下。我已经尝试了两天。

这是gridview代码:

 @Override 
        public View getView(final int position, View view, final ViewGroup viewGroup) {

        if (view == null) {
            holder = new ViewHolder(); 
            view = LayoutInflater.from(viewGroup.getContext())
                    .inflate(R.layout.grid_item, viewGroup, false);
            holder.wholeGrid = (LinearLayout) view.findViewById(R.id.whole_grid);
            holder.imageGrid = (FrameLayout) view.findViewById(R.id.myFrame);
            holder.headerText = (TextView) view.findViewById(R.id.header);
            holder.text = (TextView) view.findViewById(R.id.detailText);
            view.setTag(holder);
        } else { 
            holder = (ViewHolder) view.getTag();
        } 
        ................................ 

        Date denemeSaat = new Date(); 
        denemeSaat.setTime(milis); 

        int x = holder.imageGrid.getWidth();
        int y = holder.imageGrid.getHeight();

        View clockView = new CustomClock(getApplicationContext(), x,y, denemeSaat, position);
        holder.imageGrid.addView(clockView,1);
        Log.i("draw clock", "addView position" + position);

        ............... 
        return view;
    } 
}  

以下是自定义视图:

public class CustomClock extends View {
    private final float x;
    private final float y;
    private final int r=150;
    private final int position;
    private final Date date; 
    Drawable d = getResources().getDrawable(R.drawable.yelkovan);
    Drawable saat = getResources().getDrawable(R.drawable.white_clock);
    Drawable akrep = getResources().getDrawable(R.drawable.akrep);
    private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);


    public CustomClock(Context context, float x, float y, Date date, int position) {
        super(context);
        this.x = x;
        this.y = y;
        this.date=date;
        this.position=position;
    } 

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        float sec=(float)date.getSeconds();
        float min=(float)date.getMinutes();
        float hour=(float)date.getHours()+min/60.0f;
        int x = canvas.getWidth();

        saat.setBounds(0, 0, x, x);
        saat.draw(canvas);
        canvas.save();
        //canvas.drawLine(x, y, (float)(x+(r-15)*Math.cos(Math.toRadians((hour / 12.0f * 360.0f)-90f))), (float)(y+(r-10)*Math.sin(Math.toRadians((hour / 12.0f * 360.0f)-90f))), mPaint); 
        d.setBounds(0, 0, x, x);
        canvas.rotate(min * 6, x / 2, x / 2);
        d.draw(canvas);
        canvas.save();
        float degrees = hour*30 - min*6;
        akrep.setBounds(0, 0, x, x);
        canvas.rotate(degrees, x / 2, x / 2);
        akrep.draw(canvas);
        canvas.save();
        Log.i("draw clock", "Draw canvas position" + position);
    } 

我在日志之下:

08-31 20:20:41.444  13100-13100/org.xxxx.xxxx I/draw clock﹕ addView position0
08-31 20:20:41.444  13100-13100/org.xxxx.xxxx I/draw clock﹕ addView position0
08-31 20:20:41.455  13100-13100/org.xxxx.xxxx I/draw clock﹕ Draw canvas position0
08-31 20:20:41.455  13100-13100/org.xxxx.xxxx I/draw clock﹕ Draw canvas position0
08-31 20:20:41.455  13100-13100/org.xxxx.xxxx I/draw clock﹕ Draw canvas position0
08-31 20:20:41.455  13100-13100/org.xxxx.xxxx I/draw clock﹕ Draw canvas position1
08-31 20:20:41.455  13100-13100/org.xxxx.xxxx I/draw clock﹕ Draw canvas position2
08-31 20:20:41.455  13100-13100/org.xxxx.xxxx I/draw clock﹕ Draw canvas position3
08-31 20:21:24.455  13100-13100/org.xxxx.xxxx I/draw clock﹕ addView position4
08-31 20:21:24.455  13100-13100/org.xxxx.xxxx I/draw clock﹕ addView position5
08-31 20:21:24.465  13100-13100/org.xxxx.xxxx I/draw clock﹕ Draw canvas position4
08-31 20:21:24.465  13100-13100/org.xxxx.xxxx I/draw clock﹕ Draw canvas position0
08-31 20:21:24.465  13100-13100/org.xxxx.xxxx I/draw clock﹕ Draw canvas position0
08-31 20:21:24.465  13100-13100/org.xxxx.xxxx I/draw clock﹕ Draw canvas position0
08-31 20:21:24.465  13100-13100/org.xxxx.xxxx I/draw clock﹕ Draw canvas position0
08-31 20:21:24.465  13100-13100/org.xxxx.xxxx I/draw clock﹕ Draw canvas position5
08-31 20:21:45.936  13100-13100/org.xxxx.xxxx I/draw clock﹕ addView position6
08-31 20:21:45.946  13100-13100/org.xxxx.xxxx I/draw clock﹕ addView position7
08-31 20:21:45.955  13100-13100/org.xxxx.xxxx I/draw clock﹕ Draw canvas position6
08-31 20:21:45.955  13100-13100/org.xxxx.xxxx I/draw clock﹕ Draw canvas position1
08-31 20:21:45.965  13100-13100/org.xxxx.xxxx I/draw clock﹕ Draw canvas position7
08-31 20:21:45.965  13100-13100/org.xxxx.xxxx I/draw clock﹕ Draw canvas position0
08-31 20:21:45.965  13100-13100/org.xxxx.xxxx I/draw clock﹕ Draw canvas position0
08-31 20:21:45.965  13100-13100/org.xxxx.xxxx I/draw clock﹕ Draw canvas position0
08-31 20:21:48.125  13100-13100/org.xxxx.xxxx I/draw clock﹕ addView position8
08-31 20:21:48.125  13100-13100/org.xxxx.xxxx I/draw clock﹕ addView position9
08-31 20:21:48.125  13100-13100/org.xxxx.xxxx I/draw clock﹕ Draw canvas position8
08-31 20:21:48.125  13100-13100/org.xxxx.xxxx I/draw clock﹕ Draw canvas position3
08-31 20:21:48.125  13100-13100/org.xxxx.xxxx I/draw clock﹕ Draw canvas position9
08-31 20:21:48.125  13100-13100/org.xxxx.xxxx I/draw clock﹕ Draw canvas position2
08-31 20:21:49.285  13100-13100/org.xxxx.xxxx I/draw clock﹕ addView position4
08-31 20:21:49.285  13100-13100/org.xxxx.xxxx I/draw clock﹕ addView position5
08-31 20:21:49.285  13100-13100/org.xxxx.xxxx I/draw clock﹕ addView position2
08-31 20:21:49.295  13100-13100/org.xxxx.xxxx I/draw clock﹕ addView position3
08-31 20:21:49.295  13100-13100/org.xxxx.xxxx I/draw clock﹕ Draw canvas position2
08-31 20:21:49.295  13100-13100/org.xxxx.xxxx I/draw clock﹕ Draw canvas position8
08-31 20:21:49.305  13100-13100/org.xxxx.xxxx I/draw clock﹕ Draw canvas position3
08-31 20:21:49.305  13100-13100/org.xxxx.xxxx I/draw clock﹕ Draw canvas position3

0 个答案:

没有答案