0

我从她那里得到了一些示例代码来更新计时器的文本控件。接下来我创建了一个自定义视图并覆盖了 onDraw,这样我就可以为视图制作动画了。自定义视图有一个位图,现在只需在每次计时器关闭时将其向下移动一个像素,然后在工作后变得更加精美。

我正在考虑从计时器调用中调用我的自定义视图无效,因此将调用我的 OnDraw 函数。但是我不知道它是否来自计时器,这意味着我将不得不移动位图,或者只是来自 android 更新屏幕。

做这个的最好方式是什么???带计时器的代码

package besttecksolutions.tellafortune;

/////////////
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Handler.Callback;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class cShuffle2  extends Activity   {
    TextView text3;
    long starttime = 0;
    //this  posts a message to the main thread from our timertask
    //and updates the textfield

   //runs without timer be reposting self
   Handler h2 = new Handler();
   Runnable run = new Runnable() {

        @Override
        public void run() {
           long millis = System.currentTimeMillis() - starttime;
           int seconds = (int) (millis / 1000);
           int minutes = seconds / 60;
           seconds     = seconds % 60;

           text3.setText(String.format("%d:%02d", minutes, seconds));

           h2.postDelayed(this, 500);
        }
    };

      @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shuffle2);

        text3 = (TextView)findViewById(R.id.text3);



        h2.postDelayed(run, 0);

        LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);


        // Add the adView to it
        cShuffleView2 myView;
        myView=new cShuffleView2(this);
            layout.addView(myView);

    }

}

执行动画的自定义视图类

package besttecksolutions.tellafortune;



import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import java.io.InputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.content.Intent;
import android.content.Context;

import android.os.Bundle;
import android.view.View;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.os.Bundle;
import android.view.*;
import android.view.View.OnClickListener;

public class cShuffleView2 extends View {

 View t;
 Bitmap icon;

 cShuffleView2( Context context)
    {
        super(context);


        // load in bitmaps
        try {
        AssetManager assetManager= context.getAssets();
       InputStream inputStream;
       inputStream=assetManager.open("bio.png");  
       icon=BitmapFactory.decodeStream(inputStream);
       inputStream.close();
        } catch( IOException e)
        {

        }

    }

 protected void onDraw( Canvas canvas)
    {
    Paint color = new Paint();
    color.setColor(0xffffff00);
    canvas.drawRect(0,0, getWidth(),getHeight(),color);
    canvas.drawBitmap(icon, 100,100,null);

    }
}
4

1 回答 1

0

Create a public boolean field, or private with get/set, and set it to true in your timer callback (e.g. myView.updateBitmap = true;). In onDraw, you can then test it:

if (this.updateBitmap){

    // update bitmap
    this.updateBitmap = false;

}

updateBitmap can only be true if it was set in your timer callback (assuming you don't use it anywhere else).

Even better would be to implement your own callback on your extended View then call that directly from the timer.

于 2012-06-05T16:12:14.197 回答