解决方案 »
- android ListView Cursor的问题
- Activity自己停止自己
- 录音问题 unsupported parameter: x-pvmf/media-input-node/cap-config
- Android Sim卡工具包是怎么实现的???
- android在锁屏状态下能监控电源键并触发事件(程序)吗?
- 搜集的手机开发资料,大家有兴趣的话可以去看看
- 全志 A20内核clean命令
- myeclipse android 虚拟机启动时报的错,请亲们帮忙看看!在此谢过。
- handler 中使用lock.lock锁住代码块,这样做有必要么?是符合逻辑的吗?hanlder中会出现多线程的情况?
- 如何自定义JieCaoVedio的UI?
- Android
- Android 4.0真机系统无法连接Tomcat服务器
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);
}
我用你的代码画地球的时候,我想让地球图片在屏幕中间固定旋转;比如
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分别表示啥意思。
能不能给一下完整的代码,呵呵,不好意思,很多不会。
先给你地球自转的吧,公转的我看看下班之前能不能给你写出来: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);
}
}
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);
}
}
[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]