提高重绘Android屏幕的速度

时间:2012-02-03 05:46:05

标签: android

我是Android新手。我的应用程序需要一遍又一遍地重绘画布圈。但是,重绘速度低于我想要的速度。我怎么能增加它。

我的代码如下:

- > ImagePracActivity.java

package com.pkg.ImagPrac;

  import android.app.Activity;
  import android.os.Bundle;
  import android.view.Window;
  import android.view.WindowManager;

   public class ImagePracActivity extends Activity {
      //DrawView drawView;
        movement mv;

   @Override
     public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set full screen view
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                         WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);    

        mv=new movement(getApplicationContext());            
        setContentView(mv);            
        mv.requestFocus();            
      }
   }

- > movement.java

package com.pkg.ImagPrac;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;

public class movement extends View implements OnTouchListener{    
    Display display;
    float x=0,y=0;
    Paint paint=new Paint();
    private boolean flag;

    public movement(Context context) {
        super(context);         
        setFocusable(true);
        setFocusableInTouchMode(true);    
        this.setOnTouchListener(this);

        paint.setColor(Color.BLUE);
        paint.setAntiAlias(true);

        display = ((WindowManager)   
        context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        x = 0;
        y = display.getHeight();            
    }

      @Override
         protected void onDraw(Canvas canvas) {             

           if(x<(display.getWidth()/2))
        {
          canvas.drawCircle(x++, y--, 5, paint);
        }           
          else if(x<(display.getWidth()))
        {
          canvas.drawCircle(x++, y++, 5, paint);
        }       

        this.invalidate();
    }    
          public boolean onTouch(View arg0, MotionEvent arg1) {
        // TODO Auto-generated method stub
        return false;
    }    
}

2 个答案:

答案 0 :(得分:0)

试试这个

 mPaint = new Paint();
 mPaint.setAntiAlias(true);
 mPaint.setDither(true);
 mPaint.setColor(Color.BLUE);
 mPaint.setStyle(Paint.Style.STROKE);
 mPaint.setStrokeJoin(Paint.Join.ROUND);
 mPaint.setStrokeCap(Paint.Cap.ROUND);
 mPaint.setStrokeWidth(5);

答案 1 :(得分:0)

A会建议您使用SurfaceView类而不是View,因为SurfaceView更快,并允许您将所有绘图逻辑放在单独的线程中。阅读有关SurfaceView的更多信息:http://developer.android.com/guide/topics/graphics/2d-graphics.html