的onDraw方法在我的应用程序不叫 [英] onDraw method not called in my application

查看:323
本文介绍了的onDraw方法在我的应用程序不叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发的应用程序,在的onDraw()被调用。我使用下面的逻辑。

所以,请指导我在那里做了错误的。

MainActivity

 公共类您好扩展活动实现SensorListener
{
  私人MyView的MyView的;
  私人的SensorManager的SensorManager;  公共无效onAccuracyChanged(INT paramInt1,诠释paramInt2)
  {
  }  公共无效的onCreate(捆绑paramBundle)
  {
    super.onCreate(paramBundle);
    Log.e(你好,你好);
    this.myView =新的MyView的(本);
    的setContentView(this.myView);
    this.sensorManager =((的SensorManager)getSystemService(传感));
    this.myView.setTheme(2);
  }  保护无效onResume()
  {
    super.onResume();
    this.sensorManager.registerListener(此,3,0);
  }  公共无效onSensorChanged(INT paramInt,浮动[] paramArrayOfFloat)
  {
    开关(paramInt)
    {
    默认:
    情况1:
    案例2:
    }
    this.myView.resetTama();
    做
    {
      做
        返回;
      而(Math.abs(paramArrayOfFloat [2])&下; = 50.0F);
    }
    而(Math.abs(paramArrayOfFloat [0])&下; = 15.0F);  }  公共无效onWindowFocusChanged(布尔paramBoolean)
  {
    super.onWindowFocusChanged(paramBoolean);
    如果(paramBoolean)
      this.myView.init();
  }
}

MyView的类

 进口android.content.Context;
进口android.content.res.Resources;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.graphics.Canvas;
进口android.graphics.Paint;
进口android.util.Log;
进口android.view.MotionEvent;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.ImageButton;
进口android.widget.RelativeLayout;
进口android.widget.RelativeLayout.LayoutParams;
进口android.widget.TextView;
进口java.lang.reflect.Array中;公共类MyView的扩展的RelativeLayout
{
  私人最终诠释FP = -1;
  私人最终诠释WC = -2;
  公共布尔disableApplication = FALSE;
  私人INT imgType = 1;
  公共布尔isBlack = FALSE;
  布尔将IsInitialized;
  私人位图MYBITMAP;
  私人油漆myPaint =新的油漆();
  私人诠释[]号;
  私人摩[] oyatamaArray;
  私人摩[] [] tamaArray;  公共MyView的(语境paramContext)
  {
    超(paramContext);
    Log.e(MyView的,MyView的);
    setFocusable(真);
    setBackgroundResource(R.drawable.haikei);
    setWillNotDraw(假);
    this.myBitmap = BitmapFactory.de codeResource(的getContext()getResources(),R.drawable.tama);
    this.numbers =新INT [6];
    的for(int i = 0; I< numbers.length;我++)
    {
      如果(ⅰ&GT = this.numbers.length)
      {
        loadChangeThemeButton(paramContext);
        新的TextView(paramContext).setBackgroundResource(R.drawable.bar);
        返回;
      }
      this.numbers [I] = 0;
    }
  }  私人无效changeBackgroundImage()
  {
    INT I = 1 + this.imgType;
    this.imgType = I;
    如果(ⅰ→3)
      this.imgType = 1;
    setTheme(this.imgType);
  }  私人无效loadChangeThemeButton(上下文paramContext)
  {
    的ImageButton localImageButton =新的ImageButton(paramContext);
    localImageButton.setBackgroundResource(R.drawable.themebutton);
    RelativeLayout.LayoutParams localLayoutParams =新RelativeLayout.LayoutParams(-2,-2);
    localLayoutParams.addRule(9);
    localLayoutParams.addRule(12);
    localLayoutParams.setMargins(0,0,30,30);
    addView(localImageButton,localLayoutParams);
    localImageButton.setOnClickListener(新View.OnClickListener()
    {
      公共无效的onClick(查看paramView)
      {
        MyView.this.changeBackgroundImage();
      }
    });
  }  私人位图returnNumberImg(资源paramResources,诠释paramInt)
  {
    开关(paramInt)
    {
    默认:
      返回BitmapFactory.de codeResource(paramResources,R.drawable.n0);
    情况1:
      返回BitmapFactory.de codeResource(paramResources,R.drawable.n1);
    案例2:
      返回BitmapFactory.de codeResource(paramResources,R.drawable.n2);
    案例3:
      返回BitmapFactory.de codeResource(paramResources,R.drawable.n3);
    情况4:
      返回BitmapFactory.de codeResource(paramResources,R.drawable.n4);
    情况5:
      返回BitmapFactory.de codeResource(paramResources,R.drawable.n5);
    情况6:
      返回BitmapFactory.de codeResource(paramResources,R.drawable.n6);
    案例7:
      返回BitmapFactory.de codeResource(paramResources,R.drawable.n7);
    案例8:
      返回BitmapFactory.de codeResource(paramResources,R.drawable.n8);
    案例9:
    }
    返回BitmapFactory.de codeResource(paramResources,R.drawable.n9);
  }  私人无效settingTamasTouchX(INT paramInt1,诠释paramInt2)
  {
    INT I = 0;
    如果(ⅰ&GT = this.oyatamaArray.length);
    时int k;
    对于(INT J = 0; J ++)
    {
      如果(J> = this.tamaArray.length)
      {
        返回;
      }
      K = 0;
      如果(K< this.tamaArray [J]。长度)
      {
          如果(this.tamaArray [J] [K] .checkArea(paramInt1,paramInt2));
            对于(this.tamaArray [J] [K] .touchX = paramInt1; this.tamaArray [J] [K] .touchX = 0)
            {
              ķ++;
              打破;
            }
//打破label90;
      }
    }
  }  公共无效的init()
  {
    INT I =的getHeight()/ 7;
    INT [] arrayOfInt =新INT [6];
    INT J = 5;
    如果(J&小于0)
      this.oyatamaArray =新多摩[6];
    INT米;
    INT的i1 = 0;
    对于(INT K = 0;; k ++)
    {
      如果(K> = this.oyatamaArray.length)
      {
        this.tamaArray =((多摩[] [])Array.newInstance(Tama.class,新INT [] {6,4}));
        M = 0;
        如果(M< this.tamaArray.length)
        {            对于(INT N = 0;; N ++)
            {
              如果(N> = this.tamaArray [M]。长度)
              {
                M +;
                打破;
              }
              this.tamaArray [M] [N] =新的多摩(40 + N * 37,arrayOfInt [M] - 32,N + M * 10);
            }
//打破label154;
        }
        的i1 = 0;
        如果(I1< this.tamaArray.length)
        {
            为(中间体I2 = 0; I2 +)
            {
              如果(I> = this.tamaArray [I1]。长度)
              {
                I1 ++;
                打破;
              }
              如果(I2 = this.tamaArray [I1]。长度 - 1)
                this.tamaArray [I1] [12] .ueTama = this.tamaArray [I1] [(I 2 + 1)];
              如果(I2 == 0)
                继续;
              this.tamaArray [I1] [12] .shitaTama = this.tamaArray [I1] [(I 2 - 1)];
            }
//打破label222;
        }
        this.isInitialized = TRUE;
        返回;
      }
      this.oyatamaArray [K] =新的多摩(279,arrayOfInt [K] - 32,K);
      this.oyatamaArray [K] .isOya = TRUE;
    }
  }  @覆盖
  保护无效dispatchDraw(帆布油画){        super.dispatchDraw(画布);
        Log.e(......这.........,绘图);
    }
@覆盖
  保护无效的onDraw(帆布paramCanvas)
  {      super.onDraw(paramCanvas);
      Log.e(本上画,......是的);    如果(!this.isInitialized)
      在里面();
    INT I = 0;
    资源localResources;
    INT米;
    而(真)
    {
      诠释J;
      如果(ⅰ&GT = this.oyatamaArray.length)
      {
        J = 0;
        如果(J> = this.tamaArray.length)
        {
          localResources =的getContext()getResources()。
          M = 0;
          如果(M 7;)
            打破;
          返回;
        }
      }
      其他
      {
        paramCanvas.drawBitmap(this.myBitmap,this.oyatamaArray [I] .getX(),this.oyatamaArray [I] .getY(),this.myPaint);
        我++;
        继续;
      }
      对于(INT K = 0;; k ++)
      {
        如果(K> = this.tamaArray [J]。长度)
        {
          J ++;
          打破;
        }
        paramCanvas.drawBitmap(this.myBitmap,this.tamaArray [J] [K] .getX(),this.tamaArray [J] [K] .getY(),this.myPaint);
      }
    }
    如果(this.numbers.length&下; = m)个
    {
    对(位图localBitmap = BitmapFactory.de codeResource(localResources,2130837523); localBitmap = returnNumberImg(localResources,this.numbers [M]))
    {
      paramCanvas.drawBitmap(localBitmap,8.0F,的getHeight()/ 2 + M * 18,this.myPaint);
      M +;
      打破;
    }
    }
  }  公共布尔onTouchEvent(MotionEvent paramMotionEvent)
  {
    INT I =(INT)paramMotionEvent.getX();
    INT J =(int)的paramMotionEvent.getY();
    如果((paramMotionEvent.getAction()== 0)及及(I&GT = 0)及及(I&下; = 40)及及(410下; = j的)及及( J< = 430))
      changeBackgroundImage();
    如果(paramMotionEvent.getAction()== 0)
      settingTamasTouchX(I,J);
    如果(1 == paramMotionEvent.getAction())
      settingTamasTouchX(0,0);
    时int k;
    INT m为0;
    如果(2 == paramMotionEvent.getAction())
    {
      K = 0;
      如果(K> = this.oyatamaArray.length)
      {
        M = 0;
        如果(M< this.tamaArray.length)
        {
            对于(INT N = 0;; N ++)
            {
              如果(N> = this.tamaArray [M]。长度)
              {
                M +;
                打破;
              }
              this.tamaArray [米] [n]的.checkAndSetArea(I,J);
              如果(!this.tamaArray [米] [n]的.isUp)
                继续;
              INT [] = arrayOfInt this.numbers;
              arrayOfInt [米] =(1 + arrayOfInt [米]);
            }
//打破label164;
        }
        无效();
      }
    }
    其他
    {
      返回true;
    }
    this.oyatamaArray [K] .checkAndSetArea(I,J);
    如果(this.oyatamaArray [k]的.isUp)
      this.numbers [K] = 5;
    而(真)
    {
      ķ++;
      打破;
    }
// label164:
    返回disableApplication;
  }  公共无效resetTama()
  {
    诠释J;
    的for(int i = 0; i ++在)
    {
      如果(ⅰ&GT = this.oyatamaArray.length)
      {
        J = 0;
        如果(J< this.tamaArray.length)
          打破;
        无效();
        返回;
      }
      this.oyatamaArray [I] .moveX(this.oyatamaArray [I] .startX);
      this.oyatamaArray [I] .isUp = FALSE;
      this.numbers [I] = 0;
    }
    对于(INT K = 0;; k ++)
    {
      如果(K> = this.tamaArray [J]。长度)
      {
        J ++;
        打破;
      }
      this.tamaArray [J] [K] .moveX(this.tamaArray [J] [K] .startX);
      this.tamaArray [J] [K] .isUp = FALSE;
    }
  }  公共无效sensorChange(浮动[] paramArrayOfFloat)
  {
    INT I = 0;
    如果(paramArrayOfFloat [2]所述; -50.0F)
      I = 0 - 3;
    时int k;
    而(真)
    {
      INT J = 0;
      如果(J> = this.oyatamaArray.length)
      {
        K = 0;
        如果(K< this.tamaArray.length)
          打破;
        如果(ⅰ!= 0)
          无效();
        返回;
      }
      其他
      {
        this.oyatamaArray [J] .moveX(1 + this.oyatamaArray [J] .getX());
        如果(this.oyatamaArray [J] .isUp)
          this.numbers [J] = 5;
        而(真)
        {
          J ++;
          打破;
        }
      }
    }
    为(中间体m为0; M +)
    {
      如果(M> = this.tamaArray [K]。长度)
      {
        ķ++;
        打破;
      }
      this.tamaArray [k]的[米] .moveX第(i + this.tamaArray [k]的[米] .getX());
      如果(!this.tamaArray [k]的[米] .isUp)
        继续;
      INT [] = arrayOfInt this.numbers;
      arrayOfInt [K] =(1 + arrayOfInt [K]);
    }
  }  公共无效setTheme(INT paramInt)
  {
    资源localResources =的getContext()getResources()。
    如果(paramInt == 1)
    {
      this.myBitmap = BitmapFactory.de codeResource(localResources,R.drawable.tama);
      setBackgroundResource(R.drawable.haikei);
    }
    而(真)
    {
      无效();
//返回;
      如果(paramInt == 2)
      {
        this.myBitmap = BitmapFactory.de codeResource(localResources,R.drawable.tama2);
        setBackgroundResource(2130837510);
        继续;
      }
      this.myBitmap = BitmapFactory.de codeResource(localResources,R.drawable.tama3);
      setBackgroundResource(2130837510);    }
  }
}

的main.xml

 <?XML版本=1.0编码=UTF-8&GT?;
<的FrameLayout机器人:ID =@ ID / FrameLayout01
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
   />


解决方案

试试下面的工作原理

要编辑之前发布的问题。

如果您是一个扩展的ViewGroup应该重写 dispatchDraw()而不是的onDraw()

探讨的话题@

https://groups.google.com/forum/?fromgroups=#!topic/android-developers/oLccWfszuUo

 公共类您好延伸活动{    私人MyView的MyView的;
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);                super.onCreate(savedInstanceState);
                Log.e(你好,你好);
                this.myView =新的MyView的(本);
                的setContentView(this.myView);    }
     公共类MyView的扩展的RelativeLayout
       {      私人油漆myPaint =新的油漆();
      私人诠释[]号;
      公共MyView的(语境paramContext)
      {
        超(paramContext);
        Log.e(MyView的,MyView的);
        setFocusable(真);
        setBackgroundResource(R.drawable.ic_launcher);
      }
      @覆盖
      保护无效dispatchDraw(帆布油画){            super.dispatchDraw(画布);
            Log.i(...............,绘图);
        }  }
}

保护无效dispatchDraw(帆布油画)

通过抽签调用绘制子视图。这可以通过派生类中重写增益控制其子绘制之前(但自己的看法已经绘就后)。

参数

画布上绘制视图的画布

如果您覆盖的onDraw 在矿井构造函数调用 setWillNotDraw(假)那么它应该工作。

http://developer.android.com/reference/android/view/View.html#setWillNotDraw(boolean)

 公共MyView的(语境paramContext)
      {
        超(paramContext);
        Log.e(MyView的,MyView的);
        setFocusable(真);
        setBackgroundResource(R.drawable.ic_launcher);
        setWillNotDraw(假);
      }

i am developing an application, in that onDraw() is called. i am using the following logic.

so, please guide where i did the mistake.

MainActivity

public class Hello extends Activity implements SensorListener
{
  private MyView myView;
  private SensorManager sensorManager;

  public void onAccuracyChanged(int paramInt1, int paramInt2)
  {
  }

  public void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    Log.e("hello", "hello");
    this.myView = new MyView(this);
    setContentView(this.myView);
    this.sensorManager = ((SensorManager)getSystemService("sensor"));
    this.myView.setTheme(2);
  }

  protected void onResume()
  {
    super.onResume();
    this.sensorManager.registerListener(this, 3, 0);
  }

  public void onSensorChanged(int paramInt, float[] paramArrayOfFloat)
  {
    switch (paramInt)
    {
    default:
    case 1:
    case 2:
    }
    this.myView.resetTama();
    do
    {
      do
        return;
      while (Math.abs(paramArrayOfFloat[2]) <= 50.0F);
    }
    while (Math.abs(paramArrayOfFloat[0]) <= 15.0F);

  }

  public void onWindowFocusChanged(boolean paramBoolean)
  {
    super.onWindowFocusChanged(paramBoolean);
    if (paramBoolean)
      this.myView.init();
  }
}

MyView class

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import java.lang.reflect.Array;

public class MyView extends RelativeLayout
{
  private final int FP = -1;
  private final int WC = -2;
  public boolean disableApplication = false;
  private int imgType = 1;
  public boolean isBlack = false;
  boolean isInitialized;
  private Bitmap myBitmap;
  private Paint myPaint = new Paint();
  private int[] numbers;
  private Tama[] oyatamaArray;
  private Tama[][] tamaArray;

  public MyView(Context paramContext)
  {
    super(paramContext);
    Log.e("MyView", "MyView");
    setFocusable(true);
    setBackgroundResource(R.drawable.haikei);
    setWillNotDraw(false); 
    this.myBitmap = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.tama);
    this.numbers = new int[6];


    for (int i = 0; i<numbers.length; i++)
    {
      if (i >= this.numbers.length)
      {
        loadChangeThemeButton(paramContext);
        new TextView(paramContext).setBackgroundResource(R.drawable.bar);
        return;
      }
      this.numbers[i] = 0;
    }
  }

  private void changeBackgroundImage()
  {
    int i = 1 + this.imgType;
    this.imgType = i;
    if (i > 3)
      this.imgType = 1;
    setTheme(this.imgType);
  }

  private void loadChangeThemeButton(Context paramContext)
  {
    ImageButton localImageButton = new ImageButton(paramContext);
    localImageButton.setBackgroundResource(R.drawable.themebutton);
    RelativeLayout.LayoutParams localLayoutParams = new RelativeLayout.LayoutParams(-2, -2);
    localLayoutParams.addRule(9);
    localLayoutParams.addRule(12);
    localLayoutParams.setMargins(0, 0, 30, 30);
    addView(localImageButton, localLayoutParams);
    localImageButton.setOnClickListener(new View.OnClickListener()
    {
      public void onClick(View paramView)
      {
        MyView.this.changeBackgroundImage();
      }
    });
  }

  private Bitmap returnNumberImg(Resources paramResources, int paramInt)
  {
    switch (paramInt)
    {
    default:
      return BitmapFactory.decodeResource(paramResources, R.drawable.n0);
    case 1:
      return BitmapFactory.decodeResource(paramResources, R.drawable.n1);
    case 2:
      return BitmapFactory.decodeResource(paramResources, R.drawable.n2);
    case 3:
      return BitmapFactory.decodeResource(paramResources, R.drawable.n3);
    case 4:
      return BitmapFactory.decodeResource(paramResources, R.drawable.n4);
    case 5:
      return BitmapFactory.decodeResource(paramResources, R.drawable.n5);
    case 6:
      return BitmapFactory.decodeResource(paramResources, R.drawable.n6);
    case 7:
      return BitmapFactory.decodeResource(paramResources, R.drawable.n7);
    case 8:
      return BitmapFactory.decodeResource(paramResources, R.drawable.n8);
    case 9:
    }
    return BitmapFactory.decodeResource(paramResources, R.drawable.n9);
  }

  private void settingTamasTouchX(int paramInt1, int paramInt2)
  {
    int i = 0;
    if (i >= this.oyatamaArray.length);
    int k;
    for (int j = 0; ; j++)
    {
      if (j >= this.tamaArray.length)
      {
        return;
      }
      k = 0;
      if (k < this.tamaArray[j].length)
      {
          if (this.tamaArray[j][k].checkArea(paramInt1, paramInt2));
            for (this.tamaArray[j][k].touchX = paramInt1; ; this.tamaArray[j][k].touchX = 0)
            {
              k++;
              break;
            }
//        break label90;
      }
    }
  }

  public void init()
  {
    int i = getHeight() / 7;
    int[] arrayOfInt = new int[6];
    int j = 5;
    if (j < 0)
      this.oyatamaArray = new Tama[6];
    int m;
    int i1 = 0;
    for (int k = 0; ; k++)
    {
      if (k >= this.oyatamaArray.length)
      {
        this.tamaArray = ((Tama[][])Array.newInstance(Tama.class, new int[] { 6, 4 }));
        m = 0;
        if (m < this.tamaArray.length)
        {

            for (int n = 0; ; n++)
            {
              if (n >= this.tamaArray[m].length)
              {
                m++;
                break;
              }
              this.tamaArray[m][n] = new Tama(40 + n * 37, arrayOfInt[m] - 32, n + m * 10);
            }
//          break label154;
        }
        i1 = 0;
        if (i1 < this.tamaArray.length)
        {
            for (int i2 = 0; ; i2++)
            {
              if (i2 >= this.tamaArray[i1].length)
              {
                i1++;
                break;
              }
              if (i2 != this.tamaArray[i1].length - 1)
                this.tamaArray[i1][i2].ueTama = this.tamaArray[i1][(i2 + 1)];
              if (i2 == 0)
                continue;
              this.tamaArray[i1][i2].shitaTama = this.tamaArray[i1][(i2 - 1)];
            }
//          break label222;
        }
        this.isInitialized = true;
        return;
      }
      this.oyatamaArray[k] = new Tama(279, arrayOfInt[k] - 32, k);
      this.oyatamaArray[k].isOya = true;
    }
  }



  @Override
  protected void dispatchDraw(Canvas canvas){         

        super.dispatchDraw(canvas);     
        Log.e("......this.........","drawing");   
    }


@Override
  protected void onDraw(Canvas paramCanvas)
  {

      super.onDraw(paramCanvas);


      Log.e("This on draw","....yes");

    if (!this.isInitialized)
      init();
    int i = 0;
    Resources localResources;
    int m;
    while (true)
    {
      int j;
      if (i >= this.oyatamaArray.length)
      {
        j = 0;
        if (j >= this.tamaArray.length)
        {
          localResources = getContext().getResources();
          m = 0;
          if (m < 7)
            break;
          return;
        }
      }
      else
      {
        paramCanvas.drawBitmap(this.myBitmap, this.oyatamaArray[i].getX(), this.oyatamaArray[i].getY(), this.myPaint);
        i++;
        continue;
      }
      for (int k = 0; ; k++)
      {
        if (k >= this.tamaArray[j].length)
        {
          j++;
          break;
        }
        paramCanvas.drawBitmap(this.myBitmap, this.tamaArray[j][k].getX(), this.tamaArray[j][k].getY(), this.myPaint);
      }
    }
    if (this.numbers.length <= m)
    {
    for (Bitmap localBitmap = BitmapFactory.decodeResource(localResources, 2130837523); ; localBitmap = returnNumberImg(localResources, this.numbers[m]))
    {
      paramCanvas.drawBitmap(localBitmap, 8.0F, getHeight() / 2 + m * 18, this.myPaint);
      m++;
      break;
    }
    }
  }

  public boolean onTouchEvent(MotionEvent paramMotionEvent)
  {
    int i = (int)paramMotionEvent.getX();
    int j = (int)paramMotionEvent.getY();
    if ((paramMotionEvent.getAction() == 0) && (i >= 0) && (i <= 40) && (410 <= j) && (j <= 430))
      changeBackgroundImage();
    if (paramMotionEvent.getAction() == 0)
      settingTamasTouchX(i, j);
    if (1 == paramMotionEvent.getAction())
      settingTamasTouchX(0, 0);
    int k;
    int m = 0;
    if (2 == paramMotionEvent.getAction())
    {
      k = 0;
      if (k >= this.oyatamaArray.length)
      {
        m = 0;
        if (m < this.tamaArray.length)
        {
            for (int n = 0; ; n++)
            {
              if (n >= this.tamaArray[m].length)
              {
                m++;
                break;
              }
              this.tamaArray[m][n].checkAndSetArea(i, j);
              if (!this.tamaArray[m][n].isUp)
                continue;
              int[] arrayOfInt = this.numbers;
              arrayOfInt[m] = (1 + arrayOfInt[m]);
            }
//          break label164;
        }
        invalidate();
      }
    }
    else
    {
      return true;
    }
    this.oyatamaArray[k].checkAndSetArea(i, j);
    if (this.oyatamaArray[k].isUp)
      this.numbers[k] = 5;
    while (true)
    {
      k++;
      break;
    }
//    label164: 
    return disableApplication;
  }

  public void resetTama()
  {
    int j;
    for (int i = 0; ; i++)
    {
      if (i >= this.oyatamaArray.length)
      {
        j = 0;
        if (j < this.tamaArray.length)
          break;
        invalidate();
        return;
      }
      this.oyatamaArray[i].moveX(this.oyatamaArray[i].startX);
      this.oyatamaArray[i].isUp = false;
      this.numbers[i] = 0;
    }
    for (int k = 0; ; k++)
    {
      if (k >= this.tamaArray[j].length)
      {
        j++;
        break;
      }
      this.tamaArray[j][k].moveX(this.tamaArray[j][k].startX);
      this.tamaArray[j][k].isUp = false;
    }
  }

  public void sensorChange(float[] paramArrayOfFloat)
  {
    int i = 0;
    if (paramArrayOfFloat[2] < -50.0F)
      i = 0 - 3;
    int k;
    while (true)
    {
      int j = 0;
      if (j >= this.oyatamaArray.length)
      {
        k = 0;
        if (k < this.tamaArray.length)
          break;
        if (i != 0)
          invalidate();
        return;
      }
      else
      {
        this.oyatamaArray[j].moveX(i + this.oyatamaArray[j].getX());
        if (this.oyatamaArray[j].isUp)
          this.numbers[j] = 5;
        while (true)
        {
          j++;
          break;
        }
      }
    }
    for (int m = 0; ; m++)
    {
      if (m >= this.tamaArray[k].length)
      {
        k++;
        break;
      }
      this.tamaArray[k][m].moveX(i + this.tamaArray[k][m].getX());
      if (!this.tamaArray[k][m].isUp)
        continue;
      int[] arrayOfInt = this.numbers;
      arrayOfInt[k] = (1 + arrayOfInt[k]);
    }
  }

  public void setTheme(int paramInt)
  {
    Resources localResources = getContext().getResources();
    if (paramInt == 1)
    {
      this.myBitmap = BitmapFactory.decodeResource(localResources, R.drawable.tama);
      setBackgroundResource(R.drawable.haikei);
    }
    while (true)
    {
      invalidate();
//      return;
      if (paramInt == 2)
      {
        this.myBitmap = BitmapFactory.decodeResource(localResources, R.drawable.tama2);
        setBackgroundResource(2130837510);
        continue;
      }
      this.myBitmap = BitmapFactory.decodeResource(localResources, R.drawable.tama3);
      setBackgroundResource(2130837510);

    }
  }
}

main.xml

    <?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="@id/FrameLayout01" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
   />

解决方案

Try the below it works

To the question posted before edit.

If you're extending a ViewGroup you should override dispatchDraw() instead of onDraw().

Discussion on the topic @

https://groups.google.com/forum/?fromgroups=#!topic/android-developers/oLccWfszuUo

public class Hello  extends Activity {

    private MyView myView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

                super.onCreate(savedInstanceState);
                Log.e("hello", "hello");
                this.myView = new MyView(this);
                setContentView(this.myView);

    }
     public class MyView extends RelativeLayout
       {

      private Paint myPaint = new Paint();
      private int[] numbers;
      public MyView(Context paramContext)
      {
        super(paramContext);
        Log.e("MyView", "MyView");
        setFocusable(true);
        setBackgroundResource(R.drawable.ic_launcher);
      }
      @Override
      protected void dispatchDraw(Canvas canvas){         

            super.dispatchDraw(canvas);     
            Log.i("...............","drawing");   
        }

  }
}

protected void dispatchDraw (Canvas canvas)

Called by draw to draw the child views. This may be overridden by derived classes to gain control just before its children are drawn (but after its own view has been drawn).

Parameters

canvas the canvas on which to draw the view

If you Override onDraw inthe constructor call setWillNotDraw(false) then it should work.

http://developer.android.com/reference/android/view/View.html#setWillNotDraw(boolean)

public MyView(Context paramContext)
      {
        super(paramContext);
        Log.e("MyView", "MyView");
        setFocusable(true);
        setBackgroundResource(R.drawable.ic_launcher);
        setWillNotDraw(false); 
      }

这篇关于的onDraw方法在我的应用程序不叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆