本帖最后由 tianke0711 于 2013-05-07 21:13:53 编辑

解决方案 »

  1.   

    你用canvas,那就应该是2D绘图了,2D的话,地球自转使用matrix就行,我给你画布旋转的方法吧,比较简单:        Matrix m = new Matrix();
            m.postTranslate(x,y);//移动到地球中心点
            m.postRotate(degree);//旋转
            c.drawBitmap(bmp,m,paint);//画地球月球公转的话,使用下面函数来计算圆形上某点坐标:    protected float xn(float pointX,float pointY,float x,float y,double angle){
            double angleHude = Math.toRadians(angle);//角度转弧度
            double cosAngle = Math.cos(angleHude);
            double sinAngle = Math.sin(angleHude);
            return (float)((pointX-x)*cosAngle-(pointY-y)*sinAngle+x);
        }    protected float yn(float pointX,float pointY,float x,float y,double angle){
            double angleHude = Math.toRadians(angle);//角度转弧度
            double cosAngle = Math.cos(angleHude);
            double sinAngle = Math.sin(angleHude);
            return (float)((pointX-x)*sinAngle+(pointY-y)*cosAngle+y);
        }
      

  2.   

    大虾你好:
           我用你的代码画地球的时候,我想让地球图片在屏幕中间固定旋转;比如   
                  Matrix m = new Matrix();
    m.postTranslate(width/2-bitmap_earth.getWidth()/2, height/2-bitmap_earth.getHeight()/2);
    m.postRotate(10);
    canvas.drawBitmap(bitmap_earth, m, paint1);
      开始旋转角度10度的时候,还在屏幕内,当我变成m.postRotate(70)的时候,图片旋转出屏幕外了。
         我想就在中间,一直旋转,旋转一周,请指点一下啊,很多不会。
      还有就是月球protected float xn(float pointX,float pointY,float x,float y,double angle)函数中的参数
      pointX,PointY ,x ,y分别表示啥意思。
    能不能给一下完整的代码,呵呵,不好意思,很多不会。
       
      

  3.   

    那个是我没考虑周全
    先给你地球自转的吧,公转的我看看下班之前能不能给你写出来:package org.yye.android.commons.rotate;import android.app.Activity;
    import android.content.Context;
    import android.graphics.*;
    import android.os.Bundle;
    import android.os.Handler;import android.util.DisplayMetrics;
    import android.view.WindowManager;
    import android.widget.ImageView;
    import org.yye.android.commons.R;import java.util.HashMap;
    import java.util.Map;/**
     * User: ye.yang
     * Date: 13-5-10
     * Time: 上午10:57
     */
    public class RotateActivity extends Activity {
        private float degree = 0f;
        private Canvas c;
        private Paint p;
        private Handler handler = new Handler();
        private ImageView iv;
        private int w,h;
        private Bitmap earthBmp;
        private int earthW,earthH;
        private Runnable task = new Runnable() {
            @Override
            public void run() {
                handler.postDelayed(this,(long)(0.01*1000));
                degree++;
                //Bitmap backgroundBmp = BitmapFactory.decodeResource(getResources(),R.drawable.grid);
                Bitmap backgroundBmp = Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888);
                c = new Canvas(backgroundBmp);
                p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);            Rect r = new Rect(w/2-earthW/2,h/2-earthH/2,w/2+earthW/2,h/2+earthH/2);            c.save();
                c.rotate(degree, w / 2, h / 2);
                c.drawBitmap(earthBmp, null, r, p);
                c.restore();
                p.setStyle(Paint.Style.STROKE);
                c.drawRect(r,p);            iv.setImageBitmap(backgroundBmp);
            }
        };    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.rotate);
            w=getMetrics().get("width");
            h=getMetrics().get("height");
            earthBmp = BitmapFactory.decodeResource(getResources(),R.drawable.earth);
            earthW = earthBmp.getWidth();
            earthH = earthBmp.getHeight();
            iv = (ImageView)findViewById(R.id.rotate_iv);
            handler.post(task);
        }    private Map<String, Integer> getMetrics() {
            DisplayMetrics displayMetrics = new DisplayMetrics();
            WindowManager wm = getWindowManager();
            if(wm == null)
                wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
            if (wm != null) {
                wm.getDefaultDisplay().getMetrics(displayMetrics);
            }
            int height = displayMetrics.heightPixels;
            int width = displayMetrics.widthPixels;
            Map<String, Integer> resultMap = new HashMap<String, Integer>();
            resultMap.put("width", width);
            resultMap.put("height", height);
            return resultMap;
        }    protected float xn(float pointX,float pointY,float x,float y,double angle){
            double angleHude = Math.toRadians(angle);//角度转弧度
            double cosAngle = Math.cos(angleHude);
            double sinAngle = Math.sin(angleHude);
            return (float)((pointX-x)*cosAngle-(pointY-y)*sinAngle+x);
        }    protected float yn(float pointX,float pointY,float x,float y,double angle){
            double angleHude = Math.toRadians(angle);//角度转弧度
            double cosAngle = Math.cos(angleHude);
            double sinAngle = Math.sin(angleHude);
            return (float)((pointX-x)*sinAngle+(pointY-y)*cosAngle+y);
        }
    }
      

  4.   

    要下班了,先给你发一个吧,公转速度计算得有问题,中心点计算也有问题,你先看看吧。。:package org.yye.android.commons.rotate;import android.app.Activity;
    import android.content.Context;
    import android.graphics.*;
    import android.os.Bundle;
    import android.os.Handler;import android.util.DisplayMetrics;
    import android.view.WindowManager;
    import android.widget.ImageView;
    import org.yye.android.commons.R;import java.util.HashMap;
    import java.util.Map;/**
     * User: ye.yang
     * Date: 13-5-10
     * Time: 上午10:57
     */
    public class RotateActivity extends Activity {
        private float earthDegree = 0f;
        private float moonDegree = 0f;    private Canvas c;
        private Paint p;
        private Handler handler = new Handler();
        private ImageView iv;
        private int w,h;
        private Bitmap earthBmp,moonBmp;
        private int earthW,earthH;
        private float moonX,moonY;
        private Runnable task = new Runnable() {
            @Override
            public void run() {
                handler.postDelayed(this,(long)(0.01*1000));
                earthDegree++;
                Bitmap backgroundBmp = Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888);
                c = new Canvas(backgroundBmp);
                p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);            Rect r = new Rect(w/2-earthW/2,h/2-earthH/2,w/2+earthW/2,h/2+earthH/2);            c.save();
                c.rotate(earthDegree, w / 2, h / 2);
                c.drawBitmap(earthBmp, null, r, p);
                c.restore();
                p.setStyle(Paint.Style.STROKE);
                c.drawRect(r,p);            moonDegree+=(1f/30);//自转一天的角度=公转30天的角度,都是360°,所以简单/30
                float tempX = moonX;
                moonX = xn(tempX,moonY,w/2,h/2, moonDegree);
                moonY = yn(tempX,moonY,w/2,h/2, moonDegree);
                c.drawBitmap(moonBmp,moonX,moonY,p);            iv.setImageBitmap(backgroundBmp);
            }
        };    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.rotate);
            w=getMetrics().get("width");
            h=getMetrics().get("height");
            earthBmp = BitmapFactory.decodeResource(getResources(), R.drawable.earth);
            moonBmp = BitmapFactory.decodeResource(getResources(), R.drawable.moon);
            earthW = earthBmp.getWidth();
            earthH = earthBmp.getHeight();
            moonX = w/2+100;
            moonY = h/2;
            iv = (ImageView)findViewById(R.id.rotate_iv);
            handler.post(task);
        }    private Map<String, Integer> getMetrics() {
            DisplayMetrics displayMetrics = new DisplayMetrics();
            WindowManager wm = getWindowManager();
            if(wm == null)
                wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
            if (wm != null) {
                wm.getDefaultDisplay().getMetrics(displayMetrics);
            }
            int height = displayMetrics.heightPixels;
            int width = displayMetrics.widthPixels;
            Map<String, Integer> resultMap = new HashMap<String, Integer>();
            resultMap.put("width", width);
            resultMap.put("height", height);
            return resultMap;
        }    protected float xn(float pointX,float pointY,float x,float y,double angle){
            double angleHude = Math.toRadians(angle);//角度转弧度
            double cosAngle = Math.cos(angleHude);
            double sinAngle = Math.sin(angleHude);
            return (float)((pointX-x)*cosAngle-(pointY-y)*sinAngle+x);
        }    protected float yn(float pointX,float pointY,float x,float y,double angle){
            double angleHude = Math.toRadians(angle);//角度转弧度
            double cosAngle = Math.cos(angleHude);
            double sinAngle = Math.sin(angleHude);
            return (float)((pointX-x)*sinAngle+(pointY-y)*cosAngle+y);
        }
    }
      

  5.   

    http://www.eoeandroid.com/thread-653-1-1.html 看一下这个
      

  6.   

    原来想太多了,其实公转也可以用同样的方法实现,这个应该是没问题了,在本机跑过了:
    [code=java-android]
    package org.yye.android.commons.rotate;import android.app.Activity;
    import android.content.Context;
    import android.graphics.*;
    import android.os.Bundle;
    import android.os.Handler;import android.util.DisplayMetrics;import android.view.WindowManager;
    import android.widget.ImageView;
    import org.yye.android.commons.R;import java.util.HashMap;
    import java.util.Map;/**
     * User: ye.yang
     * Date: 13-5-10
     * Time: 上午10:57
     */
    public class RotateActivity extends Activity {
        private float earthDegree = 0f;
        private float moonDegree = 0f;    private Canvas c;
        private Paint p;
        private Handler handler = new Handler();
        private ImageView iv;
        private int w,h;
        private Bitmap earthBmp,moonBmp;
        private int earthW,earthH;
        private int moonW,moonH;
        private Runnable task = new Runnable() {
            @Override
            public void run() {
                handler.postDelayed(this,(long)(0.01*1000));
                earthDegree++;
                Bitmap backgroundBmp = Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888);
                c = new Canvas(backgroundBmp);
                p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);            Rect r = new Rect(w/2-earthW/2,h/2-earthH/2,w/2+earthW/2,h/2+earthH/2);            c.save();
                c.rotate(earthDegree, w / 2, h / 2);
                c.drawBitmap(earthBmp, null, r, p);
                c.restore();
                p.setStyle(Paint.Style.STROKE);
                c.drawRect(r,p);            //自转一天的角度=公转30天的角度,都是360°,所以简单/30
                moonDegree-=(1f/10);//除与30太慢了,就/10了
                Rect rMoon = new Rect(w/2-moonW/2-50,h/2-moonH/2-50,w/2+moonW/2-50,h/2+moonH/2-50);
                c.save();
                c.rotate(moonDegree,w/2,h/2);
                c.drawBitmap(moonBmp,null,rMoon,p);
                c.restore();            iv.setImageBitmap(backgroundBmp);
            }
        };    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.rotate);
            w=getMetrics().get("width");
            h=getMetrics().get("height");
            earthBmp = BitmapFactory.decodeResource(getResources(), R.drawable.earth);
            moonBmp = BitmapFactory.decodeResource(getResources(), R.drawable.moon);
            earthW = earthBmp.getWidth();
            earthH = earthBmp.getHeight();
            moonW = moonBmp.getWidth();
            moonH = moonBmp.getHeight();
            iv = (ImageView)findViewById(R.id.rotate_iv);
            handler.post(task);
        }    private Map<String, Integer> getMetrics() {
            DisplayMetrics displayMetrics = new DisplayMetrics();
            WindowManager wm = getWindowManager();
            if(wm == null)
                wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
            if (wm != null) {
                wm.getDefaultDisplay().getMetrics(displayMetrics);
            }
            int height = displayMetrics.heightPixels;
            int width = displayMetrics.widthPixels;
            Map<String, Integer> resultMap = new HashMap<String, Integer>();
            resultMap.put("width", width);
            resultMap.put("height", height);
            return resultMap;
        }
    }
    [/code]